ClientTest.php 29 KB

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