@@ -256,6 +256,61 @@ public function makeLazyFromSearchResponse(SearchResponse $searchResponse, Build
256256 $ documentManager ->assertSearched ('clients ' , $ request );
257257});
258258
259+ it ('applies raw result callbacks when cursor paginating ' , function () {
260+ $ response = SearchResponse::fake ([
261+ Hit::fake (['name ' => 'John ' ], index: 'clients ' , id: '1 ' , attributes: ['sort ' => ['john@example.com ' , '1 ' ]]),
262+ Hit::fake (['name ' => 'Jane ' ], index: 'clients ' , id: '2 ' , attributes: ['sort ' => ['jane@example.com ' , '2 ' ]]),
263+ Hit::fake (['name ' => 'Taylor ' ], index: 'clients ' , id: '3 ' , attributes: ['sort ' => ['taylor@example.com ' , '3 ' ]]),
264+ ], 'clients ' );
265+
266+ $ callbackResponse = SearchResponse::fake ([
267+ Hit::fake (['name ' => 'Adam ' ], index: 'clients ' , id: '4 ' , attributes: ['sort ' => ['adam@example.com ' , '4 ' ]]),
268+ Hit::fake (['name ' => 'Erin ' ], index: 'clients ' , id: '5 ' , attributes: ['sort ' => ['erin@example.com ' , '5 ' ]]),
269+ Hit::fake (['name ' => 'Zoe ' ], index: 'clients ' , id: '6 ' , attributes: ['sort ' => ['zoe@example.com ' , '6 ' ]]),
270+ ], 'clients ' );
271+
272+ $ documentManager = new FakeDocumentManager ($ response );
273+
274+ $ modelFactory = new class implements ModelFactoryInterface
275+ {
276+ public function makeFromSearchResponse (SearchResponse $ searchResponse , Builder $ builder ): Collection
277+ {
278+ return $ builder ->model ->newCollection (array_map (
279+ fn (Hit $ hit ) => new Client (['id ' => (int ) $ hit ->document ()->id ()]),
280+ $ searchResponse ->hits (),
281+ ));
282+ }
283+
284+ public function makeLazyFromSearchResponse (SearchResponse $ searchResponse , Builder $ builder ): LazyCollection
285+ {
286+ return LazyCollection::make ($ this ->makeFromSearchResponse ($ searchResponse , $ builder ));
287+ }
288+ };
289+
290+ $ engine = new Engine (
291+ $ modelFactory ,
292+ new FakeIndexManager ,
293+ $ documentManager ,
294+ new DocumentFactory ,
295+ new SearchRequestFactory ,
296+ true ,
297+ );
298+
299+ $ builder = Client::search ('' )
300+ ->orderBy ('email ' )
301+ ->orderBy ('id ' )
302+ ->withRawResults (function (SearchResponse $ results ) use ($ callbackResponse ) {
303+ expect ($ results ->hits ()[0 ]->document ()->id ())->toBe ('1 ' );
304+
305+ return $ callbackResponse ;
306+ });
307+
308+ $ paginator = $ engine ->cursorPaginate ($ builder , 2 );
309+
310+ expect (collect ($ paginator ->items ())->pluck ('id ' )->all ())->toBe ([4 , 5 ])
311+ ->and ($ paginator ->nextCursor ()?->parameter(CursorPaginator::SEARCH_AFTER_PARAMETER ))->toBe (['erin@example.com ' , '5 ' ]);
312+ });
313+
259314it ('requires an explicit sort for cursor pagination ' , function () {
260315 $ engine = new Engine (
261316 new ModelFactory ,
0 commit comments