Idle.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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\Idle as SUT;
  37. use Hoa\Test;
  38. /**
  39. * Class \Hoa\Exception\Test\Unit\Idle.
  40. *
  41. * Test suite of the idle exception.
  42. *
  43. * @copyright Copyright © 2007-2017 Hoa community
  44. * @license New BSD License
  45. */
  46. class Idle extends Test\Unit\Suite
  47. {
  48. public function case_is_a_real_exception()
  49. {
  50. $this
  51. ->when($result = new SUT('foo'))
  52. ->then
  53. ->object($result)
  54. ->isInstanceOf('Exception');
  55. }
  56. public function case_get_backtrace()
  57. {
  58. $this
  59. ->given($exception = new SUT('foo'))
  60. ->when($result = $exception->getBacktrace())
  61. ->then
  62. ->array($result)
  63. ->hasKey(0)
  64. ->array($result[0])
  65. ->hasKey('file')
  66. ->hasKey('line')
  67. ->hasKey('function')
  68. ->hasKey('class')
  69. ->hasKey('type')
  70. ->hasKey('args');
  71. }
  72. public function case_get_previous_throw()
  73. {
  74. $this
  75. ->given(
  76. $previous = new SUT('previous'),
  77. $exception = new SUT('foo', 0, [], $previous)
  78. )
  79. ->when($result = $exception->getPreviousThrow())
  80. ->then
  81. ->object($result)
  82. ->isIdenticalTo($previous);
  83. }
  84. public function case_get_arguments()
  85. {
  86. $this
  87. ->given($exception = new SUT('foo', 0, ['arg', 42, null]))
  88. ->when($result = $exception->getArguments())
  89. ->then
  90. ->array($result)
  91. ->isEqualTo(['arg', 42, '(null)']);
  92. }
  93. public function case_get_arguments_from_a_string()
  94. {
  95. $this
  96. ->given($exception = new SUT('foo', 0, 'arg'))
  97. ->when($result = $exception->getArguments())
  98. ->then
  99. ->array($result)
  100. ->isEqualTo(['arg']);
  101. }
  102. public function case_get_raw_message()
  103. {
  104. $this
  105. ->given(
  106. $message = 'foo %s',
  107. $exception = new SUT($message)
  108. )
  109. ->when($result = $exception->getRawMessage())
  110. ->then
  111. ->string($result)
  112. ->isEqualTo($message);
  113. }
  114. public function case_get_formatted_message()
  115. {
  116. $this
  117. ->given(
  118. $message = 'foo %s',
  119. $exception = new SUT($message, 0, 'bar')
  120. )
  121. ->when($result = $exception->getFormattedMessage())
  122. ->then
  123. ->string($result)
  124. ->isEqualTo($exception->getMessage())
  125. ->isEqualTo('foo bar');
  126. }
  127. public function case_get_from_object()
  128. {
  129. $this
  130. ->given($exception = new SUT('foo'))
  131. ->when($result = $exception->getFrom())
  132. ->then
  133. ->string($result)
  134. ->isEqualTo(__METHOD__ . '()');
  135. }
  136. public function case_raise()
  137. {
  138. $this
  139. ->given($exception = new SUT('foo'), $line = __LINE__)
  140. ->when($result = $exception->raise())
  141. ->then
  142. ->string($result)
  143. ->isEqualTo(
  144. __METHOD__ . '(): (0) foo' . "\n" .
  145. 'in ' . __FILE__ . ' at line ' . $line . '.'
  146. );
  147. }
  148. public function case_raise_with_previous()
  149. {
  150. $this
  151. ->given(
  152. $previous = new SUT('previous'), $previousLine = __LINE__,
  153. $exception = new SUT('foo', 0, [], $previous), $line = __LINE__
  154. )
  155. ->when($result = $exception->raise(true))
  156. ->then
  157. ->string($result)
  158. ->isEqualTo(
  159. __METHOD__ . '(): (0) foo' . "\n" .
  160. 'in ' . __FILE__ . ' at line ' . $line . '.' . "\n\n" .
  161. ' ⬇' . "\n\n" .
  162. 'Nested exception (' . get_class($previous) . '):' . "\n" .
  163. __METHOD__ . '(): (0) previous' . "\n" .
  164. 'in ' . __FILE__ . ' at line ' . $previousLine . '.'
  165. );
  166. }
  167. public function case_uncaught()
  168. {
  169. $this
  170. ->given(
  171. $this->function->ob_get_level = 0,
  172. $exception = new SUT('foo'), $line = __LINE__
  173. )
  174. ->when($result = SUT::uncaught($exception))
  175. ->then
  176. ->variable($result)
  177. ->isNull()
  178. ->output
  179. ->isEqualTo(
  180. 'Uncaught exception (' . get_class($exception) . '):' . "\n" .
  181. __METHOD__ . '(): (0) foo' . "\n" .
  182. 'in ' . __FILE__ . ' at line ' . $line . '.'
  183. );
  184. }
  185. public function case_uncaught_not_Hoa()
  186. {
  187. $this
  188. ->exception(function () {
  189. SUT::uncaught(new \Exception('foo'));
  190. })
  191. ->isInstanceOf('Exception')
  192. ->output
  193. ->isEmpty();
  194. }
  195. public function case_to_string()
  196. {
  197. $this
  198. ->given($exception = new SUT('foo'))
  199. ->when($result = $exception->__toString())
  200. ->then
  201. ->string($result)
  202. ->isEqualTo($exception->raise());
  203. }
  204. public function case_disable_uncaught_handler()
  205. {
  206. $this
  207. ->given(
  208. $this->function->restore_exception_handler = function () use (&$called) {
  209. $called = true;
  210. return null;
  211. }
  212. )
  213. ->when($result = SUT::enableUncaughtHandler(false))
  214. ->then
  215. ->variable($result)
  216. ->isNull()
  217. ->boolean($called)
  218. ->isTrue();
  219. }
  220. public function case_enable_uncaught_handler()
  221. {
  222. $self = $this;
  223. $this
  224. ->given(
  225. $this->function->set_exception_handler = function ($handler) use ($self, &$called) {
  226. $called = true;
  227. $self
  228. ->object($handler)
  229. ->isInstanceOf('Closure')
  230. ->let($reflection = new \ReflectionObject($handler))
  231. ->array($invokeParameters = $reflection->getMethod('__invoke')->getParameters())
  232. ->hasSize(1)
  233. ->string($invokeParameters[0]->getName())
  234. ->isEqualTo('exception');
  235. return null;
  236. }
  237. )
  238. ->when($result = SUT::enableUncaughtHandler())
  239. ->then
  240. ->variable($result)
  241. ->isNull()
  242. ->boolean($called)
  243. ->isTrue();
  244. }
  245. }