ServerTest.php 20 KB

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