ServerTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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-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_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-2009 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 testHandleRequestWithErrorsShouldReturnErrorResponse()
  293. {
  294. $this->server->setClass('Zend_Json_ServerTest_Foo')
  295. ->setAutoEmitResponse(false);
  296. $response = $this->server->handle();
  297. $this->assertTrue($response instanceof Zend_Json_Server_Response);
  298. $this->assertTrue($response->isError());
  299. $this->assertEquals(Zend_Json_Server_Error::ERROR_INVALID_REQUEST, $response->getError()->getCode());
  300. }
  301. public function testHandleRequestWithInvalidMethodShouldReturnErrorResponse()
  302. {
  303. $this->server->setClass('Zend_Json_ServerTest_Foo')
  304. ->setAutoEmitResponse(false);
  305. $request = $this->server->getRequest();
  306. $request->setMethod('bogus')
  307. ->setId('foo');
  308. $response = $this->server->handle();
  309. $this->assertTrue($response instanceof Zend_Json_Server_Response);
  310. $this->assertTrue($response->isError());
  311. $this->assertEquals(Zend_Json_Server_Error::ERROR_INVALID_METHOD, $response->getError()->getCode());
  312. }
  313. public function testHandleRequestWithExceptionShouldReturnErrorResponse()
  314. {
  315. $this->server->setClass('Zend_Json_ServerTest_Foo')
  316. ->setAutoEmitResponse(false);
  317. $request = $this->server->getRequest();
  318. $request->setMethod('baz')
  319. ->setId('foo');
  320. $response = $this->server->handle();
  321. $this->assertTrue($response instanceof Zend_Json_Server_Response);
  322. $this->assertTrue($response->isError());
  323. $this->assertEquals(Zend_Json_Server_Error::ERROR_OTHER, $response->getError()->getCode());
  324. $this->assertEquals('application error', $response->getError()->getMessage());
  325. }
  326. public function testHandleShouldEmitResponseByDefault()
  327. {
  328. $this->server->setClass('Zend_Json_ServerTest_Foo');
  329. $request = $this->server->getRequest();
  330. $request->setMethod('bar')
  331. ->setParams(array(true, 'foo', 'bar'))
  332. ->setId('foo');
  333. ob_start();
  334. $this->server->handle();
  335. $buffer = ob_get_clean();
  336. $decoded = Zend_Json::decode($buffer);
  337. $this->assertTrue(is_array($decoded));
  338. $this->assertTrue(array_key_exists('result', $decoded));
  339. $this->assertTrue(array_key_exists('id', $decoded));
  340. $response = $this->server->getResponse();
  341. $this->assertEquals($response->getResult(), $decoded['result']);
  342. $this->assertEquals($response->getId(), $decoded['id']);
  343. }
  344. public function testResponseShouldBeEmptyWhenRequestHasNoId()
  345. {
  346. $this->server->setClass('Zend_Json_ServerTest_Foo');
  347. $request = $this->server->getRequest();
  348. $request->setMethod('bar')
  349. ->setParams(array(true, 'foo', 'bar'));
  350. ob_start();
  351. $this->server->handle();
  352. $buffer = ob_get_clean();
  353. $this->assertTrue(empty($buffer));
  354. }
  355. public function testLoadFunctionsShouldLoadResultOfGetFunctions()
  356. {
  357. $this->server->setClass('Zend_Json_ServerTest_Foo');
  358. $functions = $this->server->getFunctions();
  359. $server = new Zend_Json_Server();
  360. $server->loadFunctions($functions);
  361. $this->assertEquals($functions->toArray(), $server->getFunctions()->toArray());
  362. }
  363. }
  364. /**
  365. * Class for testing JSON-RPC server
  366. */
  367. class Zend_Json_ServerTest_Foo
  368. {
  369. /**
  370. * Bar
  371. *
  372. * @param bool $one
  373. * @param string $two
  374. * @param mixed $three
  375. * @return array
  376. */
  377. public function bar($one, $two = 'two', $three = null)
  378. {
  379. return array($one, $two, $three);
  380. }
  381. /**
  382. * Baz
  383. *
  384. * @return void
  385. */
  386. public function baz()
  387. {
  388. throw new Exception('application error');
  389. }
  390. }
  391. /**
  392. * Test function for JSON-RPC server
  393. *
  394. * @return bool
  395. */
  396. function Zend_Json_ServerTest_FooFunc()
  397. {
  398. return true;
  399. }
  400. // Call Zend_Json_ServerTest::main() if this source file is executed directly.
  401. if (PHPUnit_MAIN_METHOD == "Zend_Json_ServerTest::main") {
  402. Zend_Json_ServerTest::main();
  403. }