ServerTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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 (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 (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 (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 (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. /**
  160. * fault() test
  161. */
  162. public function testFault()
  163. {
  164. $fault = $this->_server->fault('This is a fault', 411);
  165. $this->assertTrue($fault instanceof Zend_XmlRpc_Server_Fault);
  166. $this->assertEquals(411, $fault->getCode());
  167. $this->assertEquals('This is a fault', $fault->getMessage());
  168. $fault = $this->_server->fault(new Zend_XmlRpc_Server_Exception('Exception fault', 511));
  169. $this->assertTrue($fault instanceof Zend_XmlRpc_Server_Fault);
  170. $this->assertEquals(511, $fault->getCode());
  171. $this->assertEquals('Exception fault', $fault->getMessage());
  172. }
  173. /**
  174. * handle() test
  175. *
  176. * Call as method call
  177. *
  178. * Expects:
  179. * - request: Optional;
  180. *
  181. * Returns: Zend_XmlRpc_Response|Zend_XmlRpc_Fault
  182. */
  183. public function testHandle()
  184. {
  185. $request = new Zend_XmlRpc_Request();
  186. $request->setMethod('system.listMethods');
  187. $response = $this->_server->handle($request);
  188. $this->assertTrue($response instanceof Zend_XmlRpc_Response);
  189. $return = $response->getReturnValue();
  190. $this->assertTrue(is_array($return));
  191. $this->assertTrue(in_array('system.multicall', $return));
  192. }
  193. /**
  194. * Test that only calling methods using a valid parameter signature works
  195. */
  196. public function testHandle2()
  197. {
  198. $request = new Zend_XmlRpc_Request();
  199. $request->setMethod('system.methodHelp');
  200. $response = $this->_server->handle($request);
  201. $this->assertTrue($response instanceof Zend_XmlRpc_Fault);
  202. $this->assertEquals(623, $response->getCode());
  203. }
  204. /**
  205. * setResponseClass() test
  206. *
  207. * Call as method call
  208. *
  209. * Expects:
  210. * - class:
  211. *
  212. * Returns: boolean
  213. */
  214. public function testSetResponseClass()
  215. {
  216. $this->_server->setResponseClass('Zend_XmlRpc_Server_testResponse');
  217. $request = new Zend_XmlRpc_Request();
  218. $request->setMethod('system.listMethods');
  219. $response = $this->_server->handle($request);
  220. $this->assertTrue($response instanceof Zend_XmlRpc_Response);
  221. $this->assertTrue($response instanceof Zend_XmlRpc_Server_testResponse);
  222. }
  223. /**
  224. * listMethods() test
  225. *
  226. * Call as method call
  227. *
  228. * Returns: array
  229. */
  230. public function testListMethods()
  231. {
  232. $methods = $this->_server->listMethods();
  233. $this->assertTrue(is_array($methods));
  234. $this->assertTrue(in_array('system.listMethods', $methods));
  235. $this->assertTrue(in_array('system.methodHelp', $methods));
  236. $this->assertTrue(in_array('system.methodSignature', $methods));
  237. $this->assertTrue(in_array('system.multicall', $methods));
  238. }
  239. /**
  240. * methodHelp() test
  241. *
  242. * Call as method call
  243. *
  244. * Expects:
  245. * - method:
  246. *
  247. * Returns: string
  248. */
  249. public function testMethodHelp()
  250. {
  251. $help = $this->_server->methodHelp('system.listMethods');
  252. $this->assertContains('all available XMLRPC methods', $help);
  253. }
  254. /**
  255. * methodSignature() test
  256. *
  257. * Call as method call
  258. *
  259. * Expects:
  260. * - method:
  261. *
  262. * Returns: array
  263. */
  264. public function testMethodSignature()
  265. {
  266. $sig = $this->_server->methodSignature('system.methodSignature');
  267. $this->assertTrue(is_array($sig));
  268. $this->assertEquals(1, count($sig), var_export($sig, 1));
  269. }
  270. /**
  271. * multicall() test
  272. *
  273. * Call as method call
  274. *
  275. * Expects:
  276. * - methods:
  277. *
  278. * Returns: array
  279. */
  280. public function testMulticall()
  281. {
  282. $struct = array(
  283. array(
  284. 'methodName' => 'system.listMethods',
  285. 'params' => array()
  286. ),
  287. array(
  288. 'methodName' => 'system.methodHelp',
  289. 'params' => array('system.multicall')
  290. )
  291. );
  292. $request = new Zend_XmlRpc_Request();
  293. $request->setMethod('system.multicall');
  294. $request->addParam($struct);
  295. $response = $this->_server->handle($request);
  296. $this->assertTrue($response instanceof Zend_XmlRpc_Response, $response->__toString() . "\n\n" . $request->__toString());
  297. $returns = $response->getReturnValue();
  298. $this->assertTrue(is_array($returns));
  299. $this->assertEquals(2, count($returns), var_export($returns, 1));
  300. $this->assertTrue(is_array($returns[0]), var_export($returns[0], 1));
  301. $this->assertTrue(is_string($returns[1]), var_export($returns[1], 1));
  302. }
  303. /**
  304. * Test get/setEncoding()
  305. */
  306. public function testGetSetEncoding()
  307. {
  308. $this->assertEquals('UTF-8', $this->_server->getEncoding());
  309. $this->_server->setEncoding('ISO-8859-1');
  310. $this->assertEquals('ISO-8859-1', $this->_server->getEncoding());
  311. }
  312. /**
  313. * Test request/response encoding
  314. */
  315. public function testRequestResponseEncoding()
  316. {
  317. $response = $this->_server->handle();
  318. $request = $this->_server->getRequest();
  319. $this->assertEquals('UTF-8', $request->getEncoding());
  320. $this->assertEquals('UTF-8', $response->getEncoding());
  321. }
  322. /**
  323. * Test request/response encoding (alternate encoding)
  324. */
  325. public function testRequestResponseEncoding2()
  326. {
  327. $this->_server->setEncoding('ISO-8859-1');
  328. $response = $this->_server->handle();
  329. $request = $this->_server->getRequest();
  330. $this->assertEquals('ISO-8859-1', $request->getEncoding());
  331. $this->assertEquals('ISO-8859-1', $response->getEncoding());
  332. }
  333. public function testAddFunctionWithExtraArgs()
  334. {
  335. $this->_server->addFunction('Zend_XmlRpc_Server_testFunction', 'test', 'arg1');
  336. $methods = $this->_server->listMethods();
  337. $this->assertContains('test.Zend_XmlRpc_Server_testFunction', $methods);
  338. }
  339. public function testAddFunctionThrowsExceptionWithBadData()
  340. {
  341. $o = new stdClass();
  342. try {
  343. $this->_server->addFunction($o);
  344. $this->fail('addFunction() should not accept objects');
  345. } catch (Exception $e) {
  346. // success
  347. }
  348. }
  349. public function testLoadFunctionsThrowsExceptionWithBadData()
  350. {
  351. $o = new stdClass();
  352. try {
  353. $this->_server->loadFunctions($o);
  354. $this->fail('loadFunctions() should not accept objects');
  355. } catch (Exception $e) {
  356. // success
  357. }
  358. $o = array($o);
  359. try {
  360. $this->_server->loadFunctions($o);
  361. $this->fail('loadFunctions() should not allow non-reflection objects in an array');
  362. } catch (Exception $e) {
  363. // success
  364. }
  365. }
  366. public function testSetClassThrowsExceptionWithInvalidClass()
  367. {
  368. try {
  369. $this->_server->setClass('mybogusclass');
  370. $this->fail('setClass() should not allow invalid classes');
  371. } catch (Exception $e) {
  372. // success
  373. }
  374. }
  375. public function testSetRequestUsingString()
  376. {
  377. $this->_server->setRequest('Zend_XmlRpc_Server_testRequest');
  378. $req = $this->_server->getRequest();
  379. $this->assertTrue($req instanceof Zend_XmlRpc_Server_testRequest);
  380. }
  381. public function testSetRequestThrowsExceptionOnBadClass()
  382. {
  383. try {
  384. $this->_server->setRequest('Zend_XmlRpc_Server_testRequest2');
  385. $this->fail('Invalid request class should throw exception');
  386. } catch (Exception $e) {
  387. // success
  388. }
  389. try {
  390. $this->_server->setRequest($this);
  391. $this->fail('Invalid request object should throw exception');
  392. } catch (Exception $e) {
  393. // success
  394. }
  395. }
  396. public function testHandleObjectMethod()
  397. {
  398. $this->_server->setClass('Zend_XmlRpc_Server_testClass');
  399. $request = new Zend_XmlRpc_Request();
  400. $request->setMethod('test1');
  401. $request->addParam('value');
  402. $response = $this->_server->handle($request);
  403. $this->assertFalse($response instanceof Zend_XmlRpc_Fault);
  404. $this->assertEquals('String: value', $response->getReturnValue());
  405. }
  406. public function testHandleClassStaticMethod()
  407. {
  408. $this->_server->setClass('Zend_XmlRpc_Server_testClass');
  409. $request = new Zend_XmlRpc_Request();
  410. $request->setMethod('test2');
  411. $request->addParam(array('value1', 'value2'));
  412. $response = $this->_server->handle($request);
  413. $this->assertFalse($response instanceof Zend_XmlRpc_Fault);
  414. $this->assertEquals('value1; value2', $response->getReturnValue());
  415. }
  416. public function testHandleFunction()
  417. {
  418. $this->_server->addFunction('Zend_XmlRpc_Server_testFunction');
  419. $request = new Zend_XmlRpc_Request();
  420. $request->setMethod('Zend_XmlRpc_Server_testFunction');
  421. $request->setParams(array(array('value1'), 'key'));
  422. $response = $this->_server->handle($request);
  423. $this->assertFalse($response instanceof Zend_XmlRpc_Fault);
  424. $this->assertEquals('key: value1', $response->getReturnValue());
  425. }
  426. public function testMulticallReturnsFaultsWithBadData()
  427. {
  428. // bad method array
  429. $try = array(
  430. 'system.listMethods',
  431. array(
  432. 'name' => 'system.listMethods'
  433. ),
  434. array(
  435. 'methodName' => 'system.listMethods'
  436. ),
  437. array(
  438. 'methodName' => 'system.listMethods',
  439. 'params' => ''
  440. ),
  441. array(
  442. 'methodName' => 'system.multicall',
  443. 'params' => array()
  444. )
  445. );
  446. $returned = $this->_server->multicall($try);
  447. $this->assertTrue(is_array($returned));
  448. $this->assertEquals(5, count($returned));
  449. $response = $returned[0];
  450. $this->assertTrue(is_array($response));
  451. $this->assertTrue(isset($response['faultCode']));
  452. $this->assertEquals(601, $response['faultCode']);
  453. $response = $returned[1];
  454. $this->assertTrue(is_array($response));
  455. $this->assertTrue(isset($response['faultCode']));
  456. $this->assertEquals(602, $response['faultCode']);
  457. $response = $returned[2];
  458. $this->assertTrue(is_array($response));
  459. $this->assertTrue(isset($response['faultCode']));
  460. $this->assertEquals(603, $response['faultCode']);
  461. $response = $returned[3];
  462. $this->assertTrue(is_array($response));
  463. $this->assertTrue(isset($response['faultCode']));
  464. $this->assertEquals(604, $response['faultCode']);
  465. $response = $returned[4];
  466. $this->assertTrue(is_array($response));
  467. $this->assertTrue(isset($response['faultCode']));
  468. $this->assertEquals(605, $response['faultCode']);
  469. }
  470. /**
  471. * @see ZF-2872
  472. */
  473. public function testCanMarshalBase64Requests()
  474. {
  475. $this->_server->setClass('Zend_XmlRpc_Server_testClass', 'test');
  476. $data = base64_encode('this is the payload');
  477. $param = array('type' => 'base64', 'value' => $data);
  478. $request = new Zend_XmlRpc_Request('test.base64', array($param));
  479. $response = $this->_server->handle($request);
  480. $this->assertFalse($response instanceof Zend_XmlRpc_Fault);
  481. $this->assertEquals($data, $response->getReturnValue());
  482. }
  483. /**
  484. * @group ZF-6034
  485. */
  486. public function testPrototypeReturnValueMustReflectDocBlock()
  487. {
  488. $server = new Zend_XmlRpc_Server();
  489. $server->setClass('Zend_XmlRpc_Server_testClass');
  490. $table = $server->getDispatchTable();
  491. $method = $table->getMethod('test1');
  492. foreach ($method->getPrototypes() as $prototype) {
  493. $this->assertNotEquals('void', $prototype->getReturnType(), var_export($prototype, 1));
  494. }
  495. }
  496. }
  497. /**
  498. * Zend_XmlRpc_Server_testFunction
  499. *
  500. * Function for use with xmlrpc server unit tests
  501. *
  502. * @param array $var1
  503. * @param string $var2
  504. * @return string
  505. */
  506. function Zend_XmlRpc_Server_testFunction($var1, $var2 = 'optional')
  507. {
  508. return $var2 . ': ' . implode(',', (array) $var1);
  509. }
  510. /**
  511. * Zend_XmlRpc_Server_testFunction2
  512. *
  513. * Function for use with xmlrpc server unit tests
  514. *
  515. * @return string
  516. */
  517. function Zend_XmlRpc_Server_testFunction2()
  518. {
  519. return 'function2';
  520. }
  521. class Zend_XmlRpc_Server_testClass
  522. {
  523. /**
  524. * Constructor
  525. *
  526. * @return void
  527. */
  528. public function __construct()
  529. {
  530. }
  531. /**
  532. * Test1
  533. *
  534. * Returns 'String: ' . $string
  535. *
  536. * @param string $string
  537. * @return string
  538. */
  539. public function test1($string)
  540. {
  541. return 'String: ' . (string) $string;
  542. }
  543. /**
  544. * Test2
  545. *
  546. * Returns imploded array
  547. *
  548. * @param array $array
  549. * @return string
  550. */
  551. public static function test2($array)
  552. {
  553. return implode('; ', (array) $array);
  554. }
  555. /**
  556. * Test3
  557. *
  558. * Should not be available...
  559. *
  560. * @return void
  561. */
  562. protected function _test3()
  563. {
  564. }
  565. /**
  566. * Test base64 encoding in request and response
  567. *
  568. * @param base64 $data
  569. * @return base64
  570. */
  571. public function base64($data)
  572. {
  573. return $data;
  574. }
  575. }
  576. class Zend_XmlRpc_Server_testResponse extends Zend_XmlRpc_Response
  577. {
  578. }
  579. class Zend_XmlRpc_Server_testRequest extends Zend_XmlRpc_Request
  580. {
  581. }
  582. // Call Zend_XmlRpc_ServerTest::main() if this source file is executed directly.
  583. if (PHPUnit_MAIN_METHOD == "Zend_XmlRpc_ServerTest::main") {
  584. Zend_XmlRpc_ServerTest::main();
  585. }