2
0

ClientTest.php 21 KB

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