|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +use DirectoryTree\OpenSearchAdapter\Indices\Mapping; |
| 4 | +use DirectoryTree\OpenSearchAdapter\Indices\Settings; |
3 | 5 | use DirectoryTree\OpenSearchMigrations\Facades\Index; |
4 | 6 | use DirectoryTree\OpenSearchMigrations\IndexManagerInterface; |
5 | 7 | use DirectoryTree\OpenSearchMigrations\Testing\Fakes\FakeIndexManager; |
| 8 | +use PHPUnit\Framework\ExpectationFailedException; |
6 | 9 |
|
7 | 10 | it('resolves the index manager interface', function (): void { |
8 | 11 | expect(Index::getFacadeRoot())->toBeInstanceOf(IndexManagerInterface::class); |
|
14 | 17 |
|
15 | 18 | $fake = Index::fake(); |
16 | 19 |
|
17 | | - Index::create('posts'); |
| 20 | + Index::create('posts', function (Mapping $mapping, Settings $settings): void { |
| 21 | + $mapping->text('title'); |
| 22 | + $settings->index(['number_of_replicas' => 0]); |
| 23 | + }); |
18 | 24 | Index::putAlias('posts', 'published_posts'); |
19 | 25 |
|
20 | 26 | expect(Index::getFacadeRoot())->toBe($fake) |
|
26 | 32 | ->assertAliasPut('posts', 'published_posts'); |
27 | 33 | }); |
28 | 34 |
|
| 35 | +it('asserts created index definitions with a callback', function (): void { |
| 36 | + config()->set('opensearch-migrations.index_name_prefix', 'tenant_'); |
| 37 | + |
| 38 | + $fake = Index::fake(); |
| 39 | + |
| 40 | + Index::create('posts', function (Mapping $mapping, Settings $settings): void { |
| 41 | + $mapping->text('title'); |
| 42 | + $settings->index(['number_of_replicas' => 0]); |
| 43 | + }); |
| 44 | + |
| 45 | + $fake->assertCreated( |
| 46 | + 'posts', |
| 47 | + fn (?Mapping $mapping, ?Settings $settings): bool => ( |
| 48 | + $mapping?->toArray() === [ |
| 49 | + 'properties' => [ |
| 50 | + 'title' => ['type' => 'text'], |
| 51 | + ], |
| 52 | + ] |
| 53 | + && $settings?->toArray() === [ |
| 54 | + 'index' => ['number_of_replicas' => 0], |
| 55 | + ] |
| 56 | + ) |
| 57 | + ); |
| 58 | +}); |
| 59 | + |
| 60 | +it('fails when a created index does not satisfy the callback', function (): void { |
| 61 | + $fake = Index::fake(); |
| 62 | + |
| 63 | + Index::create('posts'); |
| 64 | + |
| 65 | + expect(fn () => $fake->assertCreated('posts', fn (): bool => false)) |
| 66 | + ->toThrow(ExpectationFailedException::class); |
| 67 | +}); |
| 68 | + |
| 69 | +it('asserts updated mappings with an optional callback', function (): void { |
| 70 | + config()->set('opensearch-migrations.index_name_prefix', 'tenant_'); |
| 71 | + |
| 72 | + $fake = Index::fake(); |
| 73 | + |
| 74 | + Index::putMapping('posts', function (Mapping $mapping): void { |
| 75 | + $mapping->keyword('status'); |
| 76 | + }); |
| 77 | + |
| 78 | + $fake |
| 79 | + ->assertMappingPut('posts') |
| 80 | + ->assertMappingPut('posts', fn (Mapping $mapping): bool => $mapping->toArray() === [ |
| 81 | + 'properties' => [ |
| 82 | + 'status' => ['type' => 'keyword'], |
| 83 | + ], |
| 84 | + ]); |
| 85 | +}); |
| 86 | + |
| 87 | +it('fails when an updated mapping does not satisfy the callback', function (): void { |
| 88 | + $fake = Index::fake(); |
| 89 | + |
| 90 | + Index::putMapping('posts', function (Mapping $mapping): void { |
| 91 | + $mapping->keyword('status'); |
| 92 | + }); |
| 93 | + |
| 94 | + expect(fn () => $fake->assertMappingPut('posts', fn (): bool => false)) |
| 95 | + ->toThrow(ExpectationFailedException::class); |
| 96 | +}); |
| 97 | + |
| 98 | +it('asserts updated settings with an optional callback', function (): void { |
| 99 | + config()->set('opensearch-migrations.index_name_prefix', 'tenant_'); |
| 100 | + |
| 101 | + $fake = Index::fake(); |
| 102 | + |
| 103 | + Index::putSettings('posts', function (Settings $settings): void { |
| 104 | + $settings->index(['refresh_interval' => -1]); |
| 105 | + }); |
| 106 | + |
| 107 | + $fake |
| 108 | + ->assertSettingsPut('posts') |
| 109 | + ->assertSettingsPut('posts', fn (Settings $settings): bool => $settings->toArray() === [ |
| 110 | + 'index' => ['refresh_interval' => -1], |
| 111 | + ]); |
| 112 | +}); |
| 113 | + |
| 114 | +it('fails when updated settings do not satisfy the callback', function (): void { |
| 115 | + $fake = Index::fake(); |
| 116 | + |
| 117 | + Index::putSettings('posts', function (Settings $settings): void { |
| 118 | + $settings->index(['refresh_interval' => -1]); |
| 119 | + }); |
| 120 | + |
| 121 | + expect(fn () => $fake->assertSettingsPut('posts', fn (): bool => false)) |
| 122 | + ->toThrow(ExpectationFailedException::class); |
| 123 | +}); |
| 124 | + |
29 | 125 | it('fakes existing indices', function (): void { |
30 | 126 | config()->set('opensearch-migrations.index_name_prefix', 'tenant_'); |
31 | 127 |
|
|
0 commit comments