PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Json/ServerTest.php

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