|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use PDPhilip\Elasticsearch\Data\QueryMeta; |
| 6 | +use PDPhilip\Elasticsearch\Eloquent\ElasticCollection; |
| 7 | +use PDPhilip\Elasticsearch\Tests\Models\User; |
| 8 | + |
| 9 | +beforeEach(function () { |
| 10 | + User::executeSchema(); |
| 11 | + |
| 12 | + User::insert([ |
| 13 | + ['name' => 'John Doe', 'age' => 35, 'title' => 'admin'], |
| 14 | + ['name' => 'Jane Doe', 'age' => 33, 'title' => 'admin'], |
| 15 | + ['name' => 'Harry Hoe', 'age' => 13, 'title' => 'user'], |
| 16 | + ['name' => 'Robert Roe', 'age' => 37, 'title' => 'user'], |
| 17 | + ['name' => 'Mark Moe', 'age' => 23, 'title' => 'user'], |
| 18 | + ]); |
| 19 | +}); |
| 20 | + |
| 21 | +it('carries the query meta on a page that has results', function () { |
| 22 | + $results = User::where('title', 'user')->paginate(2); |
| 23 | + |
| 24 | + expect($results->getCollection())->toBeInstanceOf(ElasticCollection::class) |
| 25 | + ->and($results->getCollection()->hasQueryMeta())->toBeTrue() |
| 26 | + ->and($results->total())->toBe(3) |
| 27 | + ->and($results->getQueryMeta()->getTotalHits())->toBe(3); |
| 28 | +}); |
| 29 | + |
| 30 | +it('carries the query meta on a page that matched nothing', function () { |
| 31 | + $results = User::where('age', 999)->paginate(2); |
| 32 | + |
| 33 | + expect($results->getCollection())->toBeInstanceOf(ElasticCollection::class) |
| 34 | + ->and($results->getCollection()->hasQueryMeta())->toBeTrue() |
| 35 | + ->and($results->count())->toBe(0) |
| 36 | + ->and($results->total())->toBe(0) |
| 37 | + ->and($results->getQueryMeta())->toBeInstanceOf(QueryMeta::class) |
| 38 | + ->and($results->getQueryMeta()->getTotalHits())->toBe(0) |
| 39 | + ->and($results->getQueryMeta()->getTook())->toBeGreaterThanOrEqual(0); |
| 40 | +}); |
| 41 | + |
| 42 | +it('reports the full total on a page that matched nothing when tracking total hits', function () { |
| 43 | + $results = User::where('age', 999)->withTrackTotalHits()->paginate(2); |
| 44 | + |
| 45 | + expect($results->total())->toBe(0) |
| 46 | + ->and($results->getQueryMeta()->getTotalHits())->toBe(0); |
| 47 | +}); |
| 48 | + |
| 49 | +it('keeps the query meta through paginator transforms', function () { |
| 50 | + $emptyPage = User::where('age', 999)->paginate(2) |
| 51 | + ->withQueryString() |
| 52 | + ->through(fn ($user) => ['name' => $user->name]); |
| 53 | + |
| 54 | + $fullPage = User::where('title', 'user')->paginate(2) |
| 55 | + ->withQueryString() |
| 56 | + ->through(fn ($user) => ['name' => $user->name]); |
| 57 | + |
| 58 | + expect($emptyPage->getQueryMeta()->getTotalHits())->toBe(0) |
| 59 | + ->and($fullPage->getQueryMeta()->getTotalHits())->toBe(3); |
| 60 | +}); |
| 61 | + |
| 62 | +it('answers every meta getter on a collection built without a query', function () { |
| 63 | + $collection = new ElasticCollection; |
| 64 | + |
| 65 | + expect($collection->hasQueryMeta())->toBeFalse() |
| 66 | + ->and($collection->getQueryMeta())->toBeInstanceOf(QueryMeta::class) |
| 67 | + ->and($collection->getQueryMeta()->getTotalHits())->toBe(-1) |
| 68 | + ->and($collection->getQueryMetaAsArray())->toBe([]) |
| 69 | + ->and($collection->getTook())->toBe(-1) |
| 70 | + ->and($collection->getTotal())->toBe(-1) |
| 71 | + ->and($collection->getMaxScore())->toBe('') |
| 72 | + ->and($collection->getShards())->toBe([]) |
| 73 | + ->and($collection->getDsl())->toBe(['query' => '', 'dsl' => []]) |
| 74 | + ->and($collection->getResults())->toBe([]) |
| 75 | + ->and($collection->getAfterKey())->toBe([]) |
| 76 | + ->and($collection->getPitId())->toBeNull() |
| 77 | + ->and($collection->hasQueryMeta())->toBeFalse(); |
| 78 | +}); |
| 79 | + |
| 80 | +it('builds meta safe collections for models', function () { |
| 81 | + $newCollection = (new User)->newCollection(); |
| 82 | + $hydrated = User::hydrate([['name' => 'John Doe']]); |
| 83 | + |
| 84 | + expect($newCollection)->toBeInstanceOf(ElasticCollection::class) |
| 85 | + ->and($newCollection->hasQueryMeta())->toBeFalse() |
| 86 | + ->and($newCollection->getQueryMeta()->getTotalHits())->toBe(-1) |
| 87 | + ->and($hydrated)->toBeInstanceOf(ElasticCollection::class) |
| 88 | + ->and($hydrated->hasQueryMeta())->toBeFalse() |
| 89 | + ->and($hydrated->getQueryMeta()->getTotalHits())->toBe(-1); |
| 90 | +}); |
| 91 | + |
| 92 | +it('keeps the query meta on a fetched collection', function () { |
| 93 | + $users = User::where('title', 'user')->get(); |
| 94 | + |
| 95 | + expect($users->hasQueryMeta())->toBeTrue() |
| 96 | + ->and($users->getQueryMeta()->getTotalHits())->toBe(3) |
| 97 | + ->and($users->getTook())->toBeGreaterThanOrEqual(0); |
| 98 | +}); |
0 commit comments