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.
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
23 // Call Zend_Json_ServerTest::main() if this source file is executed directly.
24 if (!defined("PHPUnit_MAIN_METHOD")) {
25 define("PHPUnit_MAIN_METHOD", "Zend_Json_ServerTest::main");
28 require_once dirname(__FILE__
) . '/../../TestHelper.php';
30 require_once 'Zend/Json/Server.php';
31 require_once 'Zend/Json/Server/Request.php';
32 require_once 'Zend/Json/Server/Response.php';
33 require_once 'Zend/Json.php';
36 * Test class for Zend_Json_Server
39 * @package Zend_Json_Server
40 * @subpackage UnitTests
41 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
42 * @license http://framework.zend.com/license/new-bsd New BSD License
44 * @group Zend_Json_Server
46 class Zend_Json_ServerTest
extends PHPUnit_Framework_TestCase
49 * Runs the test methods of this class.
53 public static function main()
55 require_once "PHPUnit/TextUI/TestRunner.php";
57 $suite = new PHPUnit_Framework_TestSuite("Zend_Json_ServerTest");
58 $result = PHPUnit_TextUI_TestRunner
::run($suite);
62 * Sets up the fixture, for example, open a network connection.
63 * This method is called before a test is executed.
67 public function setUp()
69 $this->server
= new Zend_Json_Server();
73 * Tears down the fixture, for example, close a network connection.
74 * This method is called after a test is executed.
78 public function tearDown()
82 public function testShouldBeAbleToBindFunctionToServer()
84 $this->server
->addFunction('strtolower');
85 $methods = $this->server
->getFunctions();
86 $this->assertTrue($methods->hasMethod('strtolower'));
89 public function testShouldBeAbleToBindCallbackToServer()
91 $this->server
->addFunction(array($this, 'setUp'));
92 $methods = $this->server
->getFunctions();
93 $this->assertTrue($methods->hasMethod('setUp'));
96 public function testShouldBeAbleToBindClassToServer()
98 $this->server
->setClass('Zend_Json_Server');
99 $test = $this->server
->getFunctions();
100 $this->assertTrue(0 < count($test));
103 public function testBindingClassToServerShouldRegisterAllPublicMethods()
105 $this->server
->setClass('Zend_Json_Server');
106 $test = $this->server
->getFunctions();
107 $methods = get_class_methods('Zend_Json_Server');
108 foreach ($methods as $method) {
109 if ('_' == $method[0]) {
112 $this->assertTrue($test->hasMethod($method), 'Testing for method ' . $method . ' against ' . var_export($test, 1));
116 public function testShouldBeAbleToBindObjectToServer()
118 $object = new Zend_Json_Server();
119 $this->server
->setClass($object);
120 $test = $this->server
->getFunctions();
121 $this->assertTrue(0 < count($test));
124 public function testBindingObjectToServerShouldRegisterAllPublicMethods()
126 $object = new Zend_Json_Server();
127 $this->server
->setClass($object);
128 $test = $this->server
->getFunctions();
129 $methods = get_class_methods($object);
130 foreach ($methods as $method) {
131 if ('_' == $method[0]) {
134 $this->assertTrue($test->hasMethod($method), 'Testing for method ' . $method . ' against ' . var_export($test, 1));
138 public function testShouldBeAbleToBindMultipleClassesAndObjectsToServer()
140 $this->server
->setClass('Zend_Json_Server')
141 ->setClass(new Zend_Json());
142 $methods = $this->server
->getFunctions();
143 $zjsMethods = get_class_methods('Zend_Json_Server');
144 $zjMethods = get_class_methods('Zend_Json');
145 $this->assertTrue(count($zjsMethods) < count($methods));
146 $this->assertTrue(count($zjMethods) < count($methods));
149 public function testNamingCollisionsShouldResolveToLastRegisteredMethod()
151 $this->server
->setClass('Zend_Json_Server_Request')
152 ->setClass('Zend_Json_Server_Response');
153 $methods = $this->server
->getFunctions();
154 $this->assertTrue($methods->hasMethod('toJson'));
155 $toJson = $methods->getMethod('toJson');
156 $this->assertEquals('Zend_Json_Server_Response', $toJson->getCallback()->getClass());
159 public function testGetRequestShouldInstantiateRequestObjectByDefault()
161 $request = $this->server
->getRequest();
162 $this->assertTrue($request instanceof Zend_Json_Server_Request
);
165 public function testShouldAllowSettingRequestObjectManually()
167 $orig = $this->server
->getRequest();
168 $new = new Zend_Json_Server_Request();
169 $this->server
->setRequest($new);
170 $test = $this->server
->getRequest();
171 $this->assertSame($new, $test);
172 $this->assertNotSame($orig, $test);
175 public function testGetResponseShouldInstantiateResponseObjectByDefault()
177 $response = $this->server
->getResponse();
178 $this->assertTrue($response instanceof Zend_Json_Server_Response
);
181 public function testShouldAllowSettingResponseObjectManually()
183 $orig = $this->server
->getResponse();
184 $new = new Zend_Json_Server_Response();
185 $this->server
->setResponse($new);
186 $test = $this->server
->getResponse();
187 $this->assertSame($new, $test);
188 $this->assertNotSame($orig, $test);
191 public function testFaultShouldCreateErrorResponse()
193 $response = $this->server
->getResponse();
194 $this->assertFalse($response->isError());
195 $this->server
->fault('error condition', -32000);
196 $this->assertTrue($response->isError());
197 $error = $response->getError();
198 $this->assertEquals(-32000, $error->getCode());
199 $this->assertEquals('error condition', $error->getMessage());
202 public function testResponseShouldBeEmittedAutomaticallyByDefault()
204 $this->assertTrue($this->server
->autoEmitResponse());
207 public function testShouldBeAbleToDisableAutomaticResponseEmission()
209 $this->testResponseShouldBeEmittedAutomaticallyByDefault();
210 $this->server
->setAutoEmitResponse(false);
211 $this->assertFalse($this->server
->autoEmitResponse());
214 public function testShouldBeAbleToRetrieveSmdObject()
216 $smd = $this->server
->getServiceMap();
217 $this->assertTrue($smd instanceof Zend_Json_Server_Smd
);
220 public function testShouldBeAbleToSetArbitrarySmdMetadata()
222 $this->server
->setTransport('POST')
223 ->setEnvelope('JSON-RPC-1.0')
224 ->setContentType('application/x-json')
225 ->setTarget('/foo/bar')
227 ->setDescription('This is a test service');
229 $this->assertEquals('POST', $this->server
->getTransport());
230 $this->assertEquals('JSON-RPC-1.0', $this->server
->getEnvelope());
231 $this->assertEquals('application/x-json', $this->server
->getContentType());
232 $this->assertEquals('/foo/bar', $this->server
->getTarget());
233 $this->assertEquals('foobar', $this->server
->getId());
234 $this->assertEquals('This is a test service', $this->server
->getDescription());
237 public function testSmdObjectRetrievedFromServerShouldReflectServerState()
239 $this->server
->addFunction('strtolower')
240 ->setClass('Zend_Json_Server')
241 ->setTransport('POST')
242 ->setEnvelope('JSON-RPC-1.0')
243 ->setContentType('application/x-json')
244 ->setTarget('/foo/bar')
246 ->setDescription('This is a test service');
247 $smd = $this->server
->getServiceMap();
248 $this->assertEquals('POST', $this->server
->getTransport());
249 $this->assertEquals('JSON-RPC-1.0', $this->server
->getEnvelope());
250 $this->assertEquals('application/x-json', $this->server
->getContentType());
251 $this->assertEquals('/foo/bar', $this->server
->getTarget());
252 $this->assertEquals('foobar', $this->server
->getId());
253 $this->assertEquals('This is a test service', $this->server
->getDescription());
255 $services = $smd->getServices();
256 $this->assertTrue(is_array($services));
257 $this->assertTrue(0 < count($services));
258 $this->assertTrue(array_key_exists('strtolower', $services));
259 $methods = get_class_methods('Zend_Json_Server');
260 foreach ($methods as $method) {
261 if ('_' == $method[0]) {
264 $this->assertTrue(array_key_exists($method, $services));
268 public function testHandleValidMethodShouldWork()
270 $this->server
->setClass('Zend_Json_ServerTest_Foo')
271 ->addFunction('Zend_Json_ServerTest_FooFunc')
272 ->setAutoEmitResponse(false);
273 $request = $this->server
->getRequest();
274 $request->setMethod('bar')
275 ->setParams(array(true, 'foo', 'bar'))
277 $response = $this->server
->handle();
278 $this->assertTrue($response instanceof Zend_Json_Server_Response
);
279 $this->assertFalse($response->isError());
282 $request->setMethod('Zend_Json_ServerTest_FooFunc')
284 $response = $this->server
->handle();
285 $this->assertTrue($response instanceof Zend_Json_Server_Response
);
286 $this->assertFalse($response->isError());
289 public function testHandleValidMethodWithTooFewParamsShouldPassDefaultsOrNullsForMissingParams()
291 $this->server
->setClass('Zend_Json_ServerTest_Foo')
292 ->setAutoEmitResponse(false);
293 $request = $this->server
->getRequest();
294 $request->setMethod('bar')
295 ->setParams(array(true))
297 $response = $this->server
->handle();
298 $this->assertTrue($response instanceof Zend_Json_Server_Response
);
299 $this->assertFalse($response->isError());
300 $result = $response->getResult();
301 $this->assertTrue(is_array($result));
302 $this->assertTrue(3 == count($result));
303 $this->assertEquals('two', $result[1], var_export($result, 1));
304 $this->assertNull($result[2]);
307 public function testHandleValidMethodWithTooManyParamsShouldWork()
309 $this->server
->setClass('Zend_Json_ServerTest_Foo')
310 ->setAutoEmitResponse(false);
311 $request = $this->server
->getRequest();
312 $request->setMethod('bar')
313 ->setParams(array(true, 'foo', 'bar', 'baz'))
315 $response = $this->server
->handle();
316 $this->assertTrue($response instanceof Zend_Json_Server_Response
);
317 $this->assertFalse($response->isError());
318 $result = $response->getResult();
319 $this->assertTrue(is_array($result));
320 $this->assertTrue(3 == count($result));
321 $this->assertEquals('foo', $result[1]);
322 $this->assertEquals('bar', $result[2]);
325 public function testHandleShouldAllowNamedParamsInAnyOrder1()
327 $this->server
->setClass('Zend_Json_ServerTest_Foo')
328 ->setAutoEmitResponse( false );
329 $request = $this->server
->getRequest();
330 $request->setMethod('bar')
337 $response = $this->server
->handle();
338 $result = $response->getResult();
340 $this->assertTrue( is_array( $result ) );
341 $this->assertEquals( 1, $result[0] );
342 $this->assertEquals( 2, $result[1] );
343 $this->assertEquals( 3, $result[2] );
346 public function testHandleShouldAllowNamedParamsInAnyOrder2()
348 $this->server
->setClass('Zend_Json_ServerTest_Foo')
349 ->setAutoEmitResponse( false );
350 $request = $this->server
->getRequest();
351 $request->setMethod('bar')
358 $response = $this->server
->handle();
359 $result = $response->getResult();
361 $this->assertTrue( is_array( $result ) );
362 $this->assertEquals( 1, $result[0] );
363 $this->assertEquals( 2, $result[1] );
364 $this->assertEquals( 3, $result[2] );
367 public function testHandleRequestWithErrorsShouldReturnErrorResponse()
369 $this->server
->setClass('Zend_Json_ServerTest_Foo')
370 ->setAutoEmitResponse(false);
371 $response = $this->server
->handle();
372 $this->assertTrue($response instanceof Zend_Json_Server_Response
);
373 $this->assertTrue($response->isError());
374 $this->assertEquals(Zend_Json_Server_Error
::ERROR_INVALID_REQUEST
, $response->getError()->getCode());
377 public function testHandleRequestWithInvalidMethodShouldReturnErrorResponse()
379 $this->server
->setClass('Zend_Json_ServerTest_Foo')
380 ->setAutoEmitResponse(false);
381 $request = $this->server
->getRequest();
382 $request->setMethod('bogus')
384 $response = $this->server
->handle();
385 $this->assertTrue($response instanceof Zend_Json_Server_Response
);
386 $this->assertTrue($response->isError());
387 $this->assertEquals(Zend_Json_Server_Error
::ERROR_INVALID_METHOD
, $response->getError()->getCode());
390 public function testHandleRequestWithExceptionShouldReturnErrorResponse()
392 $this->server
->setClass('Zend_Json_ServerTest_Foo')
393 ->setAutoEmitResponse(false);
394 $request = $this->server
->getRequest();
395 $request->setMethod('baz')
397 $response = $this->server
->handle();
398 $this->assertTrue($response instanceof Zend_Json_Server_Response
);
399 $this->assertTrue($response->isError());
400 $this->assertEquals(Zend_Json_Server_Error
::ERROR_OTHER
, $response->getError()->getCode());
401 $this->assertEquals('application error', $response->getError()->getMessage());
404 public function testHandleShouldEmitResponseByDefault()
406 $this->server
->setClass('Zend_Json_ServerTest_Foo');
407 $request = $this->server
->getRequest();
408 $request->setMethod('bar')
409 ->setParams(array(true, 'foo', 'bar'))
412 $this->server
->handle();
413 $buffer = ob_get_clean();
415 $decoded = Zend_Json
::decode($buffer);
416 $this->assertTrue(is_array($decoded));
417 $this->assertTrue(array_key_exists('result', $decoded));
418 $this->assertTrue(array_key_exists('id', $decoded));
420 $response = $this->server
->getResponse();
421 $this->assertEquals($response->getResult(), $decoded['result']);
422 $this->assertEquals($response->getId(), $decoded['id']);
425 public function testResponseShouldBeEmptyWhenRequestHasNoId()
427 $this->server
->setClass('Zend_Json_ServerTest_Foo');
428 $request = $this->server
->getRequest();
429 $request->setMethod('bar')
430 ->setParams(array(true, 'foo', 'bar'));
432 $this->server
->handle();
433 $buffer = ob_get_clean();
435 $this->assertTrue(empty($buffer));
438 public function testLoadFunctionsShouldLoadResultOfGetFunctions()
440 $this->server
->setClass('Zend_Json_ServerTest_Foo');
441 $functions = $this->server
->getFunctions();
442 $server = new Zend_Json_Server();
443 $server->loadFunctions($functions);
444 $this->assertEquals($functions->toArray(), $server->getFunctions()->toArray());
449 * Class for testing JSON-RPC server
451 class Zend_Json_ServerTest_Foo
458 * @param mixed $three
461 public function bar($one, $two = 'two', $three = null)
463 return array($one, $two, $three);
471 public function baz()
473 throw new Exception('application error');
478 * Test function for JSON-RPC server
482 function Zend_Json_ServerTest_FooFunc()
487 // Call Zend_Json_ServerTest::main() if this source file is executed directly.
488 if (PHPUnit_MAIN_METHOD
== "Zend_Json_ServerTest::main") {
489 Zend_Json_ServerTest
::main();