Error.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Exception\Test\Unit;
  36. use Hoa\Exception\Error as SUT;
  37. use Hoa\Test;
  38. /**
  39. * Class \Hoa\Exception\Test\Unit\Error.
  40. *
  41. * Test suite of the error class.
  42. *
  43. * @copyright Copyright © 2007-2017 Hoa community
  44. * @license New BSD License
  45. */
  46. class Error extends Test\Unit\Suite
  47. {
  48. public function case_is_an_exception()
  49. {
  50. $this
  51. ->when($result = new SUT('foo', 42, '/hoa/flatland', 153))
  52. ->then
  53. ->object($result)
  54. ->isInstanceOf('Hoa\Exception\Exception');
  55. }
  56. public function case_get_message()
  57. {
  58. $this
  59. ->given($exception = new SUT('foo', 42, '/hoa/flatland', 153))
  60. ->when($result = $exception->raise())
  61. ->then
  62. ->string($result)
  63. ->isEqualTo(
  64. '{main}: (42) foo' . "\n" .
  65. 'in /hoa/flatland at line 153.'
  66. );
  67. }
  68. public function case_disable_error_handler()
  69. {
  70. $this
  71. ->given(
  72. $this->function->restore_error_handler = function () use (&$called) {
  73. $called = true;
  74. return null;
  75. }
  76. )
  77. ->when($result = SUT::enableErrorHandler(false))
  78. ->then
  79. ->variable($result)
  80. ->isNull()
  81. ->boolean($called)
  82. ->isTrue();
  83. }
  84. public function case_enable_error_handler()
  85. {
  86. $self = $this;
  87. $this
  88. ->given(
  89. $this->function->set_error_handler = function ($handler) use ($self, &$called) {
  90. $called = true;
  91. $self
  92. ->object($handler)
  93. ->isInstanceOf('Closure')
  94. ->let($reflection = new \ReflectionObject($handler))
  95. ->array($invokeParameters = $reflection->getMethod('__invoke')->getParameters())
  96. ->hasSize(5)
  97. ->string($invokeParameters[0]->getName())
  98. ->isEqualTo('no')
  99. ->string($invokeParameters[1]->getName())
  100. ->isEqualTo('str')
  101. ->string($invokeParameters[2]->getName())
  102. ->isEqualTo('file')
  103. ->boolean($invokeParameters[2]->isOptional())
  104. ->isTrue()
  105. ->string($invokeParameters[3]->getName())
  106. ->isEqualTo('line')
  107. ->boolean($invokeParameters[3]->isOptional())
  108. ->isTrue()
  109. ->string($invokeParameters[4]->getName())
  110. ->isEqualTo('ctx')
  111. ->boolean($invokeParameters[4]->isOptional())
  112. ->isTrue();
  113. return null;
  114. }
  115. )
  116. ->when($result = SUT::enableErrorHandler())
  117. ->then
  118. ->variable($result)
  119. ->isNull()
  120. ->boolean($called)
  121. ->isTrue();
  122. }
  123. public function case_error_handler()
  124. {
  125. $this
  126. ->given(SUT::enableErrorHandler())
  127. ->exception(function () {
  128. ++$i;
  129. })
  130. ->isInstanceOf('Hoa\Exception\Error')
  131. ->hasMessage('Undefined variable: i');
  132. }
  133. }