ResponseTest.php 6.6 KB

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