Skip to content

Commit 4a91d8e

Browse files
committed
Add failing tests
1 parent b9d25fb commit 4a91d8e

2 files changed

Lines changed: 106 additions & 0 deletions

File tree

tests/Feature/OpenSearchScoutServiceProviderTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22

33
use DirectoryTree\OpenSearchAdapter\Documents\DocumentManagerInterface;
44
use DirectoryTree\OpenSearchAdapter\Indices\IndexManagerInterface;
5+
use DirectoryTree\OpenSearchAdapter\Testing\Fakes\FakeDocumentManager;
6+
use DirectoryTree\OpenSearchAdapter\Testing\Fakes\FakeIndexManager;
7+
use DirectoryTree\OpenSearchScoutDriver\CursorPaginator;
58
use DirectoryTree\OpenSearchScoutDriver\Engine;
9+
use DirectoryTree\OpenSearchScoutDriver\Factories\DocumentFactory;
610
use DirectoryTree\OpenSearchScoutDriver\Factories\DocumentFactoryInterface;
11+
use DirectoryTree\OpenSearchScoutDriver\Factories\ModelFactory;
712
use DirectoryTree\OpenSearchScoutDriver\Factories\ModelFactoryInterface;
13+
use DirectoryTree\OpenSearchScoutDriver\Factories\SearchRequestFactory;
814
use DirectoryTree\OpenSearchScoutDriver\Factories\SearchRequestFactoryInterface;
15+
use DirectoryTree\OpenSearchScoutDriver\Tests\Fixtures\Client;
916
use Laravel\Scout\Builder;
1017
use Laravel\Scout\EngineManager;
1118

@@ -24,3 +31,47 @@
2431
it('registers the cursor paginate builder macro', function () {
2532
expect(Builder::hasMacro('cursorPaginate'))->toBeTrue();
2633
});
34+
35+
it('appends the search query to cursor pagination urls', function () {
36+
app()->bind(Engine::class, fn () => new class extends Engine
37+
{
38+
public function __construct()
39+
{
40+
parent::__construct(
41+
new ModelFactory,
42+
new FakeIndexManager,
43+
new FakeDocumentManager,
44+
new DocumentFactory,
45+
new SearchRequestFactory,
46+
true,
47+
);
48+
}
49+
50+
public function cursorPaginate(Builder $builder, $perPage = null, $cursorName = 'cursor', $cursor = null): CursorPaginator
51+
{
52+
return new CursorPaginator(
53+
(new Client)->newCollection([
54+
new Client(['id' => 1]),
55+
new Client(['id' => 2]),
56+
]),
57+
1,
58+
null,
59+
[
60+
'cursorName' => $cursorName,
61+
'path' => '/clients',
62+
'parameters' => [CursorPaginator::SEARCH_AFTER_PARAMETER],
63+
'searchAfter' => [
64+
'1' => ['john@example.com', '1'],
65+
'2' => ['jane@example.com', '2'],
66+
],
67+
],
68+
);
69+
}
70+
});
71+
72+
app(EngineManager::class)->forgetDrivers();
73+
74+
$paginator = Client::search('john')->cursorPaginate(1);
75+
76+
expect($paginator->nextPageUrl())->toContain('query=john');
77+
});

tests/Unit/EngineTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
259314
it('requires an explicit sort for cursor pagination', function () {
260315
$engine = new Engine(
261316
new ModelFactory,

0 commit comments

Comments
 (0)