-
-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathCatchFailures.php
More file actions
212 lines (191 loc) · 5.51 KB
/
CatchFailures.php
File metadata and controls
212 lines (191 loc) · 5.51 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/
/**
* We don't care for unhandled exceptions in tests.
* It is the nature of a test to throw an exception.
* Without this suppression we had 100+ Linter warning in this file which
* don't help anything.
*
* @noinspection PhpDocMissingThrowsInspection
* @noinspection PhpUnhandledExceptionInspection
*/
namespace Tests\Traits;
use Illuminate\Testing\Assert as PHPUnit;
use Illuminate\Testing\TestResponse;
/**
* This trait allows to retrieve the message returned by the back-end in case of unexpected results.
* This provides more readable results than: "status code 500 does match expected status code 200".
*/
trait CatchFailures
{
/**
* Some of the exceptions we get are expected. We silence then.
*
* @var string[]
*/
protected array $catchFailureSilence = ["App\Exceptions\MediaFileOperationException"];
/**
* We trim the trace of exceptions to get better data.
*
* @var string[]
*/
protected array $exception_noise = [
'Illuminate\Database\Query\Builder',
'Illuminate\Database\Connection',
'Illuminate\Pipeline\Pipeline',
'Illuminate\Container\BoundMethod',
'Illuminate\Container\Util',
'Illuminate\Container\Container',
];
/**
* @param TestResponse<\Illuminate\Http\JsonResponse> $response
* @param int|array $expectedStatusCode
*
* @return void
*/
protected function assertStatus(TestResponse $response, int|array $expectedStatusCode): void
{
$expectedStatusCodeArray = is_int($expectedStatusCode) ? [$expectedStatusCode] : $expectedStatusCode;
if ($response->getStatusCode() === 500 && $expectedStatusCode !== 500) {
$exception = $response->json();
if (in_array($exception['exception'], $this->catchFailureSilence, true)) {
return;
}
$this->trimException($exception);
dump($exception);
// We remove 204 as it does not have content
// We remove 302 because it does not have json data.
} elseif (!in_array($response->getStatusCode(), [204, 302, ...$expectedStatusCodeArray], true)) {
$this->trimException($exception);
}
PHPUnit::assertContains($response->getStatusCode(), $expectedStatusCodeArray);
}
/**
* An exception is an array of the shape:
* array{message:string, exception:string, file:string, line:int, trace:array{}, previous_exception: obj }
* Unfortunately the trace contains the full call stack and dumping it completely does not add significant
* information. Most of the time only the first 3 values of the trace are of interest.
*
* For this reason this function only keeps the first 3 values of the trace of the exception returned.
*
* Additionally, this transformation is applied recursively on the previous_exception in the case of
* exception encapsulation.
*
* @param array $exception
*
* @return void
*/
private function trimException(array &$exception): void
{
if (isset($exception['trace'])) {
$exception['trace'] = array_values(array_filter($exception['trace'], fn ($t) => !in_array($t['class'] ?? '', $this->exception_noise, true)));
$exception['trace'] = array_slice($exception['trace'], 0, 3);
}
if (isset($exception['previous_exception'])) {
$this->trimException($exception['previous_exception']);
}
}
/**
* @param TestResponse<\Illuminate\Http\JsonResponse> $response
*
* @return void
*/
protected function assertOk(TestResponse $response): void
{
$this->assertStatus($response, 200);
}
/**
* @param TestResponse<\Illuminate\Http\JsonResponse> $response
*
* @return void
*/
protected function assertCreated(TestResponse $response): void
{
$this->assertStatus($response, 201);
}
/**
* @param TestResponse<\Illuminate\Http\JsonResponse> $response
*
* @return void
*/
protected function assertNoContent(TestResponse $response): void
{
$this->assertStatus($response, 204);
}
/**
* @param TestResponse<\Illuminate\Http\JsonResponse> $response
*
* @return void
*/
protected function assertRedirect(TestResponse $response): void
{
$this->assertStatus($response, 302);
}
/**
* @param TestResponse<\Illuminate\Http\JsonResponse> $response
*
* @return void
*/
protected function assertUnauthorized(TestResponse $response): void
{
$this->assertStatus($response, 401);
}
/**
* @param TestResponse<\Illuminate\Http\JsonResponse> $response
*
* @return void
*/
protected function assertSupporterRequired(TestResponse $response): void
{
$this->assertStatus($response, 402);
}
/**
* @param TestResponse<\Illuminate\Http\JsonResponse> $response
*
* @return void
*/
protected function assertForbidden(TestResponse $response): void
{
$this->assertStatus($response, 403);
}
/**
* @param TestResponse<\Illuminate\Http\JsonResponse> $response
*
* @return void
*/
protected function assertNotFound(TestResponse $response): void
{
$this->assertStatus($response, 404);
}
/**
* @param TestResponse<\Illuminate\Http\JsonResponse> $response
*
* @return void
*/
protected function assertConflict(TestResponse $response): void
{
$this->assertStatus($response, 409);
}
/**
* @param TestResponse<\Illuminate\Http\JsonResponse> $response
*
* @return void
*/
protected function assertUnprocessable(TestResponse $response): void
{
$this->assertStatus($response, 422);
}
/**
* @param TestResponse<\Illuminate\Http\JsonResponse> $response
*
* @return void
*/
public function assertInternalServerError(TestResponse $response): void
{
$this->assertStatus($response, 500);
}
}