ClientTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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-2015 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. require_once 'Zend/XmlRpc/Client.php';
  23. require_once 'Zend/XmlRpc/Response.php';
  24. require_once 'Zend/Http/Client/Adapter/Test.php';
  25. require_once 'Zend/XmlRpc/Value/DateTime.php';
  26. require_once 'Zend/XmlRpc/Client/ServerIntrospection.php';
  27. require_once 'Zend/Http/Client.php';
  28. /**
  29. * Test case for Zend_XmlRpc_Value
  30. *
  31. * @category Zend
  32. * @package Zend_XmlRpc
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_XmlRpc
  37. */
  38. class Zend_XmlRpc_ClientTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * @var Zend_Http_Client_Adapter_Abstract
  42. */
  43. protected $httpAdapter;
  44. /**
  45. * @var Zend_Http_Client
  46. */
  47. protected $httpClient;
  48. /**
  49. * @var Zend_XmlRpc_Client
  50. */
  51. protected $xmlrpcClient;
  52. public function setUp()
  53. {
  54. $this->httpAdapter = new Zend_Http_Client_Adapter_Test();
  55. $this->httpClient = new Zend_Http_Client('http://foo',
  56. array('adapter' => $this->httpAdapter));
  57. $this->xmlrpcClient = new Zend_XmlRpc_Client('http://foo');
  58. $this->xmlrpcClient->setHttpClient($this->httpClient);
  59. }
  60. // HTTP Client
  61. public function testGettingDefaultHttpClient()
  62. {
  63. $xmlrpcClient = new Zend_XmlRpc_Client('http://foo');
  64. $httpClient = $xmlrpcClient->getHttpClient();
  65. $this->assertTrue($httpClient instanceof Zend_Http_Client);
  66. $this->assertSame($httpClient, $xmlrpcClient->getHttpClient());
  67. }
  68. public function testSettingAndGettingHttpClient()
  69. {
  70. $xmlrpcClient = new Zend_XmlRpc_Client('http://foo');
  71. $httpClient = new Zend_Http_Client('http://foo');
  72. $this->assertNotSame($httpClient, $xmlrpcClient->getHttpClient());
  73. $xmlrpcClient->setHttpClient($httpClient);
  74. $this->assertSame($httpClient, $xmlrpcClient->getHttpClient());
  75. }
  76. public function testSettingHttpClientViaContructor()
  77. {
  78. $xmlrpcClient = new Zend_XmlRpc_Client('http://foo', $this->httpClient);
  79. $httpClient = $xmlrpcClient->getHttpClient();
  80. $this->assertSame($this->httpClient, $httpClient);
  81. }
  82. // Request & Response
  83. public function testLastRequestAndResponseAreInitiallyNull()
  84. {
  85. $this->assertNull($this->xmlrpcClient->getLastRequest());
  86. $this->assertNull($this->xmlrpcClient->getLastResponse());
  87. }
  88. public function testLastRequestAndResponseAreSetAfterRpcMethodCall()
  89. {
  90. $this->setServerResponseTo(true);
  91. $this->xmlrpcClient->call('foo');
  92. $this->assertTrue($this->xmlrpcClient->getLastRequest() instanceof Zend_XmlRpc_Request);
  93. $this->assertTrue($this->xmlrpcClient->getLastResponse() instanceof Zend_XmlRpc_Response);
  94. }
  95. public function testSuccessfulRpcMethodCallWithNoParameters()
  96. {
  97. $expectedMethod = 'foo.bar';
  98. $expectedReturn = 7;
  99. $this->setServerResponseTo($expectedReturn);
  100. $this->assertSame($expectedReturn, $this->xmlrpcClient->call($expectedMethod));
  101. $request = $this->xmlrpcClient->getLastRequest();
  102. $response = $this->xmlrpcClient->getLastResponse();
  103. $this->assertSame($expectedMethod, $request->getMethod());
  104. $this->assertSame(array(), $request->getParams());
  105. $this->assertSame($expectedReturn, $response->getReturnValue());
  106. $this->assertFalse($response->isFault());
  107. }
  108. public function testSuccessfulRpcMethodCallWithParameters()
  109. {
  110. $expectedMethod = 'foo.bar';
  111. $expectedParams = array(1, 'foo' => 'bar', 1.1, true);
  112. $expectedReturn = array(7, false, 'foo' => 'bar');
  113. $this->setServerResponseTo($expectedReturn);
  114. $actualReturn = $this->xmlrpcClient->call($expectedMethod, $expectedParams);
  115. $this->assertSame($expectedReturn, $actualReturn);
  116. $request = $this->xmlrpcClient->getLastRequest();
  117. $response = $this->xmlrpcClient->getLastResponse();
  118. $this->assertSame($expectedMethod, $request->getMethod());
  119. $params = $request->getParams();
  120. $this->assertSame(count($expectedParams), count($params));
  121. $this->assertSame($expectedParams[0], $params[0]->getValue());
  122. $this->assertSame($expectedParams[1], $params[1]->getValue());
  123. $this->assertSame($expectedParams[2], $params[2]->getValue());
  124. $this->assertSame($expectedParams['foo'], $params['foo']->getValue());
  125. $this->assertSame($expectedReturn, $response->getReturnValue());
  126. $this->assertFalse($response->isFault());
  127. }
  128. /**
  129. * @group ZF-2090
  130. */
  131. public function testSuccessfullyDetectsEmptyArrayParameterAsArray()
  132. {
  133. $expectedMethod = 'foo.bar';
  134. $expectedParams = array(array());
  135. $expectedReturn = array(true);
  136. $this->setServerResponseTo($expectedReturn);
  137. $actualReturn = $this->xmlrpcClient->call($expectedMethod, $expectedParams);
  138. $this->assertSame($expectedReturn, $actualReturn);
  139. $request = $this->xmlrpcClient->getLastRequest();
  140. $params = $request->getParams();
  141. $this->assertSame(count($expectedParams), count($params));
  142. $this->assertSame($expectedParams[0], $params[0]->getValue());
  143. }
  144. /**
  145. * @group ZF-1412
  146. *
  147. * @return void
  148. */
  149. public function testSuccessfulRpcMethodCallWithMixedDateParameters()
  150. {
  151. $time = time();
  152. $expectedMethod = 'foo.bar';
  153. $expectedParams = array(
  154. 'username',
  155. new Zend_XmlRpc_Value_DateTime($time)
  156. );
  157. $expectedReturn = array('username', $time);
  158. $this->setServerResponseTo($expectedReturn);
  159. $actualReturn = $this->xmlrpcClient->call($expectedMethod, $expectedParams);
  160. $this->assertSame($expectedReturn, $actualReturn);
  161. $request = $this->xmlrpcClient->getLastRequest();
  162. $response = $this->xmlrpcClient->getLastResponse();
  163. $this->assertSame($expectedMethod, $request->getMethod());
  164. $params = $request->getParams();
  165. $this->assertSame(count($expectedParams), count($params));
  166. $this->assertSame($expectedParams[0], $params[0]->getValue());
  167. $this->assertSame($expectedParams[1], $params[1]);
  168. $this->assertSame($expectedReturn, $response->getReturnValue());
  169. $this->assertFalse($response->isFault());
  170. }
  171. /**
  172. * @group ZF-1797
  173. */
  174. public function testSuccesfulRpcMethodCallWithXmlRpcValueParameters()
  175. {
  176. $time = time();
  177. $params = array(
  178. new Zend_XmlRpc_Value_Boolean(true),
  179. new Zend_XmlRpc_Value_Integer(4),
  180. new Zend_XmlRpc_Value_String('foo')
  181. );
  182. $expect = array(true, 4, 'foo');
  183. $this->setServerResponseTo($expect);
  184. $result = $this->xmlrpcClient->call('foo.bar', $params);
  185. $this->assertSame($expect, $result);
  186. $request = $this->xmlrpcClient->getLastRequest();
  187. $response = $this->xmlrpcClient->getLastResponse();
  188. $this->assertSame('foo.bar', $request->getMethod());
  189. $this->assertSame($params, $request->getParams());
  190. $this->assertSame($expect, $response->getReturnValue());
  191. $this->assertFalse($response->isFault());
  192. }
  193. /**
  194. * @group ZF-2978
  195. */
  196. public function testSkippingSystemCallDisabledByDefault()
  197. {
  198. $this->assertFalse($this->xmlrpcClient->skipSystemLookup());
  199. }
  200. /**
  201. * @group ZF-6993
  202. */
  203. public function testWhenPassingAStringAndAnIntegerIsExpectedParamIsConverted()
  204. {
  205. $this->mockIntrospector();
  206. $this->mockedIntrospector
  207. ->expects($this->exactly(2))
  208. ->method('getMethodSignature')
  209. ->with('test.method')
  210. ->will($this->returnValue(array(array('parameters' => array('int')))));
  211. $expect = 'test.method response';
  212. $this->setServerResponseTo($expect);
  213. $this->assertSame($expect, $this->xmlrpcClient->call('test.method', array('1')));
  214. $params = $this->xmlrpcClient->getLastRequest()->getParams();
  215. $this->assertSame(1, $params[0]->getValue());
  216. $this->setServerResponseTo($expect);
  217. $this->assertSame($expect, $this->xmlrpcClient->call('test.method', '1'));
  218. $params = $this->xmlrpcClient->getLastRequest()->getParams();
  219. $this->assertSame(1, $params[0]->getValue());
  220. }
  221. /**
  222. * @group ZF-8074
  223. */
  224. public function testXmlRpcObjectsAreNotConverted()
  225. {
  226. $this->mockIntrospector();
  227. $this->mockedIntrospector
  228. ->expects($this->exactly(1))
  229. ->method('getMethodSignature')
  230. ->with('date.method')
  231. ->will($this->returnValue(array(array('parameters' => array('dateTime.iso8601', 'string')))));
  232. $expects = 'date.method response';
  233. $this->setServerResponseTo($expects);
  234. $this->assertSame($expects, $this->xmlrpcClient->call('date.method', array(Zend_XmlRpc_Value::getXmlRpcValue(time(), Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME), 'foo')));
  235. }
  236. public function testAllowsSkippingSystemCallForArrayStructLookup()
  237. {
  238. $this->xmlrpcClient->setSkipSystemLookup(true);
  239. $this->assertTrue($this->xmlrpcClient->skipSystemLookup());
  240. }
  241. public function testSkipsSystemCallWhenDirected()
  242. {
  243. $this->mockHttpClient();
  244. $this->mockedHttpClient->expects($this->once())
  245. ->method('request')
  246. ->with('POST')
  247. ->will($this->returnValue($this->makeHttpResponseFor('foo')));
  248. $this->xmlrpcClient->setHttpClient($this->mockedHttpClient);
  249. $this->xmlrpcClient->setSkipSystemLookup(true);
  250. $this->assertSame('foo', $this->xmlrpcClient->call('test.method'));
  251. }
  252. /**#@-*/
  253. // Faults
  254. public function testRpcMethodCallThrowsOnHttpFailure()
  255. {
  256. $status = 404;
  257. $message = 'Not Found';
  258. $body = 'oops';
  259. $response = $this->makeHttpResponseFrom($body, $status, $message);
  260. $this->httpAdapter->setResponse($response);
  261. try {
  262. $this->xmlrpcClient->call('foo');
  263. $this->fail();
  264. } catch (Exception $e) {
  265. $this->assertTrue($e instanceof Zend_XmlRpc_Client_HttpException);
  266. $this->assertEquals($message, $e->getMessage());
  267. $this->assertEquals($status, $e->getCode());
  268. }
  269. }
  270. public function testRpcMethodCallThrowsOnXmlRpcFault()
  271. {
  272. $code = 9;
  273. $message = 'foo';
  274. $fault = new Zend_XmlRpc_Fault($code, $message);
  275. $xml = $fault->saveXml();
  276. $response = $this->makeHttpResponseFrom($xml);
  277. $this->httpAdapter->setResponse($response);
  278. try {
  279. $this->xmlrpcClient->call('foo');
  280. $this->fail();
  281. } catch (Exception $e) {
  282. $this->assertTrue($e instanceof Zend_XmlRpc_Client_FaultException);
  283. $this->assertEquals($message, $e->getMessage());
  284. $this->assertEquals($code, $e->getCode());
  285. }
  286. }
  287. // Server Proxy
  288. public function testGetProxyReturnsServerProxy()
  289. {
  290. $class = 'Zend_XmlRpc_Client_ServerProxy';
  291. $this->assertTrue($this->xmlrpcClient->getProxy() instanceof $class);
  292. }
  293. public function testRpcMethodCallsThroughServerProxy()
  294. {
  295. $expectedReturn = array(7, false, 'foo' => 'bar');
  296. $this->setServerResponseTo($expectedReturn);
  297. $server = $this->xmlrpcClient->getProxy();
  298. $this->assertSame($expectedReturn, $server->listMethods());
  299. $request = $this->xmlrpcClient->getLastRequest();
  300. $this->assertEquals('listMethods', $request->getMethod());
  301. }
  302. public function testRpcMethodCallsThroughNestedServerProxies()
  303. {
  304. $expectedReturn = array(7, false, 'foo' => 'bar');
  305. $this->setServerResponseTo($expectedReturn);
  306. $server = $this->xmlrpcClient->getProxy('foo');
  307. $this->assertSame($expectedReturn, $server->bar->baz->boo());
  308. $request = $this->xmlrpcClient->getLastRequest();
  309. $this->assertEquals('foo.bar.baz.boo', $request->getMethod());
  310. }
  311. public function testClientCachesServerProxies()
  312. {
  313. $proxy = $this->xmlrpcClient->getProxy();
  314. $this->assertSame($proxy, $this->xmlrpcClient->getProxy());
  315. $proxy = $this->xmlrpcClient->getProxy('foo');
  316. $this->assertSame($proxy, $this->xmlrpcClient->getProxy('foo'));
  317. }
  318. public function testServerProxyCachesNestedProxies()
  319. {
  320. $proxy = $this->xmlrpcClient->getProxy();
  321. $foo = $proxy->foo;
  322. $this->assertSame($foo, $proxy->foo);
  323. $bar = $proxy->foo->bar;
  324. $this->assertSame($bar, $proxy->foo->bar);
  325. }
  326. // Introspection
  327. public function testGettingDefaultIntrospector()
  328. {
  329. $xmlrpcClient = new Zend_XmlRpc_Client('http://foo');
  330. $introspector = $xmlrpcClient->getIntrospector();
  331. $this->assertTrue($introspector instanceof Zend_XmlRpc_Client_ServerIntrospection);
  332. $this->assertSame($introspector, $xmlrpcClient->getIntrospector());
  333. }
  334. public function testSettingAndGettingIntrospector()
  335. {
  336. $xmlrpcClient = new Zend_XmlRpc_Client('http://foo');
  337. $introspector = new Zend_XmlRpc_Client_ServerIntrospection($xmlrpcClient);
  338. $this->assertNotSame($introspector, $xmlrpcClient->getIntrospector());
  339. $xmlrpcClient->setIntrospector($introspector);
  340. $this->assertSame($introspector, $xmlrpcClient->getIntrospector());
  341. }
  342. public function testGettingMethodSignature()
  343. {
  344. $method = 'foo';
  345. $signatures = array(array('int', 'int', 'int'));
  346. $this->setServerResponseTo($signatures);
  347. $i = $this->xmlrpcClient->getIntrospector();
  348. $this->assertEquals($signatures, $i->getMethodSignature($method));
  349. $request = $this->xmlrpcClient->getLastRequest();
  350. $this->assertEquals('system.methodSignature', $request->getMethod());
  351. $this->assertEquals(array($method), $request->getParams());
  352. }
  353. public function testListingMethods()
  354. {
  355. $methods = array('foo', 'bar', 'baz');
  356. $this->setServerResponseTo($methods);
  357. $i = $this->xmlrpcClient->getIntrospector();
  358. $this->assertEquals($methods, $i->listMethods());
  359. $request = $this->xmlrpcClient->getLastRequest();
  360. $this->assertEquals('system.listMethods', $request->getMethod());
  361. $this->assertEquals(array(), $request->getParams());
  362. }
  363. public function testGettingAllMethodSignaturesByLooping()
  364. {
  365. // system.listMethods() will return ['foo', 'bar']
  366. $methods = array('foo', 'bar');
  367. $response = $this->getServerResponseFor($methods);
  368. $this->httpAdapter->setResponse($response);
  369. // system.methodSignature('foo') will return [['int'], ['int', 'string']]
  370. $fooSignatures = array(array('int'), array('int', 'string'));
  371. $response = $this->getServerResponseFor($fooSignatures);
  372. $this->httpAdapter->addResponse($response);
  373. // system.methodSignature('bar') will return [['boolean']]
  374. $barSignatures = array(array('boolean'));
  375. $response = $this->getServerResponseFor($barSignatures);
  376. $this->httpAdapter->addResponse($response);
  377. $expected = array('foo' => $fooSignatures,
  378. 'bar' => $barSignatures);
  379. $i = $this->xmlrpcClient->getIntrospector();
  380. $this->assertEquals($expected, $i->getSignatureForEachMethodByLooping());
  381. $request = $this->xmlrpcClient->getLastRequest();
  382. $this->assertEquals('system.methodSignature', $request->getMethod());
  383. $this->assertEquals(array('bar'), $request->getParams());
  384. }
  385. public function testGettingAllMethodSignaturesByMulticall()
  386. {
  387. // system.listMethods() will return ['foo', 'bar']
  388. $whatListMethodsReturns = array('foo', 'bar');
  389. $response = $this->getServerResponseFor($whatListMethodsReturns);
  390. $this->httpAdapter->setResponse($response);
  391. // after system.listMethods(), these system.multicall() params are expected
  392. $multicallParams = array(array('methodName' => 'system.methodSignature',
  393. 'params' => array('foo')),
  394. array('methodName' => 'system.methodSignature',
  395. 'params' => array('bar')));
  396. // system.multicall() will then return [fooSignatures, barSignatures]
  397. $fooSignatures = array(array('int'), array('int', 'string'));
  398. $barSignatures = array(array('boolean'));
  399. $whatMulticallReturns = array($fooSignatures, $barSignatures);
  400. $response = $this->getServerResponseFor($whatMulticallReturns);
  401. $this->httpAdapter->addResponse($response);
  402. $i = $this->xmlrpcClient->getIntrospector();
  403. $expected = array('foo' => $fooSignatures,
  404. 'bar' => $barSignatures);
  405. $this->assertEquals($expected, $i->getSignatureForEachMethodByMulticall());
  406. $request = $this->xmlrpcClient->getLastRequest();
  407. $this->assertEquals('system.multicall', $request->getMethod());
  408. $this->assertEquals(array($multicallParams), $request->getParams());
  409. }
  410. public function testGettingAllMethodSignaturesByMulticallThrowsOnBadCount()
  411. {
  412. // system.listMethods() will return ['foo', 'bar']
  413. $whatListMethodsReturns = array('foo', 'bar');
  414. $response = $this->getServerResponseFor($whatListMethodsReturns);
  415. $this->httpAdapter->setResponse($response);
  416. // system.multicall() will then return only [fooSignatures]
  417. $fooSignatures = array(array('int'), array('int', 'string'));
  418. $whatMulticallReturns = array($fooSignatures); // error! no bar signatures!
  419. $response = $this->getServerResponseFor($whatMulticallReturns);
  420. $this->httpAdapter->addResponse($response);
  421. $i = $this->xmlrpcClient->getIntrospector();
  422. try {
  423. $i->getSignatureForEachMethodByMulticall();
  424. } catch (Zend_XmlRpc_Client_IntrospectException $e) {
  425. $this->assertRegexp('/bad number/i', $e->getMessage());
  426. }
  427. }
  428. public function testGettingAllMethodSignaturesByMulticallThrowsOnBadType()
  429. {
  430. // system.listMethods() will return ['foo', 'bar']
  431. $whatListMethodsReturns = array('foo', 'bar');
  432. $response = $this->getServerResponseFor($whatListMethodsReturns);
  433. $this->httpAdapter->setResponse($response);
  434. // system.multicall() will then return only an int
  435. $whatMulticallReturns = 1; // error! no signatures?
  436. $response = $this->getServerResponseFor($whatMulticallReturns);
  437. $this->httpAdapter->addResponse($response);
  438. $i = $this->xmlrpcClient->getIntrospector();
  439. try {
  440. $i->getSignatureForEachMethodByMulticall();
  441. } catch (Zend_XmlRpc_Client_IntrospectException $e) {
  442. $this->assertRegexp('/got integer/i', $e->getMessage());
  443. }
  444. }
  445. public function testGettingAllMethodSignaturesDefaultsToMulticall()
  446. {
  447. // system.listMethods() will return ['foo', 'bar']
  448. $whatListMethodsReturns = array('foo', 'bar');
  449. $response = $this->getServerResponseFor($whatListMethodsReturns);
  450. $this->httpAdapter->setResponse($response);
  451. // system.multicall() will then return [fooSignatures, barSignatures]
  452. $fooSignatures = array(array('int'), array('int', 'string'));
  453. $barSignatures = array(array('boolean'));
  454. $whatMulticallReturns = array($fooSignatures, $barSignatures);
  455. $response = $this->getServerResponseFor($whatMulticallReturns);
  456. $this->httpAdapter->addResponse($response);
  457. $i = $this->xmlrpcClient->getIntrospector();
  458. $expected = array('foo' => $fooSignatures,
  459. 'bar' => $barSignatures);
  460. $this->assertEquals($expected, $i->getSignatureForEachMethod());
  461. $request = $this->xmlrpcClient->getLastRequest();
  462. $this->assertEquals('system.multicall', $request->getMethod());
  463. }
  464. public function testGettingAllMethodSignaturesDegradesToLooping()
  465. {
  466. // system.listMethods() will return ['foo', 'bar']
  467. $whatListMethodsReturns = array('foo', 'bar');
  468. $response = $this->getServerResponseFor($whatListMethodsReturns);
  469. $this->httpAdapter->setResponse($response);
  470. // system.multicall() will return a fault
  471. $fault = new Zend_XmlRpc_Fault(7, 'bad method');
  472. $xml = $fault->saveXml();
  473. $response = $this->makeHttpResponseFrom($xml);
  474. $this->httpAdapter->addResponse($response);
  475. // system.methodSignature('foo') will return [['int'], ['int', 'string']]
  476. $fooSignatures = array(array('int'), array('int', 'string'));
  477. $response = $this->getServerResponseFor($fooSignatures);
  478. $this->httpAdapter->addResponse($response);
  479. // system.methodSignature('bar') will return [['boolean']]
  480. $barSignatures = array(array('boolean'));
  481. $response = $this->getServerResponseFor($barSignatures);
  482. $this->httpAdapter->addResponse($response);
  483. $i = $this->xmlrpcClient->getIntrospector();
  484. $expected = array('foo' => $fooSignatures,
  485. 'bar' => $barSignatures);
  486. $this->assertEquals($expected, $i->getSignatureForEachMethod());
  487. $request = $this->xmlrpcClient->getLastRequest();
  488. $this->assertEquals('system.methodSignature', $request->getMethod());
  489. }
  490. /**
  491. * @group ZF-4372
  492. */
  493. public function testSettingUriOnHttpClientIsNotOverwrittenByXmlRpcClient()
  494. {
  495. $changedUri = "http://bar:80";
  496. // Overwrite: http://foo:80
  497. $this->setServerResponseTo(array());
  498. $this->xmlrpcClient->getHttpClient()->setUri($changedUri);
  499. $this->xmlrpcClient->call("foo");
  500. $uri = $this->xmlrpcClient->getHttpClient()->getUri(true);
  501. $this->assertEquals($changedUri, $uri);
  502. }
  503. /**
  504. * @group ZF-4372
  505. */
  506. public function testSettingNoHttpClientUriForcesClientToSetUri()
  507. {
  508. $baseUri = "http://foo:80";
  509. $this->httpAdapter = new Zend_Http_Client_Adapter_Test();
  510. $this->httpClient = new Zend_Http_Client(null, array('adapter' => $this->httpAdapter));
  511. $this->xmlrpcClient = new Zend_XmlRpc_Client($baseUri);
  512. $this->xmlrpcClient->setHttpClient($this->httpClient);
  513. $this->setServerResponseTo(array());
  514. $this->assertNull($this->xmlrpcClient->getHttpClient()->getUri());
  515. $this->xmlrpcClient->call("foo");
  516. $uri = $this->xmlrpcClient->getHttpClient()->getUri(true);
  517. $this->assertEquals($baseUri, $uri);
  518. }
  519. /**
  520. * @group ZF-3288
  521. */
  522. public function testCustomHttpClientUserAgentIsNotOverridden()
  523. {
  524. $this->assertNull(
  525. $this->httpClient->getHeader('user-agent'),
  526. 'UA is null if no request was made'
  527. );
  528. $this->setServerResponseTo(true);
  529. $this->assertTrue($this->xmlrpcClient->call('method'));
  530. $this->assertSame(
  531. 'Zend_XmlRpc_Client',
  532. $this->httpClient->getHeader('user-agent'),
  533. 'If no custom UA is set, set Zend_XmlRpc_Client'
  534. );
  535. $expectedUserAgent = 'Zend_XmlRpc_Client (custom)';
  536. $this->httpClient->setHeaders(array('user-agent' => $expectedUserAgent));
  537. $this->setServerResponseTo(true);
  538. $this->assertTrue($this->xmlrpcClient->call('method'));
  539. $this->assertSame($expectedUserAgent, $this->httpClient->getHeader('user-agent'));
  540. }
  541. /**
  542. * @group ZF-8478
  543. */
  544. public function testPythonSimpleXMLRPCServerWithUnsupportedMethodSignatures()
  545. {
  546. try
  547. {
  548. $introspector = new Zend_XmlRpc_Client_ServerIntrospection(
  549. new Test_XmlRpc_Client('http://localhost/')
  550. );
  551. $signature = $introspector->getMethodSignature('add');
  552. if (!is_array($signature)) {
  553. $this->fail('Expected exception has not been thrown');
  554. }
  555. }
  556. catch (Zend_XmlRpc_Client_IntrospectException $e) {
  557. $this->assertEquals('Invalid signature for method "add"', $e->getMessage());
  558. }
  559. }
  560. /**
  561. * @group ZF-8580
  562. */
  563. public function testCallSelectsCorrectSignatureIfMoreThanOneIsAvailable()
  564. {
  565. $this->mockIntrospector();
  566. $this->mockedIntrospector
  567. ->expects($this->exactly(2))
  568. ->method('getMethodSignature')
  569. ->with('get')
  570. ->will($this->returnValue(array(
  571. array('parameters' => array('int')),
  572. array('parameters' => array('array'))
  573. )));
  574. $expectedResult = 'array';
  575. $this->setServerResponseTo($expectedResult);
  576. $this->assertSame(
  577. $expectedResult,
  578. $this->xmlrpcClient->call('get', array(array(1)))
  579. );
  580. $expectedResult = 'integer';
  581. $this->setServerResponseTo($expectedResult);
  582. $this->assertSame(
  583. $expectedResult,
  584. $this->xmlrpcClient->call('get', array(1))
  585. );
  586. }
  587. /**
  588. * @group ZF-1897
  589. */
  590. public function testHandlesLeadingOrTrailingWhitespaceInChunkedResponseProperly()
  591. {
  592. $baseUri = "http://foo:80";
  593. $this->httpAdapter = new Zend_Http_Client_Adapter_Test();
  594. $this->httpClient = new Zend_Http_Client(null, array('adapter' => $this->httpAdapter));
  595. $respBody = file_get_contents(dirname(__FILE__) . "/_files/ZF1897-response-chunked.txt");
  596. $this->httpAdapter->setResponse($respBody);
  597. $this->xmlrpcClient = new Zend_XmlRpc_Client($baseUri);
  598. $this->xmlrpcClient->setHttpClient($this->httpClient);
  599. $this->assertEquals('FOO', $this->xmlrpcClient->call('foo'));
  600. }
  601. // Helpers
  602. public function setServerResponseTo($nativeVars)
  603. {
  604. $response = $this->getServerResponseFor($nativeVars);
  605. $this->httpAdapter->setResponse($response);
  606. }
  607. public function getServerResponseFor($nativeVars)
  608. {
  609. $response = new Zend_XmlRpc_Response();
  610. $response->setReturnValue($nativeVars);
  611. $xml = $response->saveXml();
  612. $response = $this->makeHttpResponseFrom($xml);
  613. return $response;
  614. }
  615. public function makeHttpResponseFrom($data, $status=200, $message='OK')
  616. {
  617. $headers = array("HTTP/1.1 $status $message",
  618. "Status: $status",
  619. 'Content_Type: text/xml; charset=utf-8',
  620. 'Content-Length: ' . strlen($data)
  621. );
  622. return implode("\r\n", $headers) . "\r\n\r\n$data\r\n\r\n";
  623. }
  624. public function makeHttpResponseFor($nativeVars)
  625. {
  626. $response = $this->getServerResponseFor($nativeVars);
  627. return Zend_Http_Response::fromString($response);
  628. }
  629. public function mockIntrospector()
  630. {
  631. $this->mockedIntrospector = $this->getMock(
  632. 'Zend_XmlRpc_Client_ServerIntrospection',
  633. array(),
  634. array(),
  635. '',
  636. false,
  637. false
  638. );
  639. $this->xmlrpcClient->setIntrospector($this->mockedIntrospector);
  640. }
  641. public function mockHttpClient()
  642. {
  643. $this->mockedHttpClient = $this->getMock('Zend_Http_Client');
  644. $this->xmlrpcClient->setHttpClient($this->mockedHttpClient);
  645. }
  646. }
  647. /** related to ZF-8478 */
  648. require_once 'Zend/XmlRpc/Client/ServerProxy.php';
  649. class Python_SimpleXMLRPCServerWithUnsupportedIntrospection extends Zend_XmlRpc_Client_ServerProxy {
  650. public function __call($method, $args) {
  651. if ($method == 'methodSignature') {
  652. return 'signatures not supported';
  653. }
  654. return parent::__call($method, $args);
  655. }
  656. }
  657. /** related to ZF-8478 */
  658. require_once 'Zend/XmlRpc/Client.php';
  659. class Test_XmlRpc_Client extends Zend_XmlRpc_Client {
  660. public function getProxy($namespace = '') {
  661. if (empty($this->_proxyCache[$namespace])) {
  662. $this->_proxyCache[$namespace] = new Python_SimpleXMLRPCServerWithUnsupportedIntrospection($this, $namespace);
  663. }
  664. return parent::getProxy($namespace);
  665. }
  666. }
  667. // Call Zend_XmlRpc_ClientTest::main() if this source file is executed directly.
  668. if (PHPUnit_MAIN_METHOD == "Zend_XmlRpc_ClientTest::main") {
  669. Zend_XmlRpc_ClientTest::main();
  670. }