ErrorTest.php 5.7 KB

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