7
7
use ApiSkeletons \Laravel \ApiProblem \ApiProblem ;
8
8
use ApiSkeletons \Laravel \ApiProblem \Exception ;
9
9
use ApiSkeletons \Laravel \ApiProblem \Facades \ApiProblem as ApiProblemFacade ;
10
- use http \Exception \InvalidArgumentException ;
11
10
use Illuminate \Http \JsonResponse ;
12
11
use ReflectionObject ;
13
12
use TypeError ;
14
13
15
14
final class ApiProblemTest extends TestCase
16
15
{
17
16
/** @psalm-return array<string, array{0: int}> */
18
- public function statusCodes (): array
17
+ public static function statusCodes (): array
19
18
{
20
19
return [
21
20
'200 ' => [200 ],
@@ -42,25 +41,21 @@ public function testResponseWithFacade(): void
42
41
$ this ->assertInstanceOf (JsonResponse::class, ApiProblemFacade::response ('Testing ' , 500 ));
43
42
}
44
43
45
- /**
46
- * @dataProvider statusCodes
47
- */
44
+ /** @dataProvider statusCodes */
48
45
public function testStatusIsUsedVerbatim (int $ status ): void
49
46
{
50
47
$ apiProblem = new ApiProblem ($ status , 'foo ' );
51
- $ payload = $ apiProblem ->toArray ();
48
+ $ payload = $ apiProblem ->toArray ();
52
49
$ this ->assertArrayHasKey ('status ' , $ payload );
53
50
$ this ->assertEquals ($ status , $ payload ['status ' ]);
54
51
}
55
52
56
- /**
57
- * @requires PHP 7.0
58
- */
53
+ /** @requires PHP 7.0 */
59
54
public function testErrorAsDetails (): void
60
55
{
61
- $ error = new TypeError ('error message ' , 705 );
56
+ $ error = new TypeError ('error message ' , 705 );
62
57
$ apiProblem = new ApiProblem (500 , $ error );
63
- $ payload = $ apiProblem ->toArray ();
58
+ $ payload = $ apiProblem ->toArray ();
64
59
65
60
$ this ->assertArrayHasKey ('title ' , $ payload );
66
61
$ this ->assertEquals ('TypeError ' , $ payload ['title ' ]);
@@ -72,33 +67,33 @@ public function testErrorAsDetails(): void
72
67
73
68
public function testExceptionCodeIsUsedForStatus (): void
74
69
{
75
- $ exception = new \Exception ('exception message ' , 401 );
70
+ $ exception = new \Exception ('exception message ' , 401 );
76
71
$ apiProblem = new ApiProblem ('500 ' , $ exception );
77
- $ payload = $ apiProblem ->toArray ();
72
+ $ payload = $ apiProblem ->toArray ();
78
73
$ this ->assertArrayHasKey ('status ' , $ payload );
79
74
$ this ->assertEquals ($ exception ->getCode (), $ payload ['status ' ]);
80
75
}
81
76
82
77
public function testDetailStringIsUsedVerbatim (): void
83
78
{
84
79
$ apiProblem = new ApiProblem ('500 ' , 'foo ' );
85
- $ payload = $ apiProblem ->toArray ();
80
+ $ payload = $ apiProblem ->toArray ();
86
81
$ this ->assertArrayHasKey ('detail ' , $ payload );
87
82
$ this ->assertEquals ('foo ' , $ payload ['detail ' ]);
88
83
}
89
84
90
85
public function testExceptionMessageIsUsedForDetail (): void
91
86
{
92
- $ exception = new \Exception ('exception message ' );
87
+ $ exception = new \Exception ('exception message ' );
93
88
$ apiProblem = new ApiProblem ('500 ' , $ exception );
94
- $ payload = $ apiProblem ->toArray ();
89
+ $ payload = $ apiProblem ->toArray ();
95
90
$ this ->assertArrayHasKey ('detail ' , $ payload );
96
91
$ this ->assertEquals ($ exception ->getMessage (), $ payload ['detail ' ]);
97
92
}
98
93
99
94
public function testExceptionsCanTriggerInclusionOfStackTraceInDetails (): void
100
95
{
101
- $ exception = new \Exception ('exception message ' );
96
+ $ exception = new \Exception ('exception message ' );
102
97
$ apiProblem = new ApiProblem ('500 ' , $ exception );
103
98
$ apiProblem ->setDetailIncludesStackTrace (true );
104
99
$ payload = $ apiProblem ->toArray ();
@@ -109,7 +104,7 @@ public function testExceptionsCanTriggerInclusionOfStackTraceInDetails(): void
109
104
110
105
public function testExceptionsCanTriggerInclusionOfNestedExceptions (): void
111
106
{
112
- $ exceptionChild = new \Exception ('child exception ' );
107
+ $ exceptionChild = new \Exception ('child exception ' );
113
108
$ exceptionParent = new \Exception ('parent exception ' , 0 , $ exceptionChild );
114
109
115
110
$ apiProblem = new ApiProblem ('500 ' , $ exceptionParent );
@@ -130,13 +125,13 @@ public function testExceptionsCanTriggerInclusionOfNestedExceptions(): void
130
125
public function testTypeUrlIsUsedVerbatim (): void
131
126
{
132
127
$ apiProblem = new ApiProblem ('500 ' , 'foo ' , 'http://status.dev:8080/details.md ' );
133
- $ payload = $ apiProblem ->toArray ();
128
+ $ payload = $ apiProblem ->toArray ();
134
129
$ this ->assertArrayHasKey ('type ' , $ payload );
135
130
$ this ->assertEquals ('http://status.dev:8080/details.md ' , $ payload ['type ' ]);
136
131
}
137
132
138
133
/** @psalm-return array<string, array{0: int}> */
139
- public function knownStatusCodes (): array
134
+ public static function knownStatusCodes (): array
140
135
{
141
136
return [
142
137
'404 ' => [404 ],
@@ -146,14 +141,12 @@ public function knownStatusCodes(): array
146
141
];
147
142
}
148
143
149
- /**
150
- * @dataProvider knownStatusCodes
151
- */
144
+ /** @dataProvider knownStatusCodes */
152
145
public function testKnownStatusResultsInKnownTitle (int $ status ): void
153
146
{
154
147
$ apiProblem = new ApiProblem ($ status , 'foo ' );
155
- $ r = new ReflectionObject ($ apiProblem );
156
- $ p = $ r ->getProperty ('problemStatusTitles ' );
148
+ $ r = new ReflectionObject ($ apiProblem );
149
+ $ p = $ r ->getProperty ('problemStatusTitles ' );
157
150
$ p ->setAccessible (true );
158
151
$ titles = $ p ->getValue ($ apiProblem );
159
152
@@ -165,15 +158,15 @@ public function testKnownStatusResultsInKnownTitle(int $status): void
165
158
public function testUnknownStatusResultsInUnknownTitle (): void
166
159
{
167
160
$ apiProblem = new ApiProblem (420 , 'foo ' );
168
- $ payload = $ apiProblem ->toArray ();
161
+ $ payload = $ apiProblem ->toArray ();
169
162
$ this ->assertArrayHasKey ('title ' , $ payload );
170
163
$ this ->assertEquals ('Unknown ' , $ payload ['title ' ]);
171
164
}
172
165
173
166
public function testProvidedTitleIsUsedVerbatim (): void
174
167
{
175
168
$ apiProblem = new ApiProblem ('500 ' , 'foo ' , 'http://status.dev:8080/details.md ' , 'some title ' );
176
- $ payload = $ apiProblem ->toArray ();
169
+ $ payload = $ apiProblem ->toArray ();
177
170
$ this ->assertArrayHasKey ('title ' , $ payload );
178
171
$ this ->assertEquals ('some title ' , $ payload ['title ' ]);
179
172
}
@@ -185,7 +178,7 @@ public function testCanPassArbitraryDetailsToConstructor(): void
185
178
'Invalid input ' ,
186
179
'http://example.com/api/problem/400 ' ,
187
180
'Invalid entity ' ,
188
- ['foo ' => 'bar ' ]
181
+ ['foo ' => 'bar ' ],
189
182
);
190
183
$ this ->assertEquals ('bar ' , $ problem ->foo );
191
184
}
@@ -197,9 +190,9 @@ public function testArraySerializationIncludesArbitraryDetails(): void
197
190
'Invalid input ' ,
198
191
'http://example.com/api/problem/400 ' ,
199
192
'Invalid entity ' ,
200
- ['foo ' => 'bar ' ]
193
+ ['foo ' => 'bar ' ],
201
194
);
202
- $ array = $ problem ->toArray ();
195
+ $ array = $ problem ->toArray ();
203
196
$ this ->assertArrayHasKey ('foo ' , $ array );
204
197
$ this ->assertEquals ('bar ' , $ array ['foo ' ]);
205
198
}
@@ -211,9 +204,9 @@ public function testArbitraryDetailsShouldNotOverwriteRequiredFieldsInArraySeria
211
204
'Invalid input ' ,
212
205
'http://example.com/api/problem/400 ' ,
213
206
'Invalid entity ' ,
214
- ['title ' => 'SHOULD NOT GET THIS ' ]
207
+ ['title ' => 'SHOULD NOT GET THIS ' ],
215
208
);
216
- $ array = $ problem ->toArray ();
209
+ $ array = $ problem ->toArray ();
217
210
$ this ->assertArrayHasKey ('title ' , $ array );
218
211
$ this ->assertEquals ('Invalid entity ' , $ array ['title ' ]);
219
212
}
@@ -223,7 +216,7 @@ public function testUsesTitleFromExceptionWhenProvided(): void
223
216
$ exception = new Exception \DomainException ('exception message ' , 401 );
224
217
$ exception ->setTitle ('problem title ' );
225
218
$ apiProblem = new ApiProblem ('401 ' , $ exception );
226
- $ payload = $ apiProblem ->toArray ();
219
+ $ payload = $ apiProblem ->toArray ();
227
220
$ this ->assertArrayHasKey ('title ' , $ payload );
228
221
$ this ->assertEquals ($ exception ->getTitle (), $ payload ['title ' ]);
229
222
}
@@ -233,7 +226,7 @@ public function testUsesTypeFromExceptionWhenProvided(): void
233
226
$ exception = new Exception \DomainException ('exception message ' , 401 );
234
227
$ exception ->setType ('http://example.com/api/help/401 ' );
235
228
$ apiProblem = new ApiProblem ('401 ' , $ exception );
236
- $ payload = $ apiProblem ->toArray ();
229
+ $ payload = $ apiProblem ->toArray ();
237
230
$ this ->assertArrayHasKey ('type ' , $ payload );
238
231
$ this ->assertEquals ($ exception ->getType (), $ payload ['type ' ]);
239
232
}
@@ -243,13 +236,13 @@ public function testUsesAdditionalDetailsFromExceptionWhenProvided(): void
243
236
$ exception = new Exception \DomainException ('exception message ' , 401 );
244
237
$ exception ->setAdditionalDetails (['foo ' => 'bar ' ]);
245
238
$ apiProblem = new ApiProblem ('401 ' , $ exception );
246
- $ payload = $ apiProblem ->toArray ();
239
+ $ payload = $ apiProblem ->toArray ();
247
240
$ this ->assertArrayHasKey ('foo ' , $ payload );
248
241
$ this ->assertEquals ('bar ' , $ payload ['foo ' ]);
249
242
}
250
243
251
244
/** @psalm-return array<string, array{0: int}> */
252
- public function invalidStatusCodes (): array
245
+ public static function invalidStatusCodes (): array
253
246
{
254
247
return [
255
248
'-1 ' => [-1 ],
@@ -266,7 +259,7 @@ public function invalidStatusCodes(): array
266
259
*/
267
260
public function testInvalidHttpStatusCodesAreCastTo500 (int $ code ): void
268
261
{
269
- $ e = new \Exception ('Testing ' , $ code );
262
+ $ e = new \Exception ('Testing ' , $ code );
270
263
$ problem = new ApiProblem ($ code , $ e );
271
264
$ this ->assertEquals (500 , $ problem ->status );
272
265
}
0 commit comments