-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFakeIndexManagerTest.php
More file actions
204 lines (150 loc) · 6.11 KB
/
Copy pathFakeIndexManagerTest.php
File metadata and controls
204 lines (150 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
use DirectoryTree\OpenSearchAdapter\Indices\Alias;
use DirectoryTree\OpenSearchAdapter\Indices\AliasActions;
use DirectoryTree\OpenSearchAdapter\Indices\IndexBlueprint;
use DirectoryTree\OpenSearchAdapter\Indices\IndexManagerInterface;
use DirectoryTree\OpenSearchAdapter\Indices\Mapping;
use DirectoryTree\OpenSearchAdapter\Indices\Settings;
use DirectoryTree\OpenSearchAdapter\Testing\Fakes\FakeIndexManager;
use PHPUnit\Framework\ExpectationFailedException;
it('implements the index manager contract', function () {
expect(new FakeIndexManager)->toBeInstanceOf(IndexManagerInterface::class);
});
it('tracks index existence', function () {
$indices = new FakeIndexManager;
expect($indices->exists('posts'))->toBeFalse();
$indices->assertChecked('posts');
$indices = new FakeIndexManager(existing: ['posts']);
expect($indices->exists('posts'))->toBeTrue();
});
it('records created indices', function () {
$indices = new FakeIndexManager;
$indices->create($index = new IndexBlueprint(
'posts',
(new Mapping)->text('title'),
(new Settings)->index(['number_of_replicas' => 0])
));
$indices->assertCreated($index);
expect($indices->exists('posts'))->toBeTrue();
});
it('asserts created indices by name', function () {
$indices = new FakeIndexManager;
$indices->create(new IndexBlueprint(
'posts',
(new Mapping)->text('title'),
(new Settings)->index(['number_of_replicas' => 0])
));
$indices
->assertCreated('posts')
->assertCreated('posts', fn (IndexBlueprint $index): bool => (
$index->mapping()?->toArray() === [
'properties' => [
'title' => ['type' => 'text'],
],
]
&& $index->settings()?->toArray() === [
'index' => ['number_of_replicas' => 0],
]
));
});
it('fails when no created index satisfies the assertion', function () {
$indices = new FakeIndexManager;
$indices->create(new IndexBlueprint('posts'));
expect(fn () => $indices->assertCreated('comments'))
->toThrow(ExpectationFailedException::class);
expect(fn () => $indices->assertCreated('posts', fn (): bool => false))
->toThrow(ExpectationFailedException::class);
});
it('records mapping updates', function () {
$indices = new FakeIndexManager;
$indices->putMapping('posts', $mapping = (new Mapping)->keyword('status'));
$indices
->assertMappingPut('posts', $mapping)
->assertMappingPut('posts')
->assertMappingPut('posts', fn (Mapping $mapping): bool => $mapping->toArray() === [
'properties' => [
'status' => ['type' => 'keyword'],
],
]);
});
it('fails when no mapping update satisfies the assertion', function () {
$indices = new FakeIndexManager;
$indices->putMapping('posts', (new Mapping)->keyword('status'));
expect(fn () => $indices->assertMappingPut('comments'))
->toThrow(ExpectationFailedException::class);
expect(fn () => $indices->assertMappingPut('posts', fn (): bool => false))
->toThrow(ExpectationFailedException::class);
});
it('records settings updates', function () {
$indices = new FakeIndexManager;
$indices->putSettings('posts', $settings = (new Settings)->index(['refresh_interval' => -1]));
$indices
->assertSettingsPut('posts', $settings)
->assertSettingsPut('posts')
->assertSettingsPut('posts', fn (Settings $settings): bool => $settings->toArray() === [
'index' => ['refresh_interval' => -1],
]);
});
it('fails when no settings update satisfies the assertion', function () {
$indices = new FakeIndexManager;
$indices->putSettings('posts', (new Settings)->index(['refresh_interval' => -1]));
expect(fn () => $indices->assertSettingsPut('comments'))
->toThrow(ExpectationFailedException::class);
expect(fn () => $indices->assertSettingsPut('posts', fn (): bool => false))
->toThrow(ExpectationFailedException::class);
});
it('records open and close operations', function () {
$indices = new FakeIndexManager;
$indices
->close('posts')
->open('posts')
->assertClosed('posts')
->assertOpened('posts');
});
it('records deleted indices', function () {
$indices = new FakeIndexManager(existing: ['posts']);
$indices->delete('posts');
$indices->assertDeleted('posts');
expect($indices->exists('posts'))->toBeFalse();
});
it('records aliases', function () {
$indices = new FakeIndexManager;
$alias = new Alias('published_posts', [
'term' => ['status' => 'published'],
], 'tenant-1');
$indices->putAlias('posts', $alias);
$indices->assertAliasPut('posts', $alias);
expect($indices->getAliases('posts'))->toBe([
'published_posts' => $alias,
]);
});
it('records deleted aliases', function () {
$indices = new FakeIndexManager;
$indices->putAlias('posts', new Alias('published_posts'));
$indices->deleteAlias('posts', 'published_posts');
$indices->assertAliasDeleted('posts', 'published_posts');
expect($indices->getAliases('posts'))->toBe([]);
});
it('applies atomic alias updates', function () {
$indices = new FakeIndexManager(existing: [
'posts_blue',
'posts_green',
'posts_retired',
]);
$indices->putAlias('posts_blue', new Alias('posts', isWriteIndex: true));
$actions = (new AliasActions)
->remove('posts_blue', 'posts')
->add('posts_green', new Alias('posts', isWriteIndex: true))
->removeIndex('posts_retired');
$indices->updateAliases($actions);
$indices
->assertAliasesUpdated($actions)
->assertAliasDeleted('posts_blue', 'posts')
->assertAliasPut('posts_green', new Alias('posts', isWriteIndex: true))
->assertDeleted('posts_retired');
expect($indices->getAliases('posts_blue'))->toBe([])
->and($indices->getAliases('posts_green'))->toEqual([
'posts' => new Alias('posts', isWriteIndex: true),
])
->and($indices->exists('posts_retired'))->toBeFalse();
});