-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
InteractsWithExceptionHandling.php
194 lines (171 loc) · 5.53 KB
/
InteractsWithExceptionHandling.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace Illuminate\Foundation\Testing\Concerns;
use Closure;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Testing\Assert;
use Illuminate\Validation\ValidationException;
use Symfony\Component\Console\Application as ConsoleApplication;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;
trait InteractsWithExceptionHandling
{
/**
* The original exception handler.
*
* @var \Illuminate\Contracts\Debug\ExceptionHandler|null
*/
protected $originalExceptionHandler;
/**
* Restore exception handling.
*
* @return $this
*/
protected function withExceptionHandling()
{
if ($this->originalExceptionHandler) {
$this->app->instance(ExceptionHandler::class, $this->originalExceptionHandler);
}
return $this;
}
/**
* Only handle the given exceptions via the exception handler.
*
* @param array $exceptions
* @return $this
*/
protected function handleExceptions(array $exceptions)
{
return $this->withoutExceptionHandling($exceptions);
}
/**
* Only handle validation exceptions via the exception handler.
*
* @return $this
*/
protected function handleValidationExceptions()
{
return $this->handleExceptions([ValidationException::class]);
}
/**
* Disable exception handling for the test.
*
* @param array $except
* @return $this
*/
protected function withoutExceptionHandling(array $except = [])
{
if ($this->originalExceptionHandler == null) {
$this->originalExceptionHandler = app(ExceptionHandler::class);
}
$this->app->instance(ExceptionHandler::class, new class($this->originalExceptionHandler, $except) implements ExceptionHandler
{
protected $except;
protected $originalHandler;
/**
* Create a new class instance.
*
* @param \Illuminate\Contracts\Debug\ExceptionHandler $originalHandler
* @param array $except
* @return void
*/
public function __construct($originalHandler, $except = [])
{
$this->except = $except;
$this->originalHandler = $originalHandler;
}
/**
* Report or log an exception.
*
* @param \Throwable $e
* @return void
*
* @throws \Exception
*/
public function report(Throwable $e)
{
//
}
/**
* Determine if the exception should be reported.
*
* @param \Throwable $e
* @return bool
*/
public function shouldReport(Throwable $e)
{
return false;
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $e
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render($request, Throwable $e)
{
foreach ($this->except as $class) {
if ($e instanceof $class) {
return $this->originalHandler->render($request, $e);
}
}
if ($e instanceof NotFoundHttpException) {
throw new NotFoundHttpException(
"{$request->method()} {$request->url()}", $e, is_int($e->getCode()) ? $e->getCode() : 0
);
}
throw $e;
}
/**
* Render an exception to the console.
*
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param \Throwable $e
* @return void
*/
public function renderForConsole($output, Throwable $e)
{
(new ConsoleApplication)->renderThrowable($e, $output);
}
});
return $this;
}
/**
* Assert that the given callback throws an exception with the given message when invoked.
*
* @param \Closure $test
* @param class-string<\Throwable> $expectedClass
* @param string|null $expectedMessage
* @return $this
*/
protected function assertThrows(Closure $test, string $expectedClass = Throwable::class, ?string $expectedMessage = null)
{
try {
$test();
$thrown = false;
} catch (Throwable $exception) {
$thrown = $exception instanceof $expectedClass;
$actualMessage = $exception->getMessage();
}
Assert::assertTrue(
$thrown,
sprintf('Failed asserting that exception of type "%s" was thrown.', $expectedClass)
);
if (isset($expectedMessage)) {
if (! isset($actualMessage)) {
Assert::fail(
sprintf(
'Failed asserting that exception of type "%s" with message "%s" was thrown.',
$expectedClass,
$expectedMessage
)
);
} else {
Assert::assertStringContainsString($expectedMessage, $actualMessage);
}
}
return $this;
}
}