|
7 | 7 | use DirectoryTree\OpenSearchAdapter\Indices\Mapping; |
8 | 8 | use DirectoryTree\OpenSearchAdapter\Indices\Settings; |
9 | 9 | use DirectoryTree\OpenSearchAdapter\Testing\Fakes\FakeIndexManager; |
| 10 | +use PHPUnit\Framework\ExpectationFailedException; |
10 | 11 |
|
11 | 12 | it('implements the index manager contract', function () { |
12 | 13 | expect(new FakeIndexManager)->toBeInstanceOf(IndexManagerInterface::class); |
|
38 | 39 | expect($indices->exists('posts'))->toBeTrue(); |
39 | 40 | }); |
40 | 41 |
|
| 42 | +it('asserts created indices by name', function () { |
| 43 | + $indices = new FakeIndexManager; |
| 44 | + |
| 45 | + $indices->create(new IndexBlueprint( |
| 46 | + 'posts', |
| 47 | + (new Mapping)->text('title'), |
| 48 | + (new Settings)->index(['number_of_replicas' => 0]) |
| 49 | + )); |
| 50 | + |
| 51 | + $indices |
| 52 | + ->assertCreated('posts') |
| 53 | + ->assertCreated('posts', fn (IndexBlueprint $index): bool => ( |
| 54 | + $index->mapping()?->toArray() === [ |
| 55 | + 'properties' => [ |
| 56 | + 'title' => ['type' => 'text'], |
| 57 | + ], |
| 58 | + ] |
| 59 | + && $index->settings()?->toArray() === [ |
| 60 | + 'index' => ['number_of_replicas' => 0], |
| 61 | + ] |
| 62 | + )); |
| 63 | +}); |
| 64 | + |
| 65 | +it('fails when no created index satisfies the assertion', function () { |
| 66 | + $indices = new FakeIndexManager; |
| 67 | + |
| 68 | + $indices->create(new IndexBlueprint('posts')); |
| 69 | + |
| 70 | + expect(fn () => $indices->assertCreated('comments')) |
| 71 | + ->toThrow(ExpectationFailedException::class); |
| 72 | + |
| 73 | + expect(fn () => $indices->assertCreated('posts', fn (): bool => false)) |
| 74 | + ->toThrow(ExpectationFailedException::class); |
| 75 | +}); |
| 76 | + |
41 | 77 | it('records mapping updates', function () { |
42 | 78 | $indices = new FakeIndexManager; |
43 | 79 |
|
44 | 80 | $indices->putMapping('posts', $mapping = (new Mapping)->keyword('status')); |
45 | 81 |
|
46 | | - $indices->assertMappingPut('posts', $mapping); |
| 82 | + $indices |
| 83 | + ->assertMappingPut('posts', $mapping) |
| 84 | + ->assertMappingPut('posts') |
| 85 | + ->assertMappingPut('posts', fn (Mapping $mapping): bool => $mapping->toArray() === [ |
| 86 | + 'properties' => [ |
| 87 | + 'status' => ['type' => 'keyword'], |
| 88 | + ], |
| 89 | + ]); |
| 90 | +}); |
| 91 | + |
| 92 | +it('fails when no mapping update satisfies the assertion', function () { |
| 93 | + $indices = new FakeIndexManager; |
| 94 | + |
| 95 | + $indices->putMapping('posts', (new Mapping)->keyword('status')); |
| 96 | + |
| 97 | + expect(fn () => $indices->assertMappingPut('comments')) |
| 98 | + ->toThrow(ExpectationFailedException::class); |
| 99 | + |
| 100 | + expect(fn () => $indices->assertMappingPut('posts', fn (): bool => false)) |
| 101 | + ->toThrow(ExpectationFailedException::class); |
47 | 102 | }); |
48 | 103 |
|
49 | 104 | it('records settings updates', function () { |
50 | 105 | $indices = new FakeIndexManager; |
51 | 106 |
|
52 | 107 | $indices->putSettings('posts', $settings = (new Settings)->index(['refresh_interval' => -1])); |
53 | 108 |
|
54 | | - $indices->assertSettingsPut('posts', $settings); |
| 109 | + $indices |
| 110 | + ->assertSettingsPut('posts', $settings) |
| 111 | + ->assertSettingsPut('posts') |
| 112 | + ->assertSettingsPut('posts', fn (Settings $settings): bool => $settings->toArray() === [ |
| 113 | + 'index' => ['refresh_interval' => -1], |
| 114 | + ]); |
| 115 | +}); |
| 116 | + |
| 117 | +it('fails when no settings update satisfies the assertion', function () { |
| 118 | + $indices = new FakeIndexManager; |
| 119 | + |
| 120 | + $indices->putSettings('posts', (new Settings)->index(['refresh_interval' => -1])); |
| 121 | + |
| 122 | + expect(fn () => $indices->assertSettingsPut('comments')) |
| 123 | + ->toThrow(ExpectationFailedException::class); |
| 124 | + |
| 125 | + expect(fn () => $indices->assertSettingsPut('posts', fn (): bool => false)) |
| 126 | + ->toThrow(ExpectationFailedException::class); |
55 | 127 | }); |
56 | 128 |
|
57 | 129 | it('records open and close operations', function () { |
|
0 commit comments