ServerTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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_Json_Server
  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. // Call Zend_Json_ServerTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Json_ServerTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../TestHelper.php';
  27. require_once 'Zend/Json/Server.php';
  28. require_once 'Zend/Json/Server/Request.php';
  29. require_once 'Zend/Json/Server/Response.php';
  30. require_once 'Zend/Json.php';
  31. /**
  32. * Test class for Zend_Json_Server
  33. *
  34. * @category Zend
  35. * @package Zend_Json_Server
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @group Zend_Json
  40. * @group Zend_Json_Server
  41. */
  42. class Zend_Json_ServerTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * Runs the test methods of this class.
  46. *
  47. * @return void
  48. */
  49. public static function main()
  50. {
  51. require_once "PHPUnit/TextUI/TestRunner.php";
  52. $suite = new PHPUnit_Framework_TestSuite("Zend_Json_ServerTest");
  53. $result = PHPUnit_TextUI_TestRunner::run($suite);
  54. }
  55. /**
  56. * Sets up the fixture, for example, open a network connection.
  57. * This method is called before a test is executed.
  58. *
  59. * @return void
  60. */
  61. public function setUp()
  62. {
  63. $this->server = new Zend_Json_Server();
  64. }
  65. /**
  66. * Tears down the fixture, for example, close a network connection.
  67. * This method is called after a test is executed.
  68. *
  69. * @return void
  70. */
  71. public function tearDown()
  72. {
  73. }
  74. public function testShouldBeAbleToBindFunctionToServer()
  75. {
  76. $this->server->addFunction('strtolower');
  77. $methods = $this->server->getFunctions();
  78. $this->assertTrue($methods->hasMethod('strtolower'));
  79. }
  80. public function testShouldBeAbleToBindCallbackToServer()
  81. {
  82. $this->server->addFunction(array($this, 'setUp'));
  83. $methods = $this->server->getFunctions();
  84. $this->assertTrue($methods->hasMethod('setUp'));
  85. }
  86. public function testShouldBeAbleToBindClassToServer()
  87. {
  88. $this->server->setClass('Zend_Json_Server');
  89. $test = $this->server->getFunctions();
  90. $this->assertTrue(0 < count($test));
  91. }
  92. public function testBindingClassToServerShouldRegisterAllPublicMethods()
  93. {
  94. $this->server->setClass('Zend_Json_Server');
  95. $test = $this->server->getFunctions();
  96. $methods = get_class_methods('Zend_Json_Server');
  97. foreach ($methods as $method) {
  98. if ('_' == $method[0]) {
  99. continue;
  100. }
  101. $this->assertTrue($test->hasMethod($method), 'Testing for method ' . $method . ' against ' . var_export($test, 1));
  102. }
  103. }
  104. public function testShouldBeAbleToBindObjectToServer()
  105. {
  106. $object = new Zend_Json_Server();
  107. $this->server->setClass($object);
  108. $test = $this->server->getFunctions();
  109. $this->assertTrue(0 < count($test));
  110. }
  111. public function testBindingObjectToServerShouldRegisterAllPublicMethods()
  112. {
  113. $object = new Zend_Json_Server();
  114. $this->server->setClass($object);
  115. $test = $this->server->getFunctions();
  116. $methods = get_class_methods($object);
  117. foreach ($methods as $method) {
  118. if ('_' == $method[0]) {
  119. continue;
  120. }
  121. $this->assertTrue($test->hasMethod($method), 'Testing for method ' . $method . ' against ' . var_export($test, 1));
  122. }
  123. }
  124. public function testShouldBeAbleToBindMultipleClassesAndObjectsToServer()
  125. {
  126. $this->server->setClass('Zend_Json_Server')
  127. ->setClass(new Zend_Json());
  128. $methods = $this->server->getFunctions();
  129. $zjsMethods = get_class_methods('Zend_Json_Server');
  130. $zjMethods = get_class_methods('Zend_Json');
  131. $this->assertTrue(count($zjsMethods) < count($methods));
  132. $this->assertTrue(count($zjMethods) < count($methods));
  133. }
  134. public function testNamingCollisionsShouldResolveToLastRegisteredMethod()
  135. {
  136. $this->server->setClass('Zend_Json_Server_Request')
  137. ->setClass('Zend_Json_Server_Response');
  138. $methods = $this->server->getFunctions();
  139. $this->assertTrue($methods->hasMethod('toJson'));
  140. $toJson = $methods->getMethod('toJson');
  141. $this->assertEquals('Zend_Json_Server_Response', $toJson->getCallback()->getClass());
  142. }
  143. public function testGetRequestShouldInstantiateRequestObjectByDefault()
  144. {
  145. $request = $this->server->getRequest();
  146. $this->assertTrue($request instanceof Zend_Json_Server_Request);
  147. }
  148. public function testShouldAllowSettingRequestObjectManually()
  149. {
  150. $orig = $this->server->getRequest();
  151. $new = new Zend_Json_Server_Request();
  152. $this->server->setRequest($new);
  153. $test = $this->server->getRequest();
  154. $this->assertSame($new, $test);
  155. $this->assertNotSame($orig, $test);
  156. }
  157. public function testGetResponseShouldInstantiateResponseObjectByDefault()
  158. {
  159. $response = $this->server->getResponse();
  160. $this->assertTrue($response instanceof Zend_Json_Server_Response);
  161. }
  162. public function testShouldAllowSettingResponseObjectManually()
  163. {
  164. $orig = $this->server->getResponse();
  165. $new = new Zend_Json_Server_Response();
  166. $this->server->setResponse($new);
  167. $test = $this->server->getResponse();
  168. $this->assertSame($new, $test);
  169. $this->assertNotSame($orig, $test);
  170. }
  171. public function testFaultShouldCreateErrorResponse()
  172. {
  173. $response = $this->server->getResponse();
  174. $this->assertFalse($response->isError());
  175. $this->server->fault('error condition', -32000);
  176. $this->assertTrue($response->isError());
  177. $error = $response->getError();
  178. $this->assertEquals(-32000, $error->getCode());
  179. $this->assertEquals('error condition', $error->getMessage());
  180. }
  181. public function testResponseShouldBeEmittedAutomaticallyByDefault()
  182. {
  183. $this->assertTrue($this->server->autoEmitResponse());
  184. }
  185. public function testShouldBeAbleToDisableAutomaticResponseEmission()
  186. {
  187. $this->testResponseShouldBeEmittedAutomaticallyByDefault();
  188. $this->server->setAutoEmitResponse(false);
  189. $this->assertFalse($this->server->autoEmitResponse());
  190. }
  191. public function testShouldBeAbleToRetrieveSmdObject()
  192. {
  193. $smd = $this->server->getServiceMap();
  194. $this->assertTrue($smd instanceof Zend_Json_Server_Smd);
  195. }
  196. public function testShouldBeAbleToSetArbitrarySmdMetadata()
  197. {
  198. $this->server->setTransport('POST')
  199. ->setEnvelope('JSON-RPC-1.0')
  200. ->setContentType('application/x-json')
  201. ->setTarget('/foo/bar')
  202. ->setId('foobar')
  203. ->setDescription('This is a test service');
  204. $this->assertEquals('POST', $this->server->getTransport());
  205. $this->assertEquals('JSON-RPC-1.0', $this->server->getEnvelope());
  206. $this->assertEquals('application/x-json', $this->server->getContentType());
  207. $this->assertEquals('/foo/bar', $this->server->getTarget());
  208. $this->assertEquals('foobar', $this->server->getId());
  209. $this->assertEquals('This is a test service', $this->server->getDescription());
  210. }
  211. public function testSmdObjectRetrievedFromServerShouldReflectServerState()
  212. {
  213. $this->server->addFunction('strtolower')
  214. ->setClass('Zend_Json_Server')
  215. ->setTransport('POST')
  216. ->setEnvelope('JSON-RPC-1.0')
  217. ->setContentType('application/x-json')
  218. ->setTarget('/foo/bar')
  219. ->setId('foobar')
  220. ->setDescription('This is a test service');
  221. $smd = $this->server->getServiceMap();
  222. $this->assertEquals('POST', $this->server->getTransport());
  223. $this->assertEquals('JSON-RPC-1.0', $this->server->getEnvelope());
  224. $this->assertEquals('application/x-json', $this->server->getContentType());
  225. $this->assertEquals('/foo/bar', $this->server->getTarget());
  226. $this->assertEquals('foobar', $this->server->getId());
  227. $this->assertEquals('This is a test service', $this->server->getDescription());
  228. $services = $smd->getServices();
  229. $this->assertTrue(is_array($services));
  230. $this->assertTrue(0 < count($services));
  231. $this->assertTrue(array_key_exists('strtolower', $services));
  232. $methods = get_class_methods('Zend_Json_Server');
  233. foreach ($methods as $method) {
  234. if ('_' == $method[0]) {
  235. continue;
  236. }
  237. $this->assertTrue(array_key_exists($method, $services));
  238. }
  239. }
  240. public function testHandleValidMethodShouldWork()
  241. {
  242. $this->server->setClass('Zend_Json_ServerTest_Foo')
  243. ->addFunction('Zend_Json_ServerTest_FooFunc')
  244. ->setAutoEmitResponse(false);
  245. $request = $this->server->getRequest();
  246. $request->setMethod('bar')
  247. ->setParams(array(true, 'foo', 'bar'))
  248. ->setId('foo');
  249. $response = $this->server->handle();
  250. $this->assertTrue($response instanceof Zend_Json_Server_Response);
  251. $this->assertFalse($response->isError());
  252. $request->setMethod('Zend_Json_ServerTest_FooFunc')
  253. ->setId('foo');
  254. $response = $this->server->handle();
  255. $this->assertTrue($response instanceof Zend_Json_Server_Response);
  256. $this->assertFalse($response->isError());
  257. }
  258. public function testHandleValidMethodWithTooFewParamsShouldPassDefaultsOrNullsForMissingParams()
  259. {
  260. $this->server->setClass('Zend_Json_ServerTest_Foo')
  261. ->setAutoEmitResponse(false);
  262. $request = $this->server->getRequest();
  263. $request->setMethod('bar')
  264. ->setParams(array(true))
  265. ->setId('foo');
  266. $response = $this->server->handle();
  267. $this->assertTrue($response instanceof Zend_Json_Server_Response);
  268. $this->assertFalse($response->isError());
  269. $result = $response->getResult();
  270. $this->assertTrue(is_array($result));
  271. $this->assertTrue(3 == count($result));
  272. $this->assertEquals('two', $result[1], var_export($result, 1));
  273. $this->assertNull($result[2]);
  274. }
  275. public function testHandleValidMethodWithTooManyParamsShouldWork()
  276. {
  277. $this->server->setClass('Zend_Json_ServerTest_Foo')
  278. ->setAutoEmitResponse(false);
  279. $request = $this->server->getRequest();
  280. $request->setMethod('bar')
  281. ->setParams(array(true, 'foo', 'bar', 'baz'))
  282. ->setId('foo');
  283. $response = $this->server->handle();
  284. $this->assertTrue($response instanceof Zend_Json_Server_Response);
  285. $this->assertFalse($response->isError());
  286. $result = $response->getResult();
  287. $this->assertTrue(is_array($result));
  288. $this->assertTrue(3 == count($result));
  289. $this->assertEquals('foo', $result[1]);
  290. $this->assertEquals('bar', $result[2]);
  291. }
  292. public function testHandleShouldAllowNamedParamsInAnyOrder1()
  293. {
  294. $this->server->setClass('Zend_Json_ServerTest_Foo')
  295. ->setAutoEmitResponse( false );
  296. $request = $this->server->getRequest();
  297. $request->setMethod('bar')
  298. ->setParams( array(
  299. 'three' => 3,
  300. 'two' => 2,
  301. 'one' => 1
  302. ))
  303. ->setId( 'foo' );
  304. $response = $this->server->handle();
  305. $result = $response->getResult();
  306. $this->assertTrue( is_array( $result ) );
  307. $this->assertEquals( 1, $result[0] );
  308. $this->assertEquals( 2, $result[1] );
  309. $this->assertEquals( 3, $result[2] );
  310. }
  311. public function testHandleShouldAllowNamedParamsInAnyOrder2()
  312. {
  313. $this->server->setClass('Zend_Json_ServerTest_Foo')
  314. ->setAutoEmitResponse( false );
  315. $request = $this->server->getRequest();
  316. $request->setMethod('bar')
  317. ->setParams( array(
  318. 'three' => 3,
  319. 'one' => 1,
  320. 'two' => 2,
  321. ) )
  322. ->setId( 'foo' );
  323. $response = $this->server->handle();
  324. $result = $response->getResult();
  325. $this->assertTrue( is_array( $result ) );
  326. $this->assertEquals( 1, $result[0] );
  327. $this->assertEquals( 2, $result[1] );
  328. $this->assertEquals( 3, $result[2] );
  329. }
  330. public function testHandleRequestWithErrorsShouldReturnErrorResponse()
  331. {
  332. $this->server->setClass('Zend_Json_ServerTest_Foo')
  333. ->setAutoEmitResponse(false);
  334. $response = $this->server->handle();
  335. $this->assertTrue($response instanceof Zend_Json_Server_Response);
  336. $this->assertTrue($response->isError());
  337. $this->assertEquals(Zend_Json_Server_Error::ERROR_INVALID_REQUEST, $response->getError()->getCode());
  338. }
  339. public function testHandleRequestWithInvalidMethodShouldReturnErrorResponse()
  340. {
  341. $this->server->setClass('Zend_Json_ServerTest_Foo')
  342. ->setAutoEmitResponse(false);
  343. $request = $this->server->getRequest();
  344. $request->setMethod('bogus')
  345. ->setId('foo');
  346. $response = $this->server->handle();
  347. $this->assertTrue($response instanceof Zend_Json_Server_Response);
  348. $this->assertTrue($response->isError());
  349. $this->assertEquals(Zend_Json_Server_Error::ERROR_INVALID_METHOD, $response->getError()->getCode());
  350. }
  351. public function testHandleRequestWithExceptionShouldReturnErrorResponse()
  352. {
  353. $this->server->setClass('Zend_Json_ServerTest_Foo')
  354. ->setAutoEmitResponse(false);
  355. $request = $this->server->getRequest();
  356. $request->setMethod('baz')
  357. ->setId('foo');
  358. $response = $this->server->handle();
  359. $this->assertTrue($response instanceof Zend_Json_Server_Response);
  360. $this->assertTrue($response->isError());
  361. $this->assertEquals(Zend_Json_Server_Error::ERROR_OTHER, $response->getError()->getCode());
  362. $this->assertEquals('application error', $response->getError()->getMessage());
  363. }
  364. public function testHandleShouldEmitResponseByDefault()
  365. {
  366. $this->server->setClass('Zend_Json_ServerTest_Foo');
  367. $request = $this->server->getRequest();
  368. $request->setMethod('bar')
  369. ->setParams(array(true, 'foo', 'bar'))
  370. ->setId('foo');
  371. ob_start();
  372. $this->server->handle();
  373. $buffer = ob_get_clean();
  374. $decoded = Zend_Json::decode($buffer);
  375. $this->assertTrue(is_array($decoded));
  376. $this->assertTrue(array_key_exists('result', $decoded));
  377. $this->assertTrue(array_key_exists('id', $decoded));
  378. $response = $this->server->getResponse();
  379. $this->assertEquals($response->getResult(), $decoded['result']);
  380. $this->assertEquals($response->getId(), $decoded['id']);
  381. }
  382. public function testResponseShouldBeEmptyWhenRequestHasNoId()
  383. {
  384. $this->server->setClass('Zend_Json_ServerTest_Foo');
  385. $request = $this->server->getRequest();
  386. $request->setMethod('bar')
  387. ->setParams(array(true, 'foo', 'bar'));
  388. ob_start();
  389. $this->server->handle();
  390. $buffer = ob_get_clean();
  391. $this->assertTrue(empty($buffer));
  392. }
  393. public function testLoadFunctionsShouldLoadResultOfGetFunctions()
  394. {
  395. $this->server->setClass('Zend_Json_ServerTest_Foo');
  396. $functions = $this->server->getFunctions();
  397. $server = new Zend_Json_Server();
  398. $server->loadFunctions($functions);
  399. $this->assertEquals($functions->toArray(), $server->getFunctions()->toArray());
  400. }
  401. }
  402. /**
  403. * Class for testing JSON-RPC server
  404. */
  405. class Zend_Json_ServerTest_Foo
  406. {
  407. /**
  408. * Bar
  409. *
  410. * @param bool $one
  411. * @param string $two
  412. * @param mixed $three
  413. * @return array
  414. */
  415. public function bar($one, $two = 'two', $three = null)
  416. {
  417. return array($one, $two, $three);
  418. }
  419. /**
  420. * Baz
  421. *
  422. * @return void
  423. */
  424. public function baz()
  425. {
  426. throw new Exception('application error');
  427. }
  428. }
  429. /**
  430. * Test function for JSON-RPC server
  431. *
  432. * @return bool
  433. */
  434. function Zend_Json_ServerTest_FooFunc()
  435. {
  436. return true;
  437. }
  438. // Call Zend_Json_ServerTest::main() if this source file is executed directly.
  439. if (PHPUnit_MAIN_METHOD == "Zend_Json_ServerTest::main") {
  440. Zend_Json_ServerTest::main();
  441. }