ErrorTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. // Call Zend_Json_Server_ErrorTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Json_Server_ErrorTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. require_once 'Zend/Json/Server/Error.php';
  8. require_once 'Zend/Json.php';
  9. /**
  10. * Test class for Zend_Json_Server_Error
  11. */
  12. class Zend_Json_Server_ErrorTest extends PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * Runs the test methods of this class.
  16. *
  17. * @return void
  18. */
  19. public static function main()
  20. {
  21. require_once "PHPUnit/TextUI/TestRunner.php";
  22. $suite = new PHPUnit_Framework_TestSuite("Zend_Json_Server_ErrorTest");
  23. $result = PHPUnit_TextUI_TestRunner::run($suite);
  24. }
  25. /**
  26. * Sets up the fixture, for example, open a network connection.
  27. * This method is called before a test is executed.
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. $this->error = new Zend_Json_Server_Error();
  34. }
  35. /**
  36. * Tears down the fixture, for example, close a network connection.
  37. * This method is called after a test is executed.
  38. *
  39. * @return void
  40. */
  41. public function tearDown()
  42. {
  43. }
  44. public function testCodeShouldBeErrOtherByDefault()
  45. {
  46. $this->assertEquals(Zend_Json_Server_Error::ERROR_OTHER, $this->error->getCode());
  47. }
  48. public function testSetCodeShouldCastToInteger()
  49. {
  50. $this->error->setCode('-32768');
  51. $this->assertEquals(-32768, $this->error->getCode());
  52. }
  53. public function testCodeShouldBeLimitedToStandardIntegers()
  54. {
  55. foreach (array(true, 'foo', array(), new stdClass, 2.0, 25) as $code) {
  56. $this->error->setCode($code);
  57. $this->assertEquals(Zend_Json_Server_Error::ERROR_OTHER, $this->error->getCode());
  58. }
  59. }
  60. public function testCodeShouldAllowArbitraryAppErrorCodesInXmlRpcErrorCodeRange()
  61. {
  62. foreach (range(-32099, -32000) as $code) {
  63. $this->error->setCode($code);
  64. $this->assertEquals($code, $this->error->getCode());
  65. }
  66. }
  67. public function testMessageShouldBeNullByDefault()
  68. {
  69. $this->assertNull($this->error->getMessage());
  70. }
  71. public function testSetMessageShouldCastToString()
  72. {
  73. foreach (array(true, 2.0, 25) as $message) {
  74. $this->error->setMessage($message);
  75. $this->assertEquals((string) $message, $this->error->getMessage());
  76. }
  77. }
  78. public function testSetMessageToNonScalarShouldSilentlyFail()
  79. {
  80. foreach (array(array(), new stdClass) as $message) {
  81. $this->error->setMessage($message);
  82. $this->assertNull($this->error->getMessage());
  83. }
  84. }
  85. public function testDataShouldBeNullByDefault()
  86. {
  87. $this->assertNull($this->error->getData());
  88. }
  89. public function testShouldAllowArbitraryData()
  90. {
  91. foreach (array(true, 'foo', 2, 2.0, array(), new stdClass) as $datum) {
  92. $this->error->setData($datum);
  93. $this->assertEquals($datum, $this->error->getData());
  94. }
  95. }
  96. public function testShouldBeAbleToCastToArray()
  97. {
  98. $this->setupError();
  99. $array = $this->error->toArray();
  100. $this->validateArray($array);
  101. }
  102. public function testShouldBeAbleToCastToJson()
  103. {
  104. $this->setupError();
  105. $json = $this->error->toJson();
  106. $this->validateArray(Zend_Json::decode($json));
  107. }
  108. public function testCastingToStringShouldCastToJson()
  109. {
  110. $this->setupError();
  111. $json = $this->error->__toString();
  112. $this->validateArray(Zend_Json::decode($json));
  113. }
  114. public function setupError()
  115. {
  116. $this->error->setCode(Zend_Json_Server_Error::ERROR_OTHER)
  117. ->setMessage('Unknown Error')
  118. ->setData(array('foo' => 'bar'));
  119. }
  120. public function validateArray($error)
  121. {
  122. $this->assertTrue(is_array($error));
  123. $this->assertTrue(array_key_exists('code', $error));
  124. $this->assertTrue(array_key_exists('message', $error));
  125. $this->assertTrue(array_key_exists('data', $error));
  126. $this->assertTrue(is_int($error['code']));
  127. $this->assertTrue(is_string($error['message']));
  128. $this->assertTrue(is_array($error['data']));
  129. $this->assertEquals($this->error->getCode(), $error['code']);
  130. $this->assertEquals($this->error->getMessage(), $error['message']);
  131. $this->assertSame($this->error->getData(), $error['data']);
  132. }
  133. }
  134. // Call Zend_Json_Server_ErrorTest::main() if this source file is executed directly.
  135. if (PHPUnit_MAIN_METHOD == "Zend_Json_Server_ErrorTest::main") {
  136. Zend_Json_Server_ErrorTest::main();
  137. }