ClientTest.php 28 KB

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