Skip to content

Commit d3a051a

Browse files
nunomaduroStyleCIBotdriesvintstimacdonaldtaylorotwell
authored
[11.x] Introduces Exceptions facade (#50704)
* Initial work on `Exceptions` facade * Apply fixes from StyleCI * Allows to fake expecific exceptions * Adds `throwOnReport` and `throwReported` * Apply fixes from StyleCI * Fixes reporting of regular errors like 404 or 302 * More tests * More tests * Update src/Illuminate/Support/Testing/Fakes/ExceptionHandlerFake.php Co-authored-by: Tim MacDonald <[email protected]> * Update src/Illuminate/Support/Testing/Fakes/ExceptionHandlerFake.php Co-authored-by: Tim MacDonald <[email protected]> * Adjusts code * formatting * marker interface * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot <[email protected]> Co-authored-by: Dries Vints <[email protected]> Co-authored-by: Tim MacDonald <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 7d40721 commit d3a051a

File tree

5 files changed

+965
-4
lines changed

5 files changed

+965
-4
lines changed

src/Illuminate/Foundation/Testing/Concerns/InteractsWithExceptionHandling.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Closure;
66
use Illuminate\Contracts\Debug\ExceptionHandler;
7+
use Illuminate\Support\Facades\Exceptions;
8+
use Illuminate\Support\Testing\Fakes\ExceptionHandlerFake;
79
use Illuminate\Testing\Assert;
810
use Illuminate\Validation\ValidationException;
911
use Symfony\Component\Console\Application as ConsoleApplication;
@@ -27,7 +29,11 @@ trait InteractsWithExceptionHandling
2729
protected function withExceptionHandling()
2830
{
2931
if ($this->originalExceptionHandler) {
30-
$this->app->instance(ExceptionHandler::class, $this->originalExceptionHandler);
32+
$currentExceptionHandler = app(ExceptionHandler::class);
33+
34+
$currentExceptionHandler instanceof ExceptionHandlerFake
35+
? $currentExceptionHandler->setHandler($this->originalExceptionHandler)
36+
: $this->app->instance(ExceptionHandler::class, $this->originalExceptionHandler);
3137
}
3238

3339
return $this;
@@ -63,10 +69,14 @@ protected function handleValidationExceptions()
6369
protected function withoutExceptionHandling(array $except = [])
6470
{
6571
if ($this->originalExceptionHandler == null) {
66-
$this->originalExceptionHandler = app(ExceptionHandler::class);
72+
$currentExceptionHandler = app(ExceptionHandler::class);
73+
74+
$this->originalExceptionHandler = $currentExceptionHandler instanceof ExceptionHandlerFake
75+
? $currentExceptionHandler->handler()
76+
: $currentExceptionHandler;
6777
}
6878

69-
$this->app->instance(ExceptionHandler::class, new class($this->originalExceptionHandler, $except) implements ExceptionHandler
79+
$exceptionHandler = new class($this->originalExceptionHandler, $except) implements ExceptionHandler, WithoutExceptionHandlingHandler
7080
{
7181
protected $except;
7282
protected $originalHandler;
@@ -145,7 +155,13 @@ public function renderForConsole($output, Throwable $e)
145155
{
146156
(new ConsoleApplication)->renderThrowable($e, $output);
147157
}
148-
});
158+
};
159+
160+
$currentExceptionHandler = app(ExceptionHandler::class);
161+
162+
$currentExceptionHandler instanceof ExceptionHandlerFake
163+
? $currentExceptionHandler->setHandler($exceptionHandler)
164+
: $this->app->instance(ExceptionHandler::class, $exceptionHandler);
149165

150166
return $this;
151167
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation\Testing\Concerns;
4+
5+
interface WithoutExceptionHandlingHandler
6+
{
7+
//
8+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Illuminate\Support\Facades;
4+
5+
use Illuminate\Contracts\Debug\ExceptionHandler;
6+
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Testing\Fakes\ExceptionHandlerFake;
8+
9+
/**
10+
* @method static void register()
11+
* @method static \Illuminate\Foundation\Exceptions\ReportableHandler reportable(callable $reportUsing)
12+
* @method static \Illuminate\Foundation\Exceptions\Handler renderable(callable $renderUsing)
13+
* @method static \Illuminate\Foundation\Exceptions\Handler map(\Closure|string $from, \Closure|string|null $to = null)
14+
* @method static \Illuminate\Foundation\Exceptions\Handler dontReport(array|string $exceptions)
15+
* @method static \Illuminate\Foundation\Exceptions\Handler ignore(array|string $exceptions)
16+
* @method static \Illuminate\Foundation\Exceptions\Handler dontFlash(array|string $attributes)
17+
* @method static \Illuminate\Foundation\Exceptions\Handler level(string $type, void $level)
18+
* @method static void report(\Throwable $e)
19+
* @method static bool shouldReport(\Throwable $e)
20+
* @method static \Illuminate\Foundation\Exceptions\Handler throttleUsing(callable $throttleUsing)
21+
* @method static \Illuminate\Foundation\Exceptions\Handler stopIgnoring(array|string $exceptions)
22+
* @method static \Illuminate\Foundation\Exceptions\Handler buildContextUsing(\Closure $contextCallback)
23+
* @method static \Symfony\Component\HttpFoundation\Response render(\Illuminate\Http\Request $request, \Throwable $e)
24+
* @method static \Illuminate\Foundation\Exceptions\Handler respondUsing(callable $callback)
25+
* @method static \Illuminate\Foundation\Exceptions\Handler shouldRenderJsonWhen(callable $callback)
26+
* @method static \Illuminate\Foundation\Exceptions\Handler dontReportDuplicates()
27+
* @method static \Illuminate\Contracts\Debug\ExceptionHandler handler()
28+
* @method static void assertNothingReported()
29+
* @method static void assertReported(\Closure|string $exception)
30+
* @method static void assertReportedCount(int $count)
31+
* @method static void assertNotReported(\Closure|string $exception)
32+
* @method static void renderForConsole(\Symfony\Component\Console\Output\OutputInterface $output, \Throwable $e)
33+
* @method static \Illuminate\Support\Testing\Fakes\ExceptionHandlerFake throwFirstReported()
34+
* @method static \Illuminate\Support\Testing\Fakes\ExceptionHandlerFake setHandler(\Illuminate\Contracts\Debug\ExceptionHandler $handler)
35+
*
36+
* @see \Illuminate\Foundation\Exceptions\Handler
37+
* @see \Illuminate\Contracts\Debug\ExceptionHandler
38+
* @see \Illuminate\Support\Testing\Fakes\ExceptionHandlerFake
39+
*/
40+
class Exceptions extends Facade
41+
{
42+
/**
43+
* Replace the bound instance with a fake.
44+
*
45+
* @param array<int, class-string<\Throwable>>|class-string<\Throwable> $exceptions
46+
* @return \Illuminate\Support\Testing\Fakes\ExceptionHandlerFake
47+
*/
48+
public static function fake(array|string $exceptions = [])
49+
{
50+
$exceptionHandler = static::isFake()
51+
? static::getFacadeRoot()->handler()
52+
: static::getFacadeRoot();
53+
54+
return tap(new ExceptionHandlerFake($exceptionHandler, Arr::wrap($exceptions)), function ($fake) {
55+
static::swap($fake);
56+
});
57+
}
58+
59+
/**
60+
* Get the registered name of the component.
61+
*
62+
* @return string
63+
*/
64+
protected static function getFacadeAccessor()
65+
{
66+
return ExceptionHandler::class;
67+
}
68+
}

0 commit comments

Comments
 (0)