@@ -218,11 +218,90 @@ public function testFilteredAndPaginated(): void
218
218
->page (['number ' => 1 , 'size ' => 10 ])
219
219
->get ('/api/v1/posts ' );
220
220
221
- $ response ->assertFetchedMany ($ expected )
221
+ $ response
222
+ ->assertFetchedMany ($ expected )
222
223
->assertMeta ($ meta )
223
224
->assertLinks ($ links );
224
225
}
225
226
227
+ public function testSparseFieldSets (): void
228
+ {
229
+ $ posts = Post::factory ()->count (3 )->create ();
230
+
231
+ $ expected = $ posts ->map (
232
+ fn (Post $ post ) => $ this ->serializer
233
+ ->post ($ post )
234
+ ->only ('author ' , 'slug ' , 'synopsis ' , 'title ' )
235
+ ->replace ('author ' , ['type ' => 'users ' , 'id ' => $ post ->author ])
236
+ );
237
+
238
+ $ authors = $ this ->identifiersFor (
239
+ 'users ' , $ posts ->pluck ('author ' )
240
+ );
241
+
242
+ $ response = $ this
243
+ ->jsonApi ('posts ' )
244
+ ->sparseFields ('posts ' , ['author ' , 'slug ' , 'synopsis ' , 'title ' ])
245
+ ->sparseFields ('users ' , ['name ' ])
246
+ ->includePaths ('author ' )
247
+ ->get ('/api/v1/posts ' );
248
+
249
+ $ response
250
+ ->assertFetchedManyExact ($ expected )
251
+ ->assertIncluded ($ authors );
252
+ }
253
+
254
+ public function testSparseFieldSetsAndPaginated (): void
255
+ {
256
+ $ posts = Post::factory ()->count (5 )->create ();
257
+
258
+ $ expected = $ posts ->take (3 )->map (
259
+ fn (Post $ post ) => $ this ->serializer
260
+ ->post ($ post )
261
+ ->only ('author ' , 'slug ' , 'synopsis ' , 'title ' )
262
+ ->replace ('author ' , ['type ' => 'users ' , 'id ' => $ post ->author ])
263
+ );
264
+
265
+ $ meta = [
266
+ 'currentPage ' => 1 ,
267
+ 'from ' => 1 ,
268
+ 'lastPage ' => 2 ,
269
+ 'perPage ' => 3 ,
270
+ 'to ' => 3 ,
271
+ 'total ' => 5 ,
272
+ ];
273
+
274
+ $ links = [
275
+ 'first ' => 'http://localhost/api/v1/posts? ' . Arr::query ([
276
+ 'fields ' => $ fields = [
277
+ 'posts ' => 'author,slug,synopsis,title ' ,
278
+ ],
279
+ 'include ' => 'author ' ,
280
+ 'page ' => ['number ' => 1 , 'size ' => 3 ],
281
+ 'sort ' => '-createdAt ' ,
282
+ ]),
283
+ 'last ' => $ last = 'http://localhost/api/v1/posts? ' . Arr::query ([
284
+ 'fields ' => $ fields ,
285
+ 'include ' => 'author ' ,
286
+ 'page ' => ['number ' => 2 , 'size ' => 3 ],
287
+ 'sort ' => '-createdAt ' ,
288
+ ]),
289
+ 'next ' => $ last ,
290
+ ];
291
+
292
+ $ response = $ this
293
+ ->jsonApi ('posts ' )
294
+ ->sparseFields ('posts ' , ['author ' , 'slug ' , 'synopsis ' , 'title ' ])
295
+ ->includePaths ('author ' )
296
+ ->page (['number ' => 1 , 'size ' => 3 ])
297
+ ->get ('/api/v1/posts ' );
298
+
299
+ $ response
300
+ ->assertFetchedManyExact ($ expected )
301
+ ->assertExactMeta ($ meta )
302
+ ->assertLinks ($ links );
303
+ }
304
+
226
305
public function testWithCount (): void
227
306
{
228
307
$ posts = Post::factory ()
0 commit comments