ErrorTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_ErrorTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Json_Server_ErrorTest::main");
  25. }
  26. require_once 'Zend/Json/Server/Error.php';
  27. require_once 'Zend/Json.php';
  28. /**
  29. * Test class for Zend_Json_Server_Error
  30. *
  31. * @category Zend
  32. * @package Zend_Json_Server
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Json
  37. * @group Zend_Json_Server
  38. */
  39. class Zend_Json_Server_ErrorTest extends PHPUnit_Framework_TestCase
  40. {
  41. /**
  42. * Runs the test methods of this class.
  43. *
  44. * @return void
  45. */
  46. public static function main()
  47. {
  48. $suite = new PHPUnit_Framework_TestSuite("Zend_Json_Server_ErrorTest");
  49. $result = PHPUnit_TextUI_TestRunner::run($suite);
  50. }
  51. /**
  52. * Sets up the fixture, for example, open a network connection.
  53. * This method is called before a test is executed.
  54. *
  55. * @return void
  56. */
  57. public function setUp()
  58. {
  59. $this->error = new Zend_Json_Server_Error();
  60. }
  61. /**
  62. * Tears down the fixture, for example, close a network connection.
  63. * This method is called after a test is executed.
  64. *
  65. * @return void
  66. */
  67. public function tearDown()
  68. {
  69. }
  70. public function testCodeShouldBeErrOtherByDefault()
  71. {
  72. $this->assertEquals(Zend_Json_Server_Error::ERROR_OTHER, $this->error->getCode());
  73. }
  74. public function testSetCodeShouldCastToInteger()
  75. {
  76. $this->error->setCode('-32768');
  77. $this->assertEquals(-32768, $this->error->getCode());
  78. }
  79. public function testCodeShouldBeLimitedToStandardIntegers()
  80. {
  81. foreach (array(true, 'foo', array(), new stdClass, 2.0, 25) as $code) {
  82. $this->error->setCode($code);
  83. $this->assertEquals(Zend_Json_Server_Error::ERROR_OTHER, $this->error->getCode());
  84. }
  85. }
  86. public function testCodeShouldAllowArbitraryAppErrorCodesInXmlRpcErrorCodeRange()
  87. {
  88. foreach (range(-32099, -32000) as $code) {
  89. $this->error->setCode($code);
  90. $this->assertEquals($code, $this->error->getCode());
  91. }
  92. }
  93. public function testMessageShouldBeNullByDefault()
  94. {
  95. $this->assertNull($this->error->getMessage());
  96. }
  97. public function testSetMessageShouldCastToString()
  98. {
  99. foreach (array(true, 2.0, 25) as $message) {
  100. $this->error->setMessage($message);
  101. $this->assertEquals((string) $message, $this->error->getMessage());
  102. }
  103. }
  104. public function testSetMessageToNonScalarShouldSilentlyFail()
  105. {
  106. foreach (array(array(), new stdClass) as $message) {
  107. $this->error->setMessage($message);
  108. $this->assertNull($this->error->getMessage());
  109. }
  110. }
  111. public function testDataShouldBeNullByDefault()
  112. {
  113. $this->assertNull($this->error->getData());
  114. }
  115. public function testShouldAllowArbitraryData()
  116. {
  117. foreach (array(true, 'foo', 2, 2.0, array(), new stdClass) as $datum) {
  118. $this->error->setData($datum);
  119. $this->assertEquals($datum, $this->error->getData());
  120. }
  121. }
  122. public function testShouldBeAbleToCastToArray()
  123. {
  124. $this->setupError();
  125. $array = $this->error->toArray();
  126. $this->validateArray($array);
  127. }
  128. public function testShouldBeAbleToCastToJson()
  129. {
  130. $this->setupError();
  131. $json = $this->error->toJson();
  132. $this->validateArray(Zend_Json::decode($json));
  133. }
  134. public function testCastingToStringShouldCastToJson()
  135. {
  136. $this->setupError();
  137. $json = $this->error->__toString();
  138. $this->validateArray(Zend_Json::decode($json));
  139. }
  140. public function setupError()
  141. {
  142. $this->error->setCode(Zend_Json_Server_Error::ERROR_OTHER)
  143. ->setMessage('Unknown Error')
  144. ->setData(array('foo' => 'bar'));
  145. }
  146. public function validateArray($error)
  147. {
  148. $this->assertTrue(is_array($error));
  149. $this->assertTrue(array_key_exists('code', $error));
  150. $this->assertTrue(array_key_exists('message', $error));
  151. $this->assertTrue(array_key_exists('data', $error));
  152. $this->assertTrue(is_int($error['code']));
  153. $this->assertTrue(is_string($error['message']));
  154. $this->assertTrue(is_array($error['data']));
  155. $this->assertEquals($this->error->getCode(), $error['code']);
  156. $this->assertEquals($this->error->getMessage(), $error['message']);
  157. $this->assertSame($this->error->getData(), $error['data']);
  158. }
  159. }
  160. // Call Zend_Json_Server_ErrorTest::main() if this source file is executed directly.
  161. if (PHPUnit_MAIN_METHOD == "Zend_Json_Server_ErrorTest::main") {
  162. Zend_Json_Server_ErrorTest::main();
  163. }