-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFakeIndexManager.php
More file actions
449 lines (371 loc) · 10.8 KB
/
Copy pathFakeIndexManager.php
File metadata and controls
449 lines (371 loc) · 10.8 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<?php
namespace DirectoryTree\OpenSearchAdapter\Testing\Fakes;
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 PHPUnit\Framework\Assert as PHPUnit;
/**
* Fakes OpenSearch index operations for tests.
*/
class FakeIndexManager implements IndexManagerInterface
{
/**
* The opened index names.
*
* @var array<int, string>
*/
protected array $opened = [];
/**
* The closed index names.
*
* @var array<int, string>
*/
protected array $closed = [];
/**
* The checked index names.
*
* @var array<int, string>
*/
protected array $checked = [];
/**
* The created index blueprints.
*
* @var array<int, IndexBlueprint>
*/
protected array $created = [];
/**
* The updated index mappings.
*
* @var array<int, array{index: string, mapping: Mapping}>
*/
protected array $mappings = [];
/**
* The updated index settings.
*
* @var array<int, array{index: string, settings: Settings}>
*/
protected array $settings = [];
/**
* The deleted index names.
*
* @var array<int, string>
*/
protected array $deleted = [];
/**
* The aliases put on indices.
*
* @var array<int, array{index: string, alias: Alias}>
*/
protected array $aliases = [];
/**
* The atomic alias updates.
*
* @var array<int, AliasActions>
*/
protected array $aliasUpdates = [];
/**
* The deleted aliases.
*
* @var array<int, array{index: string, alias: string}>
*/
protected array $deletedAliases = [];
/**
* Create a new fake index manager instance.
*
* @param array<int, string> $existing
*/
public function __construct(
protected array $existing = [],
) {}
/**
* Open the given index.
*/
public function open(string $index): static
{
$this->opened[] = $index;
return $this;
}
/**
* Close the given index.
*/
public function close(string $index): static
{
$this->closed[] = $index;
return $this;
}
/**
* Determine if the given index exists.
*/
public function exists(string $index): bool
{
$this->checked[] = $index;
return in_array($index, $this->existing, true);
}
/**
* Create an index from the given blueprint.
*/
public function create(IndexBlueprint $index): static
{
$this->created[] = $index;
$this->existing[] = $index->name();
return $this;
}
/**
* Update the mapping for the given index.
*/
public function putMapping(string $index, Mapping $mapping): static
{
$this->mappings[] = compact('index', 'mapping');
return $this;
}
/**
* Update the settings for the given index.
*/
public function putSettings(string $index, Settings $settings): static
{
$this->settings[] = compact('index', 'settings');
return $this;
}
/**
* Delete the given index.
*/
public function delete(string $index): static
{
$this->deleted[] = $index;
$this->existing = array_values(
array_diff($this->existing, [$index])
);
$this->aliases = array_values(array_filter(
$this->aliases,
fn (array $operation) => $operation['index'] !== $index,
));
return $this;
}
/**
* Get the aliases for the given index.
*
* @return array<string, Alias>
*/
public function getAliases(string $index): array
{
$aliases = [];
foreach ($this->aliases as $operation) {
if ($operation['index'] === $index) {
$aliases[$operation['alias']->name()] = $operation['alias'];
}
}
return $aliases;
}
/**
* Create or update an alias for the given index.
*/
public function putAlias(string $index, Alias $alias): static
{
$this->removeAlias($index, $alias->name());
$this->aliases[] = compact('index', 'alias');
return $this;
}
/**
* Delete the given alias from the index.
*/
public function deleteAlias(string $index, string $aliasName): static
{
$this->deletedAliases[] = [
'index' => $index,
'alias' => $aliasName,
];
$this->removeAlias($index, $aliasName);
return $this;
}
/**
* Atomically apply multiple alias actions.
*/
public function updateAliases(AliasActions $actions): static
{
$this->aliasUpdates[] = $actions;
foreach ($actions->actions() as $action) {
if ($parameters = $action['add'] ?? null) {
$alias = new Alias(
$parameters['alias'],
$parameters['filter'] ?? null,
$parameters['routing'] ?? null,
$parameters['is_write_index'] ?? null,
);
$this->removeAlias($parameters['index'], $parameters['alias']);
$this->aliases[] = [
'index' => $parameters['index'],
'alias' => $alias,
];
}
if ($parameters = $action['remove'] ?? null) {
$this->deletedAliases[] = [
'index' => $parameters['index'],
'alias' => $parameters['alias'],
];
$this->removeAlias($parameters['index'], $parameters['alias']);
}
if ($parameters = $action['remove_index'] ?? null) {
$this->delete($parameters['index']);
}
}
return $this;
}
/**
* Assert that the given index was checked.
*/
public function assertChecked(string $index): static
{
PHPUnit::assertContains($index, $this->checked);
return $this;
}
/**
* Assert that the given index was created.
*
* @param (callable(IndexBlueprint): bool)|null $callback
*/
public function assertCreated(IndexBlueprint|string $index, ?callable $callback = null): static
{
if ($index instanceof IndexBlueprint) {
PHPUnit::assertContainsEquals($index, $this->created);
return $this;
}
$created = array_filter(
$this->created,
fn (IndexBlueprint $created): bool => (
$created->name() === $index
&& (! isset($callback) || $callback($created))
)
);
PHPUnit::assertNotEmpty(
$created,
"The expected index [{$index}] was not created."
);
return $this;
}
/**
* Assert that the given index was not created.
*/
public function assertNotCreated(string $index): static
{
foreach ($this->created as $created) {
PHPUnit::assertNotSame($index, $created->name());
}
return $this;
}
/**
* Assert that the given index mapping was updated.
*
* @param Mapping|(callable(Mapping): bool)|null $assertion
*/
public function assertMappingPut(string $index, Mapping|callable|null $assertion = null): static
{
if ($assertion instanceof Mapping) {
PHPUnit::assertContainsEquals([
'index' => $index,
'mapping' => $assertion,
], $this->mappings);
return $this;
}
$mappings = array_filter(
$this->mappings,
fn (array $operation): bool => (
$operation['index'] === $index
&& (! isset($assertion) || $assertion($operation['mapping']))
)
);
PHPUnit::assertNotEmpty(
$mappings,
"The expected mapping for index [{$index}] was not updated."
);
return $this;
}
/**
* Assert that the given index settings were updated.
*
* @param Settings|(callable(Settings): bool)|null $assertion
*/
public function assertSettingsPut(string $index, Settings|callable|null $assertion = null): static
{
if ($assertion instanceof Settings) {
PHPUnit::assertContainsEquals([
'index' => $index,
'settings' => $assertion,
], $this->settings);
return $this;
}
$settings = array_filter(
$this->settings,
fn (array $operation): bool => (
$operation['index'] === $index
&& (! isset($assertion) || $assertion($operation['settings']))
)
);
PHPUnit::assertNotEmpty(
$settings,
"The expected settings for index [{$index}] were not updated."
);
return $this;
}
/**
* Assert that the given index was opened.
*/
public function assertOpened(string $index): static
{
PHPUnit::assertContains($index, $this->opened);
return $this;
}
/**
* Assert that the given index was closed.
*/
public function assertClosed(string $index): static
{
PHPUnit::assertContains($index, $this->closed);
return $this;
}
/**
* Assert that the given index was deleted.
*/
public function assertDeleted(string $index): static
{
PHPUnit::assertContains($index, $this->deleted);
return $this;
}
/**
* Assert that the given alias was put on an index.
*/
public function assertAliasPut(string $index, Alias $alias): static
{
PHPUnit::assertContainsEquals(compact('index', 'alias'), $this->aliases);
return $this;
}
/**
* Assert that the given alias was deleted from an index.
*/
public function assertAliasDeleted(string $index, string $alias): static
{
PHPUnit::assertContainsEquals(compact('index', 'alias'), $this->deletedAliases);
return $this;
}
/**
* Assert that the given atomic alias update was performed.
*/
public function assertAliasesUpdated(AliasActions $actions): static
{
PHPUnit::assertContainsEquals($actions, $this->aliasUpdates);
return $this;
}
/**
* Remove an alias from the fake's current state.
*/
protected function removeAlias(string $index, string $alias): void
{
$this->aliases = array_values(
array_filter($this->aliases, fn (array $operation) => (
$operation['index'] !== $index || $operation['alias']->name() !== $alias
))
);
}
}