ClientTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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->mockHttpClient();
  240. $this->mockedHttpClient->expects($this->once())
  241. ->method('request')
  242. ->with('POST')
  243. ->will($this->returnValue($this->makeHttpResponseFor('foo')));
  244. $this->xmlrpcClient->setHttpClient($this->mockedHttpClient);
  245. $this->xmlrpcClient->setSkipSystemLookup(true);
  246. $this->assertSame('foo', $this->xmlrpcClient->call('test.method'));
  247. }
  248. /**#@-*/
  249. // Faults
  250. public function testRpcMethodCallThrowsOnHttpFailure()
  251. {
  252. $status = 404;
  253. $message = 'Not Found';
  254. $body = 'oops';
  255. $response = $this->makeHttpResponseFrom($body, $status, $message);
  256. $this->httpAdapter->setResponse($response);
  257. try {
  258. $this->xmlrpcClient->call('foo');
  259. $this->fail();
  260. } catch (Exception $e) {
  261. $this->assertType('Zend_XmlRpc_Client_HttpException', $e);
  262. $this->assertEquals($message, $e->getMessage());
  263. $this->assertEquals($status, $e->getCode());
  264. }
  265. }
  266. public function testRpcMethodCallThrowsOnXmlRpcFault()
  267. {
  268. $code = 9;
  269. $message = 'foo';
  270. $fault = new Zend_XmlRpc_Fault($code, $message);
  271. $xml = $fault->saveXML();
  272. $response = $this->makeHttpResponseFrom($xml);
  273. $this->httpAdapter->setResponse($response);
  274. try {
  275. $this->xmlrpcClient->call('foo');
  276. $this->fail();
  277. } catch (Exception $e) {
  278. $this->assertType('Zend_XmlRpc_Client_FaultException', $e);
  279. $this->assertEquals($message, $e->getMessage());
  280. $this->assertEquals($code, $e->getCode());
  281. }
  282. }
  283. // Server Proxy
  284. public function testGetProxyReturnsServerProxy()
  285. {
  286. $class = 'Zend_XmlRpc_Client_ServerProxy';
  287. $this->assertType($class, $this->xmlrpcClient->getProxy());
  288. }
  289. public function testRpcMethodCallsThroughServerProxy()
  290. {
  291. $expectedReturn = array(7, false, 'foo' => 'bar');
  292. $this->setServerResponseTo($expectedReturn);
  293. $server = $this->xmlrpcClient->getProxy();
  294. $this->assertSame($expectedReturn, $server->listMethods());
  295. $request = $this->xmlrpcClient->getLastRequest();
  296. $this->assertEquals('listMethods', $request->getMethod());
  297. }
  298. public function testRpcMethodCallsThroughNestedServerProxies()
  299. {
  300. $expectedReturn = array(7, false, 'foo' => 'bar');
  301. $this->setServerResponseTo($expectedReturn);
  302. $server = $this->xmlrpcClient->getProxy('foo');
  303. $this->assertSame($expectedReturn, $server->bar->baz->boo());
  304. $request = $this->xmlrpcClient->getLastRequest();
  305. $this->assertEquals('foo.bar.baz.boo', $request->getMethod());
  306. }
  307. public function testClientCachesServerProxies()
  308. {
  309. $proxy = $this->xmlrpcClient->getProxy();
  310. $this->assertSame($proxy, $this->xmlrpcClient->getProxy());
  311. $proxy = $this->xmlrpcClient->getProxy('foo');
  312. $this->assertSame($proxy, $this->xmlrpcClient->getProxy('foo'));
  313. }
  314. public function testServerProxyCachesNestedProxies()
  315. {
  316. $proxy = $this->xmlrpcClient->getProxy();
  317. $foo = $proxy->foo;
  318. $this->assertSame($foo, $proxy->foo);
  319. $bar = $proxy->foo->bar;
  320. $this->assertSame($bar, $proxy->foo->bar);
  321. }
  322. // Introspection
  323. public function testGettingDefaultIntrospector()
  324. {
  325. $xmlrpcClient = new Zend_XmlRpc_Client('http://foo');
  326. $introspector = $xmlrpcClient->getIntrospector();
  327. $this->assertType('Zend_XmlRpc_Client_ServerIntrospection', $introspector);
  328. $this->assertSame($introspector, $xmlrpcClient->getIntrospector());
  329. }
  330. public function testSettingAndGettingIntrospector()
  331. {
  332. $xmlrpcClient = new Zend_XmlRpc_Client('http://foo');
  333. $introspector = new Zend_XmlRpc_Client_ServerIntrospection($xmlrpcClient);
  334. $this->assertNotSame($introspector, $xmlrpcClient->getIntrospector());
  335. $xmlrpcClient->setIntrospector($introspector);
  336. $this->assertSame($introspector, $xmlrpcClient->getIntrospector());
  337. }
  338. public function testGettingMethodSignature()
  339. {
  340. $method = 'foo';
  341. $signatures = array(array('int', 'int', 'int'));
  342. $this->setServerResponseTo($signatures);
  343. $i = $this->xmlrpcClient->getIntrospector();
  344. $this->assertEquals($signatures, $i->getMethodSignature($method));
  345. $request = $this->xmlrpcClient->getLastRequest();
  346. $this->assertEquals('system.methodSignature', $request->getMethod());
  347. $this->assertEquals(array($method), $request->getParams());
  348. }
  349. public function testListingMethods()
  350. {
  351. $methods = array('foo', 'bar', 'baz');
  352. $this->setServerResponseTo($methods);
  353. $i = $this->xmlrpcClient->getIntrospector();
  354. $this->assertEquals($methods, $i->listMethods());
  355. $request = $this->xmlrpcClient->getLastRequest();
  356. $this->assertEquals('system.listMethods', $request->getMethod());
  357. $this->assertEquals(array(), $request->getParams());
  358. }
  359. public function testGettingAllMethodSignaturesByLooping()
  360. {
  361. // system.listMethods() will return ['foo', 'bar']
  362. $methods = array('foo', 'bar');
  363. $response = $this->getServerResponseFor($methods);
  364. $this->httpAdapter->setResponse($response);
  365. // system.methodSignature('foo') will return [['int'], ['int', 'string']]
  366. $fooSignatures = array(array('int'), array('int', 'string'));
  367. $response = $this->getServerResponseFor($fooSignatures);
  368. $this->httpAdapter->addResponse($response);
  369. // system.methodSignature('bar') will return [['boolean']]
  370. $barSignatures = array(array('boolean'));
  371. $response = $this->getServerResponseFor($barSignatures);
  372. $this->httpAdapter->addResponse($response);
  373. $expected = array('foo' => $fooSignatures,
  374. 'bar' => $barSignatures);
  375. $i = $this->xmlrpcClient->getIntrospector();
  376. $this->assertEquals($expected, $i->getSignatureForEachMethodByLooping());
  377. $request = $this->xmlrpcClient->getLastRequest();
  378. $this->assertEquals('system.methodSignature', $request->getMethod());
  379. $this->assertEquals(array('bar'), $request->getParams());
  380. }
  381. public function testGettingAllMethodSignaturesByMulticall()
  382. {
  383. // system.listMethods() will return ['foo', 'bar']
  384. $whatListMethodsReturns = array('foo', 'bar');
  385. $response = $this->getServerResponseFor($whatListMethodsReturns);
  386. $this->httpAdapter->setResponse($response);
  387. // after system.listMethods(), these system.multicall() params are expected
  388. $multicallParams = array(array('methodName' => 'system.methodSignature',
  389. 'params' => array('foo')),
  390. array('methodName' => 'system.methodSignature',
  391. 'params' => array('bar')));
  392. // system.multicall() will then return [fooSignatures, barSignatures]
  393. $fooSignatures = array(array('int'), array('int', 'string'));
  394. $barSignatures = array(array('boolean'));
  395. $whatMulticallReturns = array($fooSignatures, $barSignatures);
  396. $response = $this->getServerResponseFor($whatMulticallReturns);
  397. $this->httpAdapter->addResponse($response);
  398. $i = $this->xmlrpcClient->getIntrospector();
  399. $expected = array('foo' => $fooSignatures,
  400. 'bar' => $barSignatures);
  401. $this->assertEquals($expected, $i->getSignatureForEachMethodByMulticall());
  402. $request = $this->xmlrpcClient->getLastRequest();
  403. $this->assertEquals('system.multicall', $request->getMethod());
  404. $this->assertEquals(array($multicallParams), $request->getParams());
  405. }
  406. public function testGettingAllMethodSignaturesByMulticallThrowsOnBadCount()
  407. {
  408. // system.listMethods() will return ['foo', 'bar']
  409. $whatListMethodsReturns = array('foo', 'bar');
  410. $response = $this->getServerResponseFor($whatListMethodsReturns);
  411. $this->httpAdapter->setResponse($response);
  412. // system.multicall() will then return only [fooSignatures]
  413. $fooSignatures = array(array('int'), array('int', 'string'));
  414. $whatMulticallReturns = array($fooSignatures); // error! no bar signatures!
  415. $response = $this->getServerResponseFor($whatMulticallReturns);
  416. $this->httpAdapter->addResponse($response);
  417. $i = $this->xmlrpcClient->getIntrospector();
  418. try {
  419. $i->getSignatureForEachMethodByMulticall();
  420. } catch (Zend_XmlRpc_Client_IntrospectException $e) {
  421. $this->assertRegexp('/bad number/i', $e->getMessage());
  422. }
  423. }
  424. public function testGettingAllMethodSignaturesByMulticallThrowsOnBadType()
  425. {
  426. // system.listMethods() will return ['foo', 'bar']
  427. $whatListMethodsReturns = array('foo', 'bar');
  428. $response = $this->getServerResponseFor($whatListMethodsReturns);
  429. $this->httpAdapter->setResponse($response);
  430. // system.multicall() will then return only an int
  431. $whatMulticallReturns = 1; // error! no signatures?
  432. $response = $this->getServerResponseFor($whatMulticallReturns);
  433. $this->httpAdapter->addResponse($response);
  434. $i = $this->xmlrpcClient->getIntrospector();
  435. try {
  436. $i->getSignatureForEachMethodByMulticall();
  437. } catch (Zend_XmlRpc_Client_IntrospectException $e) {
  438. $this->assertRegexp('/got integer/i', $e->getMessage());
  439. }
  440. }
  441. public function testGettingAllMethodSignaturesDefaultsToMulticall()
  442. {
  443. // system.listMethods() will return ['foo', 'bar']
  444. $whatListMethodsReturns = array('foo', 'bar');
  445. $response = $this->getServerResponseFor($whatListMethodsReturns);
  446. $this->httpAdapter->setResponse($response);
  447. // system.multicall() will then return [fooSignatures, barSignatures]
  448. $fooSignatures = array(array('int'), array('int', 'string'));
  449. $barSignatures = array(array('boolean'));
  450. $whatMulticallReturns = array($fooSignatures, $barSignatures);
  451. $response = $this->getServerResponseFor($whatMulticallReturns);
  452. $this->httpAdapter->addResponse($response);
  453. $i = $this->xmlrpcClient->getIntrospector();
  454. $expected = array('foo' => $fooSignatures,
  455. 'bar' => $barSignatures);
  456. $this->assertEquals($expected, $i->getSignatureForEachMethod());
  457. $request = $this->xmlrpcClient->getLastRequest();
  458. $this->assertEquals('system.multicall', $request->getMethod());
  459. }
  460. public function testGettingAllMethodSignaturesDegradesToLooping()
  461. {
  462. // system.listMethods() will return ['foo', 'bar']
  463. $whatListMethodsReturns = array('foo', 'bar');
  464. $response = $this->getServerResponseFor($whatListMethodsReturns);
  465. $this->httpAdapter->setResponse($response);
  466. // system.multicall() will return a fault
  467. $fault = new Zend_XmlRpc_Fault(7, 'bad method');
  468. $xml = $fault->saveXML();
  469. $response = $this->makeHttpResponseFrom($xml);
  470. $this->httpAdapter->addResponse($response);
  471. // system.methodSignature('foo') will return [['int'], ['int', 'string']]
  472. $fooSignatures = array(array('int'), array('int', 'string'));
  473. $response = $this->getServerResponseFor($fooSignatures);
  474. $this->httpAdapter->addResponse($response);
  475. // system.methodSignature('bar') will return [['boolean']]
  476. $barSignatures = array(array('boolean'));
  477. $response = $this->getServerResponseFor($barSignatures);
  478. $this->httpAdapter->addResponse($response);
  479. $i = $this->xmlrpcClient->getIntrospector();
  480. $expected = array('foo' => $fooSignatures,
  481. 'bar' => $barSignatures);
  482. $this->assertEquals($expected, $i->getSignatureForEachMethod());
  483. $request = $this->xmlrpcClient->getLastRequest();
  484. $this->assertEquals('system.methodSignature', $request->getMethod());
  485. }
  486. /**
  487. * @group ZF-4372
  488. */
  489. public function testSettingUriOnHttpClientIsNotOverwrittenByXmlRpcClient()
  490. {
  491. $changedUri = "http://bar:80";
  492. // Overwrite: http://foo:80
  493. $this->setServerResponseTo(array());
  494. $this->xmlrpcClient->getHttpClient()->setUri($changedUri);
  495. $this->xmlrpcClient->call("foo");
  496. $uri = $this->xmlrpcClient->getHttpClient()->getUri(true);
  497. $this->assertEquals($changedUri, $uri);
  498. }
  499. /**
  500. * @group ZF-4372
  501. */
  502. public function testSettingNoHttpClientUriForcesClientToSetUri()
  503. {
  504. $baseUri = "http://foo:80";
  505. $this->httpAdapter = new Zend_Http_Client_Adapter_Test();
  506. $this->httpClient = new Zend_Http_Client(null, array('adapter' => $this->httpAdapter));
  507. $this->xmlrpcClient = new Zend_XmlRpc_Client($baseUri);
  508. $this->xmlrpcClient->setHttpClient($this->httpClient);
  509. $this->setServerResponseTo(array());
  510. $this->assertNull($this->xmlrpcClient->getHttpClient()->getUri());
  511. $this->xmlrpcClient->call("foo");
  512. $uri = $this->xmlrpcClient->getHttpClient()->getUri(true);
  513. $this->assertEquals($baseUri, $uri);
  514. }
  515. /**
  516. * @group ZF-3288
  517. */
  518. public function testCustomHttpClientUserAgentIsNotOverridden()
  519. {
  520. $this->assertNull(
  521. $this->httpClient->getHeader('user-agent'),
  522. 'UA is null if no request was made'
  523. );
  524. $this->setServerResponseTo(true);
  525. $this->assertTrue($this->xmlrpcClient->call('method'));
  526. $this->assertSame(
  527. 'Zend_XmlRpc_Client',
  528. $this->httpClient->getHeader('user-agent'),
  529. 'If no custom UA is set, set Zend_XmlRpc_Client'
  530. );
  531. $expectedUserAgent = 'Zend_XmlRpc_Client (custom)';
  532. $this->httpClient->setHeaders(array('user-agent' => $expectedUserAgent));
  533. $this->setServerResponseTo(true);
  534. $this->assertTrue($this->xmlrpcClient->call('method'));
  535. $this->assertSame($expectedUserAgent, $this->httpClient->getHeader('user-agent'));
  536. }
  537. // Helpers
  538. public function setServerResponseTo($nativeVars)
  539. {
  540. $response = $this->getServerResponseFor($nativeVars);
  541. $this->httpAdapter->setResponse($response);
  542. }
  543. public function getServerResponseFor($nativeVars)
  544. {
  545. $response = new Zend_XmlRpc_Response();
  546. $response->setReturnValue($nativeVars);
  547. $xml = $response->saveXML();
  548. $response = $this->makeHttpResponseFrom($xml);
  549. return $response;
  550. }
  551. public function makeHttpResponseFrom($data, $status=200, $message='OK')
  552. {
  553. $headers = array("HTTP/1.1 $status $message",
  554. "Status: $status",
  555. 'Content_Type: text/xml; charset=utf-8',
  556. 'Content-Length: ' . strlen($data)
  557. );
  558. return implode("\r\n", $headers) . "\r\n\r\n$data\r\n\r\n";
  559. }
  560. public function makeHttpResponseFor($nativeVars)
  561. {
  562. $response = $this->getServerResponseFor($nativeVars);
  563. return Zend_Http_Response::fromString($response);
  564. }
  565. public function mockIntrospector()
  566. {
  567. $this->mockedIntrospector = $this->getMock(
  568. 'Zend_XmlRpc_Client_ServerIntrospection',
  569. array(),
  570. array(),
  571. '',
  572. false,
  573. false
  574. );
  575. $this->xmlrpcClient->setIntrospector($this->mockedIntrospector);
  576. }
  577. public function mockHttpClient()
  578. {
  579. $this->mockedHttpClient = $this->getMock('Zend_Http_Client');
  580. $this->xmlrpcClient->setHttpClient($this->mockedHttpClient);
  581. }
  582. }
  583. // Call Zend_XmlRpc_ClientTest::main() if this source file is executed directly.
  584. if (PHPUnit_MAIN_METHOD == "Zend_XmlRpc_ClientTest::main") {
  585. Zend_XmlRpc_ClientTest::main();
  586. }