@@ -39,6 +39,7 @@ protected function defineEnvironment($app): void
39
39
tap ($ app ['config ' ], function (Repository $ config ): void {
40
40
$ config ->set ('api-platform.formats ' , ['jsonapi ' => ['application/vnd.api+json ' ]]);
41
41
$ config ->set ('api-platform.docs_formats ' , ['jsonapi ' => ['application/vnd.api+json ' ]]);
42
+ $ config ->set ('api-platform.resources ' , [app_path ('Models ' ), app_path ('ApiResource ' )]);
42
43
$ config ->set ('app.debug ' , true );
43
44
});
44
45
}
@@ -48,13 +49,15 @@ public function testGetEntrypoint(): void
48
49
$ response = $ this ->get ('/api/ ' , ['accept ' => ['application/vnd.api+json ' ]]);
49
50
$ response ->assertStatus (200 );
50
51
$ response ->assertHeader ('content-type ' , 'application/vnd.api+json; charset=utf-8 ' );
51
- $ this ->assertJsonContains ([
52
- 'links ' => [
53
- 'self ' => 'http://localhost/api ' ,
54
- 'book ' => 'http://localhost/api/books ' ,
52
+ $ this ->assertJsonContains (
53
+ [
54
+ 'links ' => [
55
+ 'self ' => 'http://localhost/api ' ,
56
+ 'book ' => 'http://localhost/api/books ' ,
57
+ ],
55
58
],
56
- ],
57
- $ response -> json () );
59
+ $ response -> json ()
60
+ );
58
61
}
59
62
60
63
public function testGetCollection (): void
@@ -209,4 +212,77 @@ public function testRelationWithGroups(): void
209
212
$ this ->assertArrayHasKey ('relation ' , $ content ['data ' ]['relationships ' ]);
210
213
$ this ->assertArrayHasKey ('data ' , $ content ['data ' ]['relationships ' ]['relation ' ]);
211
214
}
215
+
216
+ public function testValidateJsonApi (): void
217
+ {
218
+ $ response = $ this ->postJson (
219
+ '/api/issue6745/rule_validations ' ,
220
+ [
221
+ 'data ' => [
222
+ 'type ' => 'string ' ,
223
+ 'attributes ' => ['max ' => 3 ],
224
+ ],
225
+ ],
226
+ [
227
+ 'accept ' => 'application/vnd.api+json ' ,
228
+ 'content_type ' => 'application/vnd.api+json ' ,
229
+ ]
230
+ );
231
+
232
+ $ response ->assertStatus (422 );
233
+ $ response ->assertHeader ('content-type ' , 'application/vnd.api+json; charset=utf-8 ' );
234
+ $ json = $ response ->json ();
235
+ $ this ->assertJsonContains ([
236
+ 'errors ' => [
237
+ [
238
+ 'detail ' => 'The prop field is required. ' ,
239
+ 'title ' => 'Validation Error ' ,
240
+ 'status ' => 422 ,
241
+ 'code ' => '58350900e0fc6b8e/prop ' ,
242
+ ],
243
+ [
244
+ 'detail ' => 'The max field must be less than 2. ' ,
245
+ 'title ' => 'Validation Error ' ,
246
+ 'status ' => 422 ,
247
+ 'code ' => '58350900e0fc6b8e/max ' ,
248
+ ],
249
+ ],
250
+ ], $ json );
251
+
252
+ $ this ->assertArrayHasKey ('id ' , $ json ['errors ' ][0 ]);
253
+ $ this ->assertArrayHasKey ('links ' , $ json ['errors ' ][0 ]);
254
+ $ this ->assertArrayHasKey ('type ' , $ json ['errors ' ][0 ]['links ' ]);
255
+
256
+ $ response = $ this ->postJson (
257
+ '/api/issue6745/rule_validations ' ,
258
+ [
259
+ 'data ' => [
260
+ 'type ' => 'string ' ,
261
+ 'attributes ' => [
262
+ 'prop ' => 1 ,
263
+ 'max ' => 1 ,
264
+ ],
265
+ ],
266
+ ],
267
+ [
268
+ 'accept ' => 'application/vnd.api+json ' ,
269
+ 'content_type ' => 'application/vnd.api+json ' ,
270
+ ]
271
+ );
272
+ $ response ->assertStatus (201 );
273
+ }
274
+
275
+ public function testNotFound (): void
276
+ {
277
+ $ response = $ this ->get ('/api/books/notfound ' , headers: ['accept ' => 'application/vnd.api+json ' ]);
278
+ $ response ->assertStatus (404 );
279
+ $ response ->assertHeader ('content-type ' , 'application/vnd.api+json; charset=utf-8 ' );
280
+
281
+ $ this ->assertJsonContains ([
282
+ 'links ' => ['type ' => '/errors/404 ' ],
283
+ 'title ' => 'An error occurred ' ,
284
+ 'status ' => 404 ,
285
+ 'detail ' => 'Not Found ' ,
286
+ ], $ response ->json ()['errors ' ][0 ]);
287
+ }
212
288
}
0 commit comments