ServerTest.php 17 KB

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