ClientTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_XmlRpc
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_XmlRpc_ClientTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_XmlRpc_ClientTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../TestHelper.php';
  27. require_once 'Zend/XmlRpc/Client.php';
  28. require_once 'Zend/XmlRpc/Response.php';
  29. require_once 'Zend/Http/Client/Adapter/Test.php';
  30. /**
  31. * Test case for Zend_XmlRpc_Value
  32. *
  33. * @category Zend
  34. * @package Zend_XmlRpc
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @group Zend_XmlRpc
  39. */
  40. class Zend_XmlRpc_ClientTest extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * @var Zend_Http_Client_Adapter_Abstract
  44. */
  45. protected $httpAdapter;
  46. /**
  47. * @var Zend_Http_Client
  48. */
  49. protected $httpClient;
  50. /**
  51. * @var Zend_XmlRpc_Client
  52. */
  53. protected $xmlrpcClient;
  54. /**
  55. * Runs the test methods of this class.
  56. *
  57. * @return void
  58. */
  59. public static function main()
  60. {
  61. $suite = new PHPUnit_Framework_TestSuite("Zend_XmlRpc_ClientTest");
  62. $result = PHPUnit_TextUI_TestRunner::run($suite);
  63. }
  64. public function setUp()
  65. {
  66. $this->httpAdapter = new Zend_Http_Client_Adapter_Test();
  67. $this->httpClient = new Zend_Http_Client('http://foo',
  68. array('adapter' => $this->httpAdapter));
  69. $this->xmlrpcClient = new Zend_XmlRpc_Client('http://foo');
  70. $this->xmlrpcClient->setHttpClient($this->httpClient);
  71. }
  72. // HTTP Client
  73. public function testGettingDefaultHttpClient()
  74. {
  75. $xmlrpcClient = new Zend_XmlRpc_Client('http://foo');
  76. $httpClient = $xmlrpcClient->getHttpClient();
  77. $this->assertType('Zend_Http_Client', $httpClient);
  78. $this->assertSame($httpClient, $xmlrpcClient->getHttpClient());
  79. }
  80. public function testSettingAndGettingHttpClient()
  81. {
  82. $xmlrpcClient = new Zend_XmlRpc_Client('http://foo');
  83. $httpClient = new Zend_Http_Client('http://foo');
  84. $this->assertNotSame($httpClient, $xmlrpcClient->getHttpClient());
  85. $xmlrpcClient->setHttpClient($httpClient);
  86. $this->assertSame($httpClient, $xmlrpcClient->getHttpClient());
  87. }
  88. public function testSettingHttpClientViaContructor()
  89. {
  90. $xmlrpcClient = new Zend_XmlRpc_Client('http://foo', $this->httpClient);
  91. $httpClient = $xmlrpcClient->getHttpClient();
  92. $this->assertSame($this->httpClient, $httpClient);
  93. }
  94. // Request & Response
  95. public function testLastRequestAndResponseAreInitiallyNull()
  96. {
  97. $this->assertNull($this->xmlrpcClient->getLastRequest());
  98. $this->assertNull($this->xmlrpcClient->getLastResponse());
  99. }
  100. public function testLastRequestAndResponseAreSetAfterRpcMethodCall()
  101. {
  102. $this->setServerResponseTo(true);
  103. $this->xmlrpcClient->call('foo');
  104. $this->assertType('Zend_XmlRpc_Request', $this->xmlrpcClient->getLastRequest());
  105. $this->assertType('Zend_XmlRpc_Response', $this->xmlrpcClient->getLastResponse());
  106. }
  107. public function testSuccessfulRpcMethodCallWithNoParameters()
  108. {
  109. $expectedMethod = 'foo.bar';
  110. $expectedReturn = 7;
  111. $this->setServerResponseTo($expectedReturn);
  112. $this->assertSame($expectedReturn, $this->xmlrpcClient->call($expectedMethod));
  113. $request = $this->xmlrpcClient->getLastRequest();
  114. $response = $this->xmlrpcClient->getLastResponse();
  115. $this->assertSame($expectedMethod, $request->getMethod());
  116. $this->assertSame(array(), $request->getParams());
  117. $this->assertSame($expectedReturn, $response->getReturnValue());
  118. $this->assertFalse($response->isFault());
  119. }
  120. public function testSuccessfulRpcMethodCallWithParameters()
  121. {
  122. $expectedMethod = 'foo.bar';
  123. $expectedParams = array(1, 'foo' => 'bar', 1.1, true);
  124. $expectedReturn = array(7, false, 'foo' => 'bar');
  125. $this->setServerResponseTo($expectedReturn);
  126. $actualReturn = $this->xmlrpcClient->call($expectedMethod, $expectedParams);
  127. $this->assertSame($expectedReturn, $actualReturn);
  128. $request = $this->xmlrpcClient->getLastRequest();
  129. $response = $this->xmlrpcClient->getLastResponse();
  130. $this->assertSame($expectedMethod, $request->getMethod());
  131. $params = $request->getParams();
  132. $this->assertSame(count($expectedParams), count($params));
  133. $this->assertSame($expectedParams[0], $params[0]->getValue());
  134. $this->assertSame($expectedParams[1], $params[1]->getValue());
  135. $this->assertSame($expectedParams[2], $params[2]->getValue());
  136. $this->assertSame($expectedParams['foo'], $params['foo']->getValue());
  137. $this->assertSame($expectedReturn, $response->getReturnValue());
  138. $this->assertFalse($response->isFault());
  139. }
  140. /**
  141. * @see ZF-2090
  142. */
  143. public function testSuccessfullyDetectsEmptyArrayParameterAsArray()
  144. {
  145. $expectedMethod = 'foo.bar';
  146. $expectedParams = array(array());
  147. $expectedReturn = array(true);
  148. $this->setServerResponseTo($expectedReturn);
  149. $actualReturn = $this->xmlrpcClient->call($expectedMethod, $expectedParams);
  150. $this->assertSame($expectedReturn, $actualReturn);
  151. $request = $this->xmlrpcClient->getLastRequest();
  152. $params = $request->getParams();
  153. $this->assertSame(count($expectedParams), count($params));
  154. $this->assertSame($expectedParams[0], $params[0]->getValue());
  155. }
  156. /**
  157. * @see ZF-1412
  158. *
  159. * @return void
  160. */
  161. public function testSuccessfulRpcMethodCallWithMixedDateParameters()
  162. {
  163. $time = time();
  164. $expectedMethod = 'foo.bar';
  165. $expectedParams = array(
  166. 'username',
  167. new Zend_XmlRpc_Value_DateTime($time)
  168. );
  169. $expectedReturn = array('username', $time);
  170. $this->setServerResponseTo($expectedReturn);
  171. $actualReturn = $this->xmlrpcClient->call($expectedMethod, $expectedParams);
  172. $this->assertSame($expectedReturn, $actualReturn);
  173. $request = $this->xmlrpcClient->getLastRequest();
  174. $response = $this->xmlrpcClient->getLastResponse();
  175. $this->assertSame($expectedMethod, $request->getMethod());
  176. $params = $request->getParams();
  177. $this->assertSame(count($expectedParams), count($params));
  178. $this->assertSame($expectedParams[0], $params[0]->getValue());
  179. $this->assertSame($expectedParams[1], $params[1]);
  180. $this->assertSame($expectedReturn, $response->getReturnValue());
  181. $this->assertFalse($response->isFault());
  182. }
  183. /**
  184. * @see ZF-1797
  185. */
  186. public function testSuccesfulRpcMethodCallWithXmlRpcValueParameters()
  187. {
  188. $time = time();
  189. $params = array(
  190. new Zend_XmlRpc_Value_Boolean(true),
  191. new Zend_XmlRpc_Value_Integer(4),
  192. new Zend_XmlRpc_Value_String('foo')
  193. );
  194. $expect = array(true, 4, 'foo');
  195. $this->setServerResponseTo($expect);
  196. $result = $this->xmlrpcClient->call('foo.bar', $params);
  197. $this->assertSame($expect, $result);
  198. $request = $this->xmlrpcClient->getLastRequest();
  199. $response = $this->xmlrpcClient->getLastResponse();
  200. $this->assertSame('foo.bar', $request->getMethod());
  201. $this->assertSame($params, $request->getParams());
  202. $this->assertSame($expect, $response->getReturnValue());
  203. $this->assertFalse($response->isFault());
  204. }
  205. /**
  206. * @see ZF-2978
  207. */
  208. public function testSkippingSystemCallDisabledByDefault()
  209. {
  210. $this->assertFalse($this->xmlrpcClient->skipSystemLookup());
  211. }
  212. /**
  213. * @see ZF-6993
  214. */
  215. public function testWhenPassingAStringAndAnIntegerIsExpectedParamIsConverted()
  216. {
  217. $this->mockIntrospector();
  218. $this->mockedIntrospector->expects($this->exactly(2))
  219. ->method('getMethodSignature')
  220. ->with('test.method')
  221. ->will($this->returnValue(array(array('parameters' => array('int')))));
  222. $expect = 'test.method response';
  223. $this->setServerResponseTo($expect);
  224. $this->assertSame($expect, $this->xmlrpcClient->call('test.method', array('1')));
  225. $params = $this->xmlrpcClient->getLastRequest()->getParams();
  226. $this->assertSame(1, $params[0]->getValue());
  227. $this->setServerResponseTo($expect);
  228. $this->assertSame($expect, $this->xmlrpcClient->call('test.method', '1'));
  229. $params = $this->xmlrpcClient->getLastRequest()->getParams();
  230. $this->assertSame(1, $params[0]->getValue());
  231. }
  232. public function testAllowsSkippingSystemCallForArrayStructLookup()
  233. {
  234. $this->xmlrpcClient->setSkipSystemLookup(true);
  235. $this->assertTrue($this->xmlrpcClient->skipSystemLookup());
  236. }
  237. public function testSkipsSystemCallWhenDirected()
  238. {
  239. $this->markTestIncomplete('Cannot complete this test until we add logging of requests sent to HTTP test client');
  240. }
  241. /**#@-*/
  242. // Faults
  243. public function testRpcMethodCallThrowsOnHttpFailure()
  244. {
  245. $status = 404;
  246. $message = 'Not Found';
  247. $body = 'oops';
  248. $response = $this->makeHttpResponseFrom($body, $status, $message);
  249. $this->httpAdapter->setResponse($response);
  250. try {
  251. $this->xmlrpcClient->call('foo');
  252. $this->fail();
  253. } catch (Exception $e) {
  254. $this->assertType('Zend_XmlRpc_Client_HttpException', $e);
  255. $this->assertEquals($message, $e->getMessage());
  256. $this->assertEquals($status, $e->getCode());
  257. }
  258. }
  259. public function testRpcMethodCallThrowsOnXmlRpcFault()
  260. {
  261. $code = 9;
  262. $message = 'foo';
  263. $fault = new Zend_XmlRpc_Fault($code, $message);
  264. $xml = $fault->saveXML();
  265. $response = $this->makeHttpResponseFrom($xml);
  266. $this->httpAdapter->setResponse($response);
  267. try {
  268. $this->xmlrpcClient->call('foo');
  269. $this->fail();
  270. } catch (Exception $e) {
  271. $this->assertType('Zend_XmlRpc_Client_FaultException', $e);
  272. $this->assertEquals($message, $e->getMessage());
  273. $this->assertEquals($code, $e->getCode());
  274. }
  275. }
  276. // Server Proxy
  277. public function testGetProxyReturnsServerProxy()
  278. {
  279. $class = 'Zend_XmlRpc_Client_ServerProxy';
  280. $this->assertType($class, $this->xmlrpcClient->getProxy());
  281. }
  282. public function testRpcMethodCallsThroughServerProxy()
  283. {
  284. $expectedReturn = array(7, false, 'foo' => 'bar');
  285. $this->setServerResponseTo($expectedReturn);
  286. $server = $this->xmlrpcClient->getProxy();
  287. $this->assertSame($expectedReturn, $server->listMethods());
  288. $request = $this->xmlrpcClient->getLastRequest();
  289. $this->assertEquals('listMethods', $request->getMethod());
  290. }
  291. public function testRpcMethodCallsThroughNestedServerProxies()
  292. {
  293. $expectedReturn = array(7, false, 'foo' => 'bar');
  294. $this->setServerResponseTo($expectedReturn);
  295. $server = $this->xmlrpcClient->getProxy('foo');
  296. $this->assertSame($expectedReturn, $server->bar->baz->boo());
  297. $request = $this->xmlrpcClient->getLastRequest();
  298. $this->assertEquals('foo.bar.baz.boo', $request->getMethod());
  299. }
  300. public function testClientCachesServerProxies()
  301. {
  302. $proxy = $this->xmlrpcClient->getProxy();
  303. $this->assertSame($proxy, $this->xmlrpcClient->getProxy());
  304. $proxy = $this->xmlrpcClient->getProxy('foo');
  305. $this->assertSame($proxy, $this->xmlrpcClient->getProxy('foo'));
  306. }
  307. public function testServerProxyCachesNestedProxies()
  308. {
  309. $proxy = $this->xmlrpcClient->getProxy();
  310. $foo = $proxy->foo;
  311. $this->assertSame($foo, $proxy->foo);
  312. $bar = $proxy->foo->bar;
  313. $this->assertSame($bar, $proxy->foo->bar);
  314. }
  315. // Introspection
  316. public function testGettingDefaultIntrospector()
  317. {
  318. $xmlrpcClient = new Zend_XmlRpc_Client('http://foo');
  319. $introspector = $xmlrpcClient->getIntrospector();
  320. $this->assertType('Zend_XmlRpc_Client_ServerIntrospection', $introspector);
  321. $this->assertSame($introspector, $xmlrpcClient->getIntrospector());
  322. }
  323. public function testSettingAndGettingIntrospector()
  324. {
  325. $xmlrpcClient = new Zend_XmlRpc_Client('http://foo');
  326. $introspector = new Zend_XmlRpc_Client_ServerIntrospection($xmlrpcClient);
  327. $this->assertNotSame($introspector, $xmlrpcClient->getIntrospector());
  328. $xmlrpcClient->setIntrospector($introspector);
  329. $this->assertSame($introspector, $xmlrpcClient->getIntrospector());
  330. }
  331. public function testGettingMethodSignature()
  332. {
  333. $method = 'foo';
  334. $signatures = array(array('int', 'int', 'int'));
  335. $this->setServerResponseTo($signatures);
  336. $i = $this->xmlrpcClient->getIntrospector();
  337. $this->assertEquals($signatures, $i->getMethodSignature($method));
  338. $request = $this->xmlrpcClient->getLastRequest();
  339. $this->assertEquals('system.methodSignature', $request->getMethod());
  340. $this->assertEquals(array($method), $request->getParams());
  341. }
  342. public function testListingMethods()
  343. {
  344. $methods = array('foo', 'bar', 'baz');
  345. $this->setServerResponseTo($methods);
  346. $i = $this->xmlrpcClient->getIntrospector();
  347. $this->assertEquals($methods, $i->listMethods());
  348. $request = $this->xmlrpcClient->getLastRequest();
  349. $this->assertEquals('system.listMethods', $request->getMethod());
  350. $this->assertEquals(array(), $request->getParams());
  351. }
  352. public function testGettingAllMethodSignaturesByLooping()
  353. {
  354. // system.listMethods() will return ['foo', 'bar']
  355. $methods = array('foo', 'bar');
  356. $response = $this->getServerResponseFor($methods);
  357. $this->httpAdapter->setResponse($response);
  358. // system.methodSignature('foo') will return [['int'], ['int', 'string']]
  359. $fooSignatures = array(array('int'), array('int', 'string'));
  360. $response = $this->getServerResponseFor($fooSignatures);
  361. $this->httpAdapter->addResponse($response);
  362. // system.methodSignature('bar') will return [['boolean']]
  363. $barSignatures = array(array('boolean'));
  364. $response = $this->getServerResponseFor($barSignatures);
  365. $this->httpAdapter->addResponse($response);
  366. $expected = array('foo' => $fooSignatures,
  367. 'bar' => $barSignatures);
  368. $i = $this->xmlrpcClient->getIntrospector();
  369. $this->assertEquals($expected, $i->getSignatureForEachMethodByLooping());
  370. $request = $this->xmlrpcClient->getLastRequest();
  371. $this->assertEquals('system.methodSignature', $request->getMethod());
  372. $this->assertEquals(array('bar'), $request->getParams());
  373. }
  374. public function testGettingAllMethodSignaturesByMulticall()
  375. {
  376. // system.listMethods() will return ['foo', 'bar']
  377. $whatListMethodsReturns = array('foo', 'bar');
  378. $response = $this->getServerResponseFor($whatListMethodsReturns);
  379. $this->httpAdapter->setResponse($response);
  380. // after system.listMethods(), these system.multicall() params are expected
  381. $multicallParams = array(array('methodName' => 'system.methodSignature',
  382. 'params' => array('foo')),
  383. array('methodName' => 'system.methodSignature',
  384. 'params' => array('bar')));
  385. // system.multicall() will then return [fooSignatures, barSignatures]
  386. $fooSignatures = array(array('int'), array('int', 'string'));
  387. $barSignatures = array(array('boolean'));
  388. $whatMulticallReturns = array($fooSignatures, $barSignatures);
  389. $response = $this->getServerResponseFor($whatMulticallReturns);
  390. $this->httpAdapter->addResponse($response);
  391. $i = $this->xmlrpcClient->getIntrospector();
  392. $expected = array('foo' => $fooSignatures,
  393. 'bar' => $barSignatures);
  394. $this->assertEquals($expected, $i->getSignatureForEachMethodByMulticall());
  395. $request = $this->xmlrpcClient->getLastRequest();
  396. $this->assertEquals('system.multicall', $request->getMethod());
  397. $this->assertEquals(array($multicallParams), $request->getParams());
  398. }
  399. public function testGettingAllMethodSignaturesByMulticallThrowsOnBadCount()
  400. {
  401. // system.listMethods() will return ['foo', 'bar']
  402. $whatListMethodsReturns = array('foo', 'bar');
  403. $response = $this->getServerResponseFor($whatListMethodsReturns);
  404. $this->httpAdapter->setResponse($response);
  405. // system.multicall() will then return only [fooSignatures]
  406. $fooSignatures = array(array('int'), array('int', 'string'));
  407. $whatMulticallReturns = array($fooSignatures); // error! no bar signatures!
  408. $response = $this->getServerResponseFor($whatMulticallReturns);
  409. $this->httpAdapter->addResponse($response);
  410. $i = $this->xmlrpcClient->getIntrospector();
  411. try {
  412. $i->getSignatureForEachMethodByMulticall();
  413. } catch (Zend_XmlRpc_Client_IntrospectException $e) {
  414. $this->assertRegexp('/bad number/i', $e->getMessage());
  415. }
  416. }
  417. public function testGettingAllMethodSignaturesByMulticallThrowsOnBadType()
  418. {
  419. // system.listMethods() will return ['foo', 'bar']
  420. $whatListMethodsReturns = array('foo', 'bar');
  421. $response = $this->getServerResponseFor($whatListMethodsReturns);
  422. $this->httpAdapter->setResponse($response);
  423. // system.multicall() will then return only an int
  424. $whatMulticallReturns = 1; // error! no signatures?
  425. $response = $this->getServerResponseFor($whatMulticallReturns);
  426. $this->httpAdapter->addResponse($response);
  427. $i = $this->xmlrpcClient->getIntrospector();
  428. try {
  429. $i->getSignatureForEachMethodByMulticall();
  430. } catch (Zend_XmlRpc_Client_IntrospectException $e) {
  431. $this->assertRegexp('/got integer/i', $e->getMessage());
  432. }
  433. }
  434. public function testGettingAllMethodSignaturesDefaultsToMulticall()
  435. {
  436. // system.listMethods() will return ['foo', 'bar']
  437. $whatListMethodsReturns = array('foo', 'bar');
  438. $response = $this->getServerResponseFor($whatListMethodsReturns);
  439. $this->httpAdapter->setResponse($response);
  440. // system.multicall() will then return [fooSignatures, barSignatures]
  441. $fooSignatures = array(array('int'), array('int', 'string'));
  442. $barSignatures = array(array('boolean'));
  443. $whatMulticallReturns = array($fooSignatures, $barSignatures);
  444. $response = $this->getServerResponseFor($whatMulticallReturns);
  445. $this->httpAdapter->addResponse($response);
  446. $i = $this->xmlrpcClient->getIntrospector();
  447. $expected = array('foo' => $fooSignatures,
  448. 'bar' => $barSignatures);
  449. $this->assertEquals($expected, $i->getSignatureForEachMethod());
  450. $request = $this->xmlrpcClient->getLastRequest();
  451. $this->assertEquals('system.multicall', $request->getMethod());
  452. }
  453. public function testGettingAllMethodSignaturesDegradesToLooping()
  454. {
  455. // system.listMethods() will return ['foo', 'bar']
  456. $whatListMethodsReturns = array('foo', 'bar');
  457. $response = $this->getServerResponseFor($whatListMethodsReturns);
  458. $this->httpAdapter->setResponse($response);
  459. // system.multicall() will return a fault
  460. $fault = new Zend_XmlRpc_Fault(7, 'bad method');
  461. $xml = $fault->saveXML();
  462. $response = $this->makeHttpResponseFrom($xml);
  463. $this->httpAdapter->addResponse($response);
  464. // system.methodSignature('foo') will return [['int'], ['int', 'string']]
  465. $fooSignatures = array(array('int'), array('int', 'string'));
  466. $response = $this->getServerResponseFor($fooSignatures);
  467. $this->httpAdapter->addResponse($response);
  468. // system.methodSignature('bar') will return [['boolean']]
  469. $barSignatures = array(array('boolean'));
  470. $response = $this->getServerResponseFor($barSignatures);
  471. $this->httpAdapter->addResponse($response);
  472. $i = $this->xmlrpcClient->getIntrospector();
  473. $expected = array('foo' => $fooSignatures,
  474. 'bar' => $barSignatures);
  475. $this->assertEquals($expected, $i->getSignatureForEachMethod());
  476. $request = $this->xmlrpcClient->getLastRequest();
  477. $this->assertEquals('system.methodSignature', $request->getMethod());
  478. }
  479. /**
  480. * @group ZF-4372
  481. */
  482. public function testSettingUriOnHttpClientIsNotOverwrittenByXmlRpcClient()
  483. {
  484. $changedUri = "http://bar:80";
  485. // Overwrite: http://foo:80
  486. $this->setServerResponseTo(array());
  487. $this->xmlrpcClient->getHttpClient()->setUri($changedUri);
  488. $this->xmlrpcClient->call("foo");
  489. $uri = $this->xmlrpcClient->getHttpClient()->getUri(true);
  490. $this->assertEquals($changedUri, $uri);
  491. }
  492. /**
  493. * @group ZF-4372
  494. */
  495. public function testSettingNoHttpClientUriForcesClientToSetUri()
  496. {
  497. $baseUri = "http://foo:80";
  498. $this->httpAdapter = new Zend_Http_Client_Adapter_Test();
  499. $this->httpClient = new Zend_Http_Client(null, array('adapter' => $this->httpAdapter));
  500. $this->xmlrpcClient = new Zend_XmlRpc_Client($baseUri);
  501. $this->xmlrpcClient->setHttpClient($this->httpClient);
  502. $this->setServerResponseTo(array());
  503. $this->assertNull($this->xmlrpcClient->getHttpClient()->getUri());
  504. $this->xmlrpcClient->call("foo");
  505. $uri = $this->xmlrpcClient->getHttpClient()->getUri(true);
  506. $this->assertEquals($baseUri, $uri);
  507. }
  508. /**
  509. * @group ZF-3288
  510. */
  511. public function testCustomHttpClientUserAgentIsNotOverridden()
  512. {
  513. $this->assertNull(
  514. $this->httpClient->getHeader('user-agent'),
  515. 'UA is null if no request was made'
  516. );
  517. $this->setServerResponseTo(true);
  518. $this->assertTrue($this->xmlrpcClient->call('method'));
  519. $this->assertSame(
  520. 'Zend_XmlRpc_Client',
  521. $this->httpClient->getHeader('user-agent'),
  522. 'If no custom UA is set, set Zend_XmlRpc_Client'
  523. );
  524. $expectedUserAgent = 'Zend_XmlRpc_Client (custom)';
  525. $this->httpClient->setHeaders(array('user-agent' => $expectedUserAgent));
  526. $this->setServerResponseTo(true);
  527. $this->assertTrue($this->xmlrpcClient->call('method'));
  528. $this->assertSame($expectedUserAgent, $this->httpClient->getHeader('user-agent'));
  529. }
  530. // Helpers
  531. public function setServerResponseTo($nativeVars)
  532. {
  533. $response = $this->getServerResponseFor($nativeVars);
  534. $this->httpAdapter->setResponse($response);
  535. }
  536. public function getServerResponseFor($nativeVars)
  537. {
  538. $response = new Zend_XmlRpc_Response();
  539. $response->setReturnValue($nativeVars);
  540. $xml = $response->saveXML();
  541. $response = $this->makeHttpResponseFrom($xml);
  542. return $response;
  543. }
  544. public function makeHttpResponseFrom($data, $status=200, $message='OK')
  545. {
  546. $headers = array("HTTP/1.1 $status $message",
  547. "Status: $status",
  548. 'Content_Type: text/xml; charset=utf-8',
  549. 'Content-Length: ' . strlen($data)
  550. );
  551. return implode("\r\n", $headers) . "\r\n\r\n$data\r\n\r\n";
  552. }
  553. public function mockIntrospector()
  554. {
  555. $this->mockedIntrospector = $this->getMock(
  556. 'Zend_XmlRpc_Client_ServerIntrospection',
  557. array(),
  558. array(),
  559. '',
  560. false,
  561. false
  562. );
  563. $this->xmlrpcClient->setIntrospector($this->mockedIntrospector);
  564. }
  565. }
  566. // Call Zend_XmlRpc_ClientTest::main() if this source file is executed directly.
  567. if (PHPUnit_MAIN_METHOD == "Zend_XmlRpc_ClientTest::main") {
  568. Zend_XmlRpc_ClientTest::main();
  569. }