ExceptionHandlerFake.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace Illuminate\Support\Testing\Fakes;
  3. use Closure;
  4. use Illuminate\Contracts\Debug\ExceptionHandler;
  5. use Illuminate\Foundation\Testing\Concerns\WithoutExceptionHandlingHandler;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Traits\ForwardsCalls;
  8. use Illuminate\Support\Traits\ReflectsClosures;
  9. use Illuminate\Testing\Assert;
  10. use PHPUnit\Framework\Assert as PHPUnit;
  11. use PHPUnit\Framework\ExpectationFailedException;
  12. use Throwable;
  13. /**
  14. * @mixin \Illuminate\Foundation\Exceptions\Handler
  15. */
  16. class ExceptionHandlerFake implements ExceptionHandler, Fake
  17. {
  18. use ForwardsCalls, ReflectsClosures;
  19. /**
  20. * All of the exceptions that have been reported.
  21. *
  22. * @var list<\Throwable>
  23. */
  24. protected $reported = [];
  25. /**
  26. * If the fake should throw exceptions when they are reported.
  27. *
  28. * @var bool
  29. */
  30. protected $throwOnReport = false;
  31. /**
  32. * Create a new exception handler fake.
  33. *
  34. * @param \Illuminate\Contracts\Debug\ExceptionHandler $handler
  35. * @param list<class-string<\Throwable>> $exceptions
  36. */
  37. public function __construct(
  38. protected ExceptionHandler $handler,
  39. protected array $exceptions = [],
  40. ) {
  41. //
  42. }
  43. /**
  44. * Get the underlying handler implementation.
  45. *
  46. * @return \Illuminate\Contracts\Debug\ExceptionHandler
  47. */
  48. public function handler()
  49. {
  50. return $this->handler;
  51. }
  52. /**
  53. * Assert if an exception of the given type has been reported.
  54. *
  55. * @param (\Closure(\Throwable): bool)|class-string<\Throwable> $exception
  56. * @return void
  57. */
  58. public function assertReported(Closure|string $exception)
  59. {
  60. $message = sprintf(
  61. 'The expected [%s] exception was not reported.',
  62. is_string($exception) ? $exception : $this->firstClosureParameterType($exception)
  63. );
  64. if (is_string($exception)) {
  65. Assert::assertTrue(
  66. in_array($exception, array_map(get_class(...), $this->reported), true),
  67. $message,
  68. );
  69. return;
  70. }
  71. Assert::assertTrue(
  72. (new Collection($this->reported))->contains(
  73. fn (Throwable $e) => $this->firstClosureParameterType($exception) === get_class($e)
  74. && $exception($e) === true,
  75. ), $message,
  76. );
  77. }
  78. /**
  79. * Assert the number of exceptions that have been reported.
  80. *
  81. * @param int $count
  82. * @return void
  83. */
  84. public function assertReportedCount(int $count)
  85. {
  86. $total = (new Collection($this->reported))->count();
  87. PHPUnit::assertSame(
  88. $count, $total,
  89. "The total number of exceptions reported was {$total} instead of {$count}."
  90. );
  91. }
  92. /**
  93. * Assert if an exception of the given type has not been reported.
  94. *
  95. * @param (\Closure(\Throwable): bool)|class-string<\Throwable> $exception
  96. * @return void
  97. */
  98. public function assertNotReported(Closure|string $exception)
  99. {
  100. try {
  101. $this->assertReported($exception);
  102. } catch (ExpectationFailedException) {
  103. return;
  104. }
  105. throw new ExpectationFailedException(sprintf(
  106. 'The expected [%s] exception was reported.',
  107. is_string($exception) ? $exception : $this->firstClosureParameterType($exception)
  108. ));
  109. }
  110. /**
  111. * Assert nothing has been reported.
  112. *
  113. * @return void
  114. */
  115. public function assertNothingReported()
  116. {
  117. Assert::assertEmpty(
  118. $this->reported,
  119. sprintf(
  120. 'The following exceptions were reported: %s.',
  121. implode(', ', array_map(get_class(...), $this->reported)),
  122. ),
  123. );
  124. }
  125. /**
  126. * Report or log an exception.
  127. *
  128. * @param \Throwable $e
  129. * @return void
  130. */
  131. public function report($e)
  132. {
  133. if (! $this->isFakedException($e)) {
  134. $this->handler->report($e);
  135. return;
  136. }
  137. if (! $this->shouldReport($e)) {
  138. return;
  139. }
  140. $this->reported[] = $e;
  141. if ($this->throwOnReport) {
  142. throw $e;
  143. }
  144. }
  145. /**
  146. * Determine if the given exception is faked.
  147. *
  148. * @param \Throwable $e
  149. * @return bool
  150. */
  151. protected function isFakedException(Throwable $e)
  152. {
  153. return count($this->exceptions) === 0 || in_array(get_class($e), $this->exceptions, true);
  154. }
  155. /**
  156. * Determine if the exception should be reported.
  157. *
  158. * @param \Throwable $e
  159. * @return bool
  160. */
  161. public function shouldReport($e)
  162. {
  163. return $this->runningWithoutExceptionHandling() || $this->handler->shouldReport($e);
  164. }
  165. /**
  166. * Determine if the handler is running without exception handling.
  167. *
  168. * @return bool
  169. */
  170. protected function runningWithoutExceptionHandling()
  171. {
  172. return $this->handler instanceof WithoutExceptionHandlingHandler;
  173. }
  174. /**
  175. * Render an exception into an HTTP response.
  176. *
  177. * @param \Illuminate\Http\Request $request
  178. * @param \Throwable $e
  179. * @return \Symfony\Component\HttpFoundation\Response
  180. */
  181. public function render($request, $e)
  182. {
  183. return $this->handler->render($request, $e);
  184. }
  185. /**
  186. * Render an exception to the console.
  187. *
  188. * @param \Symfony\Component\Console\Output\OutputInterface $output
  189. * @param \Throwable $e
  190. * @return void
  191. */
  192. public function renderForConsole($output, Throwable $e)
  193. {
  194. $this->handler->renderForConsole($output, $e);
  195. }
  196. /**
  197. * Throw exceptions when they are reported.
  198. *
  199. * @return $this
  200. */
  201. public function throwOnReport()
  202. {
  203. $this->throwOnReport = true;
  204. return $this;
  205. }
  206. /**
  207. * Throw the first reported exception.
  208. *
  209. * @return $this
  210. *
  211. * @throws \Throwable
  212. */
  213. public function throwFirstReported()
  214. {
  215. foreach ($this->reported as $e) {
  216. throw $e;
  217. }
  218. return $this;
  219. }
  220. /**
  221. * Get the exceptions that have been reported.
  222. *
  223. * @return list<\Throwable>
  224. */
  225. public function reported()
  226. {
  227. return $this->reported;
  228. }
  229. /**
  230. * Set the "original" handler that should be used by the fake.
  231. *
  232. * @param \Illuminate\Contracts\Debug\ExceptionHandler $handler
  233. * @return $this
  234. */
  235. public function setHandler(ExceptionHandler $handler)
  236. {
  237. $this->handler = $handler;
  238. return $this;
  239. }
  240. /**
  241. * Handle dynamic method calls to the handler.
  242. *
  243. * @param string $method
  244. * @param array<string, mixed> $parameters
  245. * @return mixed
  246. */
  247. public function __call(string $method, array $parameters)
  248. {
  249. return $this->forwardCallTo($this->handler, $method, $parameters);
  250. }
  251. }