| 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 |
|
| 18 |
|
| 19 |
class ExceptionHandlerFake implements ExceptionHandler, Fake |
| 20 |
{ |
| 21 |
use ForwardsCalls, ReflectsClosures; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
protected $reported = []; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
protected $throwOnReport = false; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function __construct( |
| 44 |
protected ExceptionHandler $handler, |
| 45 |
protected array $exceptions = [], |
| 46 |
) { |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function handler() |
| 56 |
{ |
| 57 |
return $this->handler; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 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 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 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 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 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 |
|
| 128 |
|
| 129 |
|
| 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 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 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 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 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 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public function shouldReport($e) |
| 185 |
{ |
| 186 |
return $this->runningWithoutExceptionHandling() || $this->handler->shouldReport($e); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
protected function runningWithoutExceptionHandling() |
| 195 |
{ |
| 196 |
return $this->handler instanceof WithoutExceptionHandlingHandler; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public function render($request, $e) |
| 207 |
{ |
| 208 |
return $this->handler->render($request, $e); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public function renderForConsole($output, Throwable $e) |
| 219 |
{ |
| 220 |
$this->handler->renderForConsole($output, $e); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public function throwOnReport() |
| 229 |
{ |
| 230 |
$this->throwOnReport = true; |
| 231 |
|
| 232 |
return $this; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public function throwFirstReported() |
| 243 |
{ |
| 244 |
foreach ($this->reported as $e) { |
| 245 |
throw $e; |
| 246 |
} |
| 247 |
|
| 248 |
return $this; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public function reported() |
| 257 |
{ |
| 258 |
return $this->reported; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public function setHandler(ExceptionHandler $handler) |
| 268 |
{ |
| 269 |
$this->handler = $handler; |
| 270 |
|
| 271 |
return $this; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
public function __call(string $method, array $parameters) |
| 282 |
{ |
| 283 |
return $this->forwardCallTo($this->handler, $method, $parameters); |
| 284 |
} |
| 285 |
} |