|
| 1 | +<?php |
| 2 | + |
| 3 | +use DirectoryTree\OpenSearchAdapter\Indices\IndexManager; |
| 4 | +use DirectoryTree\OpenSearchScoutDriver\Tests\Fixtures\Client; |
| 5 | +use Illuminate\Database\Schema\Blueprint; |
| 6 | +use Illuminate\Support\Facades\Schema; |
| 7 | +use Laravel\Scout\EngineManager; |
| 8 | + |
| 9 | +beforeEach(function (): void { |
| 10 | + config()->set('scout.prefix', sprintf('scout_integration_%s_', bin2hex(random_bytes(4)))); |
| 11 | + |
| 12 | + Schema::create('clients', function (Blueprint $table): void { |
| 13 | + $table->id(); |
| 14 | + $table->string('name'); |
| 15 | + $table->string('email'); |
| 16 | + $table->timestamp('deleted_at')->nullable(); |
| 17 | + }); |
| 18 | + |
| 19 | + Client::query()->insert([ |
| 20 | + ['id' => 1, 'name' => 'John Smith', 'email' => 'john@example.com'], |
| 21 | + ['id' => 2, 'name' => 'Jane Smith', 'email' => 'jane@example.com'], |
| 22 | + ['id' => 3, 'name' => 'Taylor Otwell', 'email' => 'taylor@example.com'], |
| 23 | + ]); |
| 24 | + |
| 25 | + $this->indexName = (new Client)->searchableAs(); |
| 26 | + $this->indexManager = app(IndexManager::class); |
| 27 | + |
| 28 | + if ($this->indexManager->exists($this->indexName)) { |
| 29 | + $this->indexManager->drop($this->indexName); |
| 30 | + } |
| 31 | + |
| 32 | + app(EngineManager::class)->engine('opensearch')->createIndex($this->indexName); |
| 33 | + |
| 34 | + $this->indexManager->putMappingRaw($this->indexName, [ |
| 35 | + 'properties' => [ |
| 36 | + 'name' => [ |
| 37 | + 'type' => 'text', |
| 38 | + 'fielddata' => true, |
| 39 | + ], |
| 40 | + 'email' => [ |
| 41 | + 'type' => 'keyword', |
| 42 | + ], |
| 43 | + ], |
| 44 | + ]); |
| 45 | +}); |
| 46 | + |
| 47 | +afterEach(function (): void { |
| 48 | + if (isset($this->indexManager, $this->indexName) && $this->indexManager->exists($this->indexName)) { |
| 49 | + $this->indexManager->drop($this->indexName); |
| 50 | + } |
| 51 | + |
| 52 | + Schema::dropIfExists('clients'); |
| 53 | +}); |
| 54 | + |
| 55 | +it('indexes searches paginates deletes and flushes models against opensearch', function (): void { |
| 56 | + $engine = app(EngineManager::class)->engine('opensearch'); |
| 57 | + |
| 58 | + $engine->update(Client::query()->orderBy('id')->get()); |
| 59 | + |
| 60 | + expect(Client::search('Smith')->get()->pluck('id')->all())->toBe([1, 2]) |
| 61 | + ->and(Client::search('')->where('email', 'jane@example.com')->get()->pluck('id')->all())->toBe([2]) |
| 62 | + ->and(Client::search('')->whereIn('email', ['john@example.com', 'taylor@example.com'])->get()->pluck('id')->all())->toBe([1, 3]) |
| 63 | + ->and(Client::search('')->orderBy('name', 'asc')->paginate(2)->pluck('id')->all())->toBe([2, 1]); |
| 64 | + |
| 65 | + $engine->delete(Client::query()->whereKey(1)->get()); |
| 66 | + |
| 67 | + expect(Client::search('John')->get())->toHaveCount(0); |
| 68 | + |
| 69 | + $engine->flush(new Client); |
| 70 | + |
| 71 | + expect(Client::search('')->get())->toHaveCount(0); |
| 72 | +}); |
0 commit comments