ClientTest.php 26 KB

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