ServerTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <?php
  2. // Call Zend_XmlRpc_ServerTest::main() if this source file is executed directly.
  3. if (!defined('PHPUnit_MAIN_METHOD')) {
  4. define('PHPUnit_MAIN_METHOD', 'Zend_XmlRpc_ServerTest::main');
  5. }
  6. require_once dirname(__FILE__) . '/../../TestHelper.php';
  7. require_once 'Zend/XmlRpc/Server.php';
  8. require_once 'Zend/XmlRpc/Request.php';
  9. require_once 'Zend/XmlRpc/Response.php';
  10. /**
  11. * Test case for Zend_XmlRpc_Server
  12. *
  13. * @package Zend_XmlRpc
  14. * @subpackage UnitTests
  15. * @version $Id$
  16. */
  17. class Zend_XmlRpc_ServerTest extends PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * Zend_XmlRpc_Server object
  21. * @var Zend_XmlRpc_Server
  22. */
  23. protected $_server;
  24. /**
  25. * Runs the test methods of this class.
  26. *
  27. * @return void
  28. */
  29. public static function main()
  30. {
  31. $suite = new PHPUnit_Framework_TestSuite("Zend_XmlRpc_ServerTest");
  32. $result = PHPUnit_TextUI_TestRunner::run($suite);
  33. }
  34. /**
  35. * Setup environment
  36. */
  37. public function setUp()
  38. {
  39. $this->_server = new Zend_XmlRpc_Server();
  40. }
  41. /**
  42. * Teardown environment
  43. */
  44. public function tearDown()
  45. {
  46. unset($this->_server);
  47. }
  48. /**
  49. * __construct() test
  50. *
  51. * Call as method call
  52. *
  53. * Returns: void
  54. */
  55. public function test__construct()
  56. {
  57. $this->assertTrue($this->_server instanceof Zend_XmlRpc_Server);
  58. }
  59. /**
  60. * addFunction() test
  61. *
  62. * Call as method call
  63. *
  64. * Expects:
  65. * - function:
  66. * - namespace: Optional; has default;
  67. *
  68. * Returns: void
  69. */
  70. public function testAddFunction()
  71. {
  72. try {
  73. $this->_server->addFunction('Zend_XmlRpc_Server_testFunction', 'zsr');
  74. } catch (Exception $e) {
  75. $this->fail('Attachment should have worked');
  76. }
  77. $methods = $this->_server->listMethods();
  78. $this->assertTrue(in_array('zsr.Zend_XmlRpc_Server_testFunction', $methods));
  79. try {
  80. $this->_server->addFunction('nosuchfunction');
  81. $this->fail('nosuchfunction() should not exist and should throw an exception');
  82. } catch (Exception $e) {
  83. // do nothing
  84. }
  85. $server = new Zend_XmlRpc_Server();
  86. try {
  87. $server->addFunction(
  88. array(
  89. 'Zend_XmlRpc_Server_testFunction',
  90. 'Zend_XmlRpc_Server_testFunction2',
  91. ),
  92. 'zsr'
  93. );
  94. } catch (Exception $e) {
  95. $this->fail('Error attaching array of functions: ' . $e->getMessage());
  96. }
  97. $methods = $server->listMethods();
  98. $this->assertTrue(in_array('zsr.Zend_XmlRpc_Server_testFunction', $methods));
  99. $this->assertTrue(in_array('zsr.Zend_XmlRpc_Server_testFunction2', $methods));
  100. }
  101. /**
  102. * get/loadFunctions() test
  103. */
  104. public function testFunctions()
  105. {
  106. try {
  107. $this->_server->addFunction(
  108. array(
  109. 'Zend_XmlRpc_Server_testFunction',
  110. 'Zend_XmlRpc_Server_testFunction2',
  111. ),
  112. 'zsr'
  113. );
  114. } catch (Exception $e) {
  115. $this->fail('Error attaching functions: ' . $e->getMessage());
  116. }
  117. $expected = $this->_server->listMethods();
  118. $functions = $this->_server->getFunctions();
  119. $server = new Zend_XmlRpc_Server();
  120. $server->loadFunctions($functions);
  121. $actual = $server->listMethods();
  122. $this->assertSame($expected, $actual);
  123. }
  124. /**
  125. * setClass() test
  126. */
  127. public function testSetClass()
  128. {
  129. $this->_server->setClass('Zend_XmlRpc_Server_testClass', 'test');
  130. $methods = $this->_server->listMethods();
  131. $this->assertTrue(in_array('test.test1', $methods));
  132. $this->assertTrue(in_array('test.test2', $methods));
  133. $this->assertFalse(in_array('test._test3', $methods));
  134. $this->assertFalse(in_array('test.__construct', $methods));
  135. }
  136. /**
  137. * fault() test
  138. */
  139. public function testFault()
  140. {
  141. $fault = $this->_server->fault('This is a fault', 411);
  142. $this->assertTrue($fault instanceof Zend_XmlRpc_Server_Fault);
  143. $this->assertEquals(411, $fault->getCode());
  144. $this->assertEquals('This is a fault', $fault->getMessage());
  145. $fault = $this->_server->fault(new Zend_XmlRpc_Server_Exception('Exception fault', 511));
  146. $this->assertTrue($fault instanceof Zend_XmlRpc_Server_Fault);
  147. $this->assertEquals(511, $fault->getCode());
  148. $this->assertEquals('Exception fault', $fault->getMessage());
  149. }
  150. /**
  151. * handle() test
  152. *
  153. * Call as method call
  154. *
  155. * Expects:
  156. * - request: Optional;
  157. *
  158. * Returns: Zend_XmlRpc_Response|Zend_XmlRpc_Fault
  159. */
  160. public function testHandle()
  161. {
  162. $request = new Zend_XmlRpc_Request();
  163. $request->setMethod('system.listMethods');
  164. $response = $this->_server->handle($request);
  165. $this->assertTrue($response instanceof Zend_XmlRpc_Response);
  166. $return = $response->getReturnValue();
  167. $this->assertTrue(is_array($return));
  168. $this->assertTrue(in_array('system.multicall', $return));
  169. }
  170. /**
  171. * Test that only calling methods using a valid parameter signature works
  172. */
  173. public function testHandle2()
  174. {
  175. $request = new Zend_XmlRpc_Request();
  176. $request->setMethod('system.methodHelp');
  177. $response = $this->_server->handle($request);
  178. $this->assertTrue($response instanceof Zend_XmlRpc_Fault);
  179. $this->assertEquals(623, $response->getCode());
  180. }
  181. /**
  182. * setResponseClass() test
  183. *
  184. * Call as method call
  185. *
  186. * Expects:
  187. * - class:
  188. *
  189. * Returns: boolean
  190. */
  191. public function testSetResponseClass()
  192. {
  193. $this->_server->setResponseClass('Zend_XmlRpc_Server_testResponse');
  194. $request = new Zend_XmlRpc_Request();
  195. $request->setMethod('system.listMethods');
  196. $response = $this->_server->handle($request);
  197. $this->assertTrue($response instanceof Zend_XmlRpc_Response);
  198. $this->assertTrue($response instanceof Zend_XmlRpc_Server_testResponse);
  199. }
  200. /**
  201. * listMethods() test
  202. *
  203. * Call as method call
  204. *
  205. * Returns: array
  206. */
  207. public function testListMethods()
  208. {
  209. $methods = $this->_server->listMethods();
  210. $this->assertTrue(is_array($methods));
  211. $this->assertTrue(in_array('system.listMethods', $methods));
  212. $this->assertTrue(in_array('system.methodHelp', $methods));
  213. $this->assertTrue(in_array('system.methodSignature', $methods));
  214. $this->assertTrue(in_array('system.multicall', $methods));
  215. }
  216. /**
  217. * methodHelp() test
  218. *
  219. * Call as method call
  220. *
  221. * Expects:
  222. * - method:
  223. *
  224. * Returns: string
  225. */
  226. public function testMethodHelp()
  227. {
  228. $help = $this->_server->methodHelp('system.listMethods');
  229. $this->assertContains('all available XMLRPC methods', $help);
  230. }
  231. /**
  232. * methodSignature() test
  233. *
  234. * Call as method call
  235. *
  236. * Expects:
  237. * - method:
  238. *
  239. * Returns: array
  240. */
  241. public function testMethodSignature()
  242. {
  243. $sig = $this->_server->methodSignature('system.methodSignature');
  244. $this->assertTrue(is_array($sig));
  245. $this->assertEquals(1, count($sig), var_export($sig, 1));
  246. }
  247. /**
  248. * multicall() test
  249. *
  250. * Call as method call
  251. *
  252. * Expects:
  253. * - methods:
  254. *
  255. * Returns: array
  256. */
  257. public function testMulticall()
  258. {
  259. $struct = array(
  260. array(
  261. 'methodName' => 'system.listMethods',
  262. 'params' => array()
  263. ),
  264. array(
  265. 'methodName' => 'system.methodHelp',
  266. 'params' => array('system.multicall')
  267. )
  268. );
  269. $request = new Zend_XmlRpc_Request();
  270. $request->setMethod('system.multicall');
  271. $request->addParam($struct);
  272. $response = $this->_server->handle($request);
  273. $this->assertTrue($response instanceof Zend_XmlRpc_Response, $response->__toString() . "\n\n" . $request->__toString());
  274. $returns = $response->getReturnValue();
  275. $this->assertTrue(is_array($returns));
  276. $this->assertEquals(2, count($returns), var_export($returns, 1));
  277. $this->assertTrue(is_array($returns[0]), var_export($returns[0], 1));
  278. $this->assertTrue(is_string($returns[1]), var_export($returns[1], 1));
  279. }
  280. /**
  281. * Test get/setEncoding()
  282. */
  283. public function testGetSetEncoding()
  284. {
  285. $this->assertEquals('UTF-8', $this->_server->getEncoding());
  286. $this->_server->setEncoding('ISO-8859-1');
  287. $this->assertEquals('ISO-8859-1', $this->_server->getEncoding());
  288. }
  289. /**
  290. * Test request/response encoding
  291. */
  292. public function testRequestResponseEncoding()
  293. {
  294. $response = $this->_server->handle();
  295. $request = $this->_server->getRequest();
  296. $this->assertEquals('UTF-8', $request->getEncoding());
  297. $this->assertEquals('UTF-8', $response->getEncoding());
  298. }
  299. /**
  300. * Test request/response encoding (alternate encoding)
  301. */
  302. public function testRequestResponseEncoding2()
  303. {
  304. $this->_server->setEncoding('ISO-8859-1');
  305. $response = $this->_server->handle();
  306. $request = $this->_server->getRequest();
  307. $this->assertEquals('ISO-8859-1', $request->getEncoding());
  308. $this->assertEquals('ISO-8859-1', $response->getEncoding());
  309. }
  310. public function testAddFunctionWithExtraArgs()
  311. {
  312. $this->_server->addFunction('Zend_XmlRpc_Server_testFunction', 'test', 'arg1');
  313. $methods = $this->_server->listMethods();
  314. $this->assertContains('test.Zend_XmlRpc_Server_testFunction', $methods);
  315. }
  316. public function testAddFunctionThrowsExceptionWithBadData()
  317. {
  318. $o = new stdClass();
  319. try {
  320. $this->_server->addFunction($o);
  321. $this->fail('addFunction() should not accept objects');
  322. } catch (Exception $e) {
  323. // success
  324. }
  325. }
  326. public function testLoadFunctionsThrowsExceptionWithBadData()
  327. {
  328. $o = new stdClass();
  329. try {
  330. $this->_server->loadFunctions($o);
  331. $this->fail('loadFunctions() should not accept objects');
  332. } catch (Exception $e) {
  333. // success
  334. }
  335. $o = array($o);
  336. try {
  337. $this->_server->loadFunctions($o);
  338. $this->fail('loadFunctions() should not allow non-reflection objects in an array');
  339. } catch (Exception $e) {
  340. // success
  341. }
  342. }
  343. public function testSetClassThrowsExceptionWithInvalidClass()
  344. {
  345. try {
  346. $this->_server->setClass('mybogusclass');
  347. $this->fail('setClass() should not allow invalid classes');
  348. } catch (Exception $e) {
  349. // success
  350. }
  351. }
  352. public function testSetRequestUsingString()
  353. {
  354. $this->_server->setRequest('Zend_XmlRpc_Server_testRequest');
  355. $req = $this->_server->getRequest();
  356. $this->assertTrue($req instanceof Zend_XmlRpc_Server_testRequest);
  357. }
  358. public function testSetRequestThrowsExceptionOnBadClass()
  359. {
  360. try {
  361. $this->_server->setRequest('Zend_XmlRpc_Server_testRequest2');
  362. $this->fail('Invalid request class should throw exception');
  363. } catch (Exception $e) {
  364. // success
  365. }
  366. try {
  367. $this->_server->setRequest($this);
  368. $this->fail('Invalid request object should throw exception');
  369. } catch (Exception $e) {
  370. // success
  371. }
  372. }
  373. public function testHandleObjectMethod()
  374. {
  375. $this->_server->setClass('Zend_XmlRpc_Server_testClass');
  376. $request = new Zend_XmlRpc_Request();
  377. $request->setMethod('test1');
  378. $request->addParam('value');
  379. $response = $this->_server->handle($request);
  380. $this->assertFalse($response instanceof Zend_XmlRpc_Fault);
  381. $this->assertEquals('String: value', $response->getReturnValue());
  382. }
  383. public function testHandleClassStaticMethod()
  384. {
  385. $this->_server->setClass('Zend_XmlRpc_Server_testClass');
  386. $request = new Zend_XmlRpc_Request();
  387. $request->setMethod('test2');
  388. $request->addParam(array('value1', 'value2'));
  389. $response = $this->_server->handle($request);
  390. $this->assertFalse($response instanceof Zend_XmlRpc_Fault);
  391. $this->assertEquals('value1; value2', $response->getReturnValue());
  392. }
  393. public function testHandleFunction()
  394. {
  395. $this->_server->addFunction('Zend_XmlRpc_Server_testFunction');
  396. $request = new Zend_XmlRpc_Request();
  397. $request->setMethod('Zend_XmlRpc_Server_testFunction');
  398. $request->setParams(array(array('value1'), 'key'));
  399. $response = $this->_server->handle($request);
  400. $this->assertFalse($response instanceof Zend_XmlRpc_Fault);
  401. $this->assertEquals('key: value1', $response->getReturnValue());
  402. }
  403. public function testMulticallReturnsFaultsWithBadData()
  404. {
  405. // bad method array
  406. $try = array(
  407. 'system.listMethods',
  408. array(
  409. 'name' => 'system.listMethods'
  410. ),
  411. array(
  412. 'methodName' => 'system.listMethods'
  413. ),
  414. array(
  415. 'methodName' => 'system.listMethods',
  416. 'params' => ''
  417. ),
  418. array(
  419. 'methodName' => 'system.multicall',
  420. 'params' => array()
  421. )
  422. );
  423. $returned = $this->_server->multicall($try);
  424. $this->assertTrue(is_array($returned));
  425. $this->assertEquals(5, count($returned));
  426. $response = $returned[0];
  427. $this->assertTrue(is_array($response));
  428. $this->assertTrue(isset($response['faultCode']));
  429. $this->assertEquals(601, $response['faultCode']);
  430. $response = $returned[1];
  431. $this->assertTrue(is_array($response));
  432. $this->assertTrue(isset($response['faultCode']));
  433. $this->assertEquals(602, $response['faultCode']);
  434. $response = $returned[2];
  435. $this->assertTrue(is_array($response));
  436. $this->assertTrue(isset($response['faultCode']));
  437. $this->assertEquals(603, $response['faultCode']);
  438. $response = $returned[3];
  439. $this->assertTrue(is_array($response));
  440. $this->assertTrue(isset($response['faultCode']));
  441. $this->assertEquals(604, $response['faultCode']);
  442. $response = $returned[4];
  443. $this->assertTrue(is_array($response));
  444. $this->assertTrue(isset($response['faultCode']));
  445. $this->assertEquals(605, $response['faultCode']);
  446. }
  447. /**
  448. * @see ZF-2872
  449. */
  450. public function testCanMarshalBase64Requests()
  451. {
  452. $this->_server->setClass('Zend_XmlRpc_Server_testClass', 'test');
  453. $data = base64_encode('this is the payload');
  454. $param = array('type' => 'base64', 'value' => $data);
  455. $request = new Zend_XmlRpc_Request('test.base64', array($param));
  456. $response = $this->_server->handle($request);
  457. $this->assertFalse($response instanceof Zend_XmlRpc_Fault);
  458. $this->assertEquals($data, $response->getReturnValue());
  459. }
  460. /**
  461. * @group ZF-6034
  462. */
  463. public function testPrototypeReturnValueMustReflectDocBlock()
  464. {
  465. $server = new Zend_XmlRpc_Server();
  466. $server->setClass('Zend_XmlRpc_Server_testClass');
  467. $table = $server->getDispatchTable();
  468. $method = $table->getMethod('test1');
  469. foreach ($method->getPrototypes() as $prototype) {
  470. $this->assertNotEquals('void', $prototype->getReturnType(), var_export($prototype, 1));
  471. }
  472. }
  473. }
  474. /**
  475. * Zend_XmlRpc_Server_testFunction
  476. *
  477. * Function for use with xmlrpc server unit tests
  478. *
  479. * @param array $var1
  480. * @param string $var2
  481. * @return string
  482. */
  483. function Zend_XmlRpc_Server_testFunction($var1, $var2 = 'optional')
  484. {
  485. return $var2 . ': ' . implode(',', (array) $var1);
  486. }
  487. /**
  488. * Zend_XmlRpc_Server_testFunction2
  489. *
  490. * Function for use with xmlrpc server unit tests
  491. *
  492. * @return string
  493. */
  494. function Zend_XmlRpc_Server_testFunction2()
  495. {
  496. return 'function2';
  497. }
  498. class Zend_XmlRpc_Server_testClass
  499. {
  500. /**
  501. * Constructor
  502. *
  503. * @return void
  504. */
  505. public function __construct()
  506. {
  507. }
  508. /**
  509. * Test1
  510. *
  511. * Returns 'String: ' . $string
  512. *
  513. * @param string $string
  514. * @return string
  515. */
  516. public function test1($string)
  517. {
  518. return 'String: ' . (string) $string;
  519. }
  520. /**
  521. * Test2
  522. *
  523. * Returns imploded array
  524. *
  525. * @param array $array
  526. * @return string
  527. */
  528. public static function test2($array)
  529. {
  530. return implode('; ', (array) $array);
  531. }
  532. /**
  533. * Test3
  534. *
  535. * Should not be available...
  536. *
  537. * @return void
  538. */
  539. protected function _test3()
  540. {
  541. }
  542. /**
  543. * Test base64 encoding in request and response
  544. *
  545. * @param base64 $data
  546. * @return base64
  547. */
  548. public function base64($data)
  549. {
  550. return $data;
  551. }
  552. }
  553. class Zend_XmlRpc_Server_testResponse extends Zend_XmlRpc_Response
  554. {
  555. }
  556. class Zend_XmlRpc_Server_testRequest extends Zend_XmlRpc_Request
  557. {
  558. }
  559. // Call Zend_XmlRpc_ServerTest::main() if this source file is executed directly.
  560. if (PHPUnit_MAIN_METHOD == "Zend_XmlRpc_ServerTest::main") {
  561. Zend_XmlRpc_ServerTest::main();
  562. }