ResponseTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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-2010 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_Server_ResponseTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Json_Server_ResponseTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  27. require_once 'Zend/Json/Server/Response.php';
  28. require_once 'Zend/Json/Server/Error.php';
  29. require_once 'Zend/Json.php';
  30. /**
  31. * Test class for Zend_Json_Server_Response
  32. *
  33. * @category Zend
  34. * @package Zend_Json_Server
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2010 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_Server_ResponseTest 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. require_once "PHPUnit/TextUI/TestRunner.php";
  51. $suite = new PHPUnit_Framework_TestSuite("Zend_Json_Server_ResponseTest");
  52. $result = PHPUnit_TextUI_TestRunner::run($suite);
  53. }
  54. /**
  55. * Sets up the fixture, for example, open a network connection.
  56. * This method is called before a test is executed.
  57. *
  58. * @return void
  59. */
  60. public function setUp()
  61. {
  62. $this->response = new Zend_Json_Server_Response();
  63. }
  64. /**
  65. * Tears down the fixture, for example, close a network connection.
  66. * This method is called after a test is executed.
  67. *
  68. * @return void
  69. */
  70. public function tearDown()
  71. {
  72. }
  73. public function testResultShouldBeNullByDefault()
  74. {
  75. $this->assertNull($this->response->getResult());
  76. }
  77. public function testResultAccessorsShouldWorkWithNormalInput()
  78. {
  79. foreach (array(true, 'foo', 2, 2.0, array(), array('foo' => 'bar')) as $result) {
  80. $this->response->setResult($result);
  81. $this->assertEquals($result, $this->response->getResult());
  82. }
  83. }
  84. public function testResultShouldNotBeErrorByDefault()
  85. {
  86. $this->assertFalse($this->response->isError());
  87. }
  88. public function testSettingErrorShouldMarkRequestAsError()
  89. {
  90. $error = new Zend_Json_Server_Error();
  91. $this->response->setError($error);
  92. $this->assertTrue($this->response->isError());
  93. }
  94. public function testShouldBeAbleToRetrieveErrorObject()
  95. {
  96. $error = new Zend_Json_Server_Error();
  97. $this->response->setError($error);
  98. $this->assertSame($error, $this->response->getError());
  99. }
  100. public function testIdShouldBeNullByDefault()
  101. {
  102. $this->assertNull($this->response->getId());
  103. }
  104. public function testIdAccesorsShouldWorkWithNormalInput()
  105. {
  106. $this->response->setId('foo');
  107. $this->assertEquals('foo', $this->response->getId());
  108. }
  109. public function testVersionShouldBeNullByDefault()
  110. {
  111. $this->assertNull($this->response->getVersion());
  112. }
  113. public function testVersionShouldBeLimitedToV2()
  114. {
  115. $this->response->setVersion('2.0');
  116. $this->assertEquals('2.0', $this->response->getVersion());
  117. foreach (array('a', 1, '1.0', array(), true) as $version) {
  118. $this->response->setVersion($version);
  119. $this->assertNull($this->response->getVersion());
  120. }
  121. }
  122. public function testResponseShouldBeAbleToCastToJson()
  123. {
  124. $this->response->setResult(true)
  125. ->setId('foo')
  126. ->setVersion('2.0');
  127. $json = $this->response->toJson();
  128. $test = Zend_Json::decode($json);
  129. $this->assertTrue(is_array($test));
  130. $this->assertTrue(array_key_exists('result', $test));
  131. $this->assertTrue(array_key_exists('error', $test));
  132. $this->assertTrue(array_key_exists('id', $test));
  133. $this->assertTrue(array_key_exists('jsonrpc', $test));
  134. $this->assertTrue($test['result']);
  135. $this->assertEquals($this->response->getId(), $test['id']);
  136. $this->assertEquals($this->response->getVersion(), $test['jsonrpc']);
  137. }
  138. public function testResponseShouldCastErrorToJsonIfIsError()
  139. {
  140. $error = new Zend_Json_Server_Error();
  141. $error->setCode(Zend_Json_Server_Error::ERROR_INTERNAL)
  142. ->setMessage('error occurred');
  143. $this->response->setId('foo')
  144. ->setResult(true)
  145. ->setError($error);
  146. $json = $this->response->toJson();
  147. $test = Zend_Json::decode($json);
  148. $this->assertTrue(is_array($test));
  149. $this->assertTrue(array_key_exists('result', $test));
  150. $this->assertTrue(array_key_exists('id', $test));
  151. $this->assertFalse(array_key_exists('jsonrpc', $test));
  152. $this->assertEquals($this->response->getId(), $test['id']);
  153. $this->assertEquals($error->getCode(), $test['error']['code']);
  154. $this->assertEquals($error->getMessage(), $test['error']['message']);
  155. }
  156. public function testCastToStringShouldCastToJson()
  157. {
  158. $this->response->setResult(true)
  159. ->setId('foo');
  160. $json = $this->response->__toString();
  161. $test = Zend_Json::decode($json);
  162. $this->assertTrue(is_array($test));
  163. $this->assertTrue(array_key_exists('result', $test));
  164. $this->assertTrue(array_key_exists('error', $test));
  165. $this->assertTrue(array_key_exists('id', $test));
  166. $this->assertFalse(array_key_exists('jsonrpc', $test));
  167. $this->assertTrue($test['result']);
  168. $this->assertEquals($this->response->getId(), $test['id']);
  169. }
  170. }
  171. // Call Zend_Json_Server_ResponseTest::main() if this source file is executed directly.
  172. if (PHPUnit_MAIN_METHOD == "Zend_Json_Server_ResponseTest::main") {
  173. Zend_Json_Server_ResponseTest::main();
  174. }