Debian Codegraph @

lib/3rdParty/composer/illuminate/support/Testing/Fakes/ExceptionHandlerFake.php

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