ServerTest.php 15 KB

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