ResponseTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. // Call Zend_Json_Server_ResponseTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Json_Server_ResponseTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. require_once 'Zend/Json/Server/Response.php';
  8. require_once 'Zend/Json/Server/Error.php';
  9. require_once 'Zend/Json.php';
  10. /**
  11. * Test class for Zend_Json_Server_Response
  12. */
  13. class Zend_Json_Server_ResponseTest extends PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * Runs the test methods of this class.
  17. *
  18. * @return void
  19. */
  20. public static function main()
  21. {
  22. require_once "PHPUnit/TextUI/TestRunner.php";
  23. $suite = new PHPUnit_Framework_TestSuite("Zend_Json_Server_ResponseTest");
  24. $result = PHPUnit_TextUI_TestRunner::run($suite);
  25. }
  26. /**
  27. * Sets up the fixture, for example, open a network connection.
  28. * This method is called before a test is executed.
  29. *
  30. * @return void
  31. */
  32. public function setUp()
  33. {
  34. $this->response = new Zend_Json_Server_Response();
  35. }
  36. /**
  37. * Tears down the fixture, for example, close a network connection.
  38. * This method is called after a test is executed.
  39. *
  40. * @return void
  41. */
  42. public function tearDown()
  43. {
  44. }
  45. public function testResultShouldBeNullByDefault()
  46. {
  47. $this->assertNull($this->response->getResult());
  48. }
  49. public function testResultAccessorsShouldWorkWithNormalInput()
  50. {
  51. foreach (array(true, 'foo', 2, 2.0, array(), array('foo' => 'bar')) as $result) {
  52. $this->response->setResult($result);
  53. $this->assertEquals($result, $this->response->getResult());
  54. }
  55. }
  56. public function testResultShouldNotBeErrorByDefault()
  57. {
  58. $this->assertFalse($this->response->isError());
  59. }
  60. public function testSettingErrorShouldMarkRequestAsError()
  61. {
  62. $error = new Zend_Json_Server_Error();
  63. $this->response->setError($error);
  64. $this->assertTrue($this->response->isError());
  65. }
  66. public function testShouldBeAbleToRetrieveErrorObject()
  67. {
  68. $error = new Zend_Json_Server_Error();
  69. $this->response->setError($error);
  70. $this->assertSame($error, $this->response->getError());
  71. }
  72. public function testIdShouldBeNullByDefault()
  73. {
  74. $this->assertNull($this->response->getId());
  75. }
  76. public function testIdAccesorsShouldWorkWithNormalInput()
  77. {
  78. $this->response->setId('foo');
  79. $this->assertEquals('foo', $this->response->getId());
  80. }
  81. public function testVersionShouldBeNullByDefault()
  82. {
  83. $this->assertNull($this->response->getVersion());
  84. }
  85. public function testVersionShouldBeLimitedToV2()
  86. {
  87. $this->response->setVersion('2.0');
  88. $this->assertEquals('2.0', $this->response->getVersion());
  89. foreach (array('a', 1, '1.0', array(), true) as $version) {
  90. $this->response->setVersion($version);
  91. $this->assertNull($this->response->getVersion());
  92. }
  93. }
  94. public function testResponseShouldBeAbleToCastToJson()
  95. {
  96. $this->response->setResult(true)
  97. ->setId('foo')
  98. ->setVersion('2.0');
  99. $json = $this->response->toJson();
  100. $test = Zend_Json::decode($json);
  101. $this->assertTrue(is_array($test));
  102. $this->assertTrue(array_key_exists('result', $test));
  103. $this->assertFalse(array_key_exists('error', $test));
  104. $this->assertTrue(array_key_exists('id', $test));
  105. $this->assertTrue(array_key_exists('jsonrpc', $test));
  106. $this->assertTrue($test['result']);
  107. $this->assertEquals($this->response->getId(), $test['id']);
  108. $this->assertEquals($this->response->getVersion(), $test['jsonrpc']);
  109. }
  110. public function testResponseShouldCastErrorToJsonIfIsError()
  111. {
  112. $error = new Zend_Json_Server_Error();
  113. $error->setCode(Zend_Json_Server_Error::ERROR_INTERNAL)
  114. ->setMessage('error occurred');
  115. $this->response->setId('foo')
  116. ->setResult(true)
  117. ->setError($error);
  118. $json = $this->response->toJson();
  119. $test = Zend_Json::decode($json);
  120. $this->assertTrue(is_array($test));
  121. $this->assertFalse(array_key_exists('result', $test));
  122. $this->assertTrue(array_key_exists('id', $test));
  123. $this->assertFalse(array_key_exists('jsonrpc', $test));
  124. $this->assertEquals($this->response->getId(), $test['id']);
  125. $this->assertEquals($error->getCode(), $test['error']['code']);
  126. $this->assertEquals($error->getMessage(), $test['error']['message']);
  127. }
  128. public function testCastToStringShouldCastToJson()
  129. {
  130. $this->response->setResult(true)
  131. ->setId('foo');
  132. $json = $this->response->__toString();
  133. $test = Zend_Json::decode($json);
  134. $this->assertTrue(is_array($test));
  135. $this->assertTrue(array_key_exists('result', $test));
  136. $this->assertFalse(array_key_exists('error', $test));
  137. $this->assertTrue(array_key_exists('id', $test));
  138. $this->assertFalse(array_key_exists('jsonrpc', $test));
  139. $this->assertTrue($test['result']);
  140. $this->assertEquals($this->response->getId(), $test['id']);
  141. }
  142. }
  143. // Call Zend_Json_Server_ResponseTest::main() if this source file is executed directly.
  144. if (PHPUnit_MAIN_METHOD == "Zend_Json_Server_ResponseTest::main") {
  145. Zend_Json_Server_ResponseTest::main();
  146. }