Skip to content

Commit 0397366

Browse files
authored
Merge pull request #4 from DirectoryTree/agent/fix-index-fake-assertions
Support predicate callbacks in fake index assertions
2 parents 375af0a + 7a10dac commit 0397366

2 files changed

Lines changed: 143 additions & 8 deletions

File tree

src/Testing/Fakes/FakeIndexManager.php

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,29 @@ public function assertChecked(string $index): static
272272

273273
/**
274274
* Assert that the given index was created.
275+
*
276+
* @param (callable(IndexBlueprint): bool)|null $callback
275277
*/
276-
public function assertCreated(IndexBlueprint $index): static
278+
public function assertCreated(IndexBlueprint|string $index, ?callable $callback = null): static
277279
{
278-
PHPUnit::assertContainsEquals($index, $this->created);
280+
if ($index instanceof IndexBlueprint) {
281+
PHPUnit::assertContainsEquals($index, $this->created);
282+
283+
return $this;
284+
}
285+
286+
$created = array_filter(
287+
$this->created,
288+
fn (IndexBlueprint $created): bool => (
289+
$created->name() === $index
290+
&& (! isset($callback) || $callback($created))
291+
)
292+
);
293+
294+
PHPUnit::assertNotEmpty(
295+
$created,
296+
"The expected index [{$index}] was not created."
297+
);
279298

280299
return $this;
281300
}
@@ -294,20 +313,64 @@ public function assertNotCreated(string $index): static
294313

295314
/**
296315
* Assert that the given index mapping was updated.
316+
*
317+
* @param Mapping|(callable(Mapping): bool)|null $assertion
297318
*/
298-
public function assertMappingPut(string $index, Mapping $mapping): static
319+
public function assertMappingPut(string $index, Mapping|callable|null $assertion = null): static
299320
{
300-
PHPUnit::assertContainsEquals(compact('index', 'mapping'), $this->mappings);
321+
if ($assertion instanceof Mapping) {
322+
PHPUnit::assertContainsEquals([
323+
'index' => $index,
324+
'mapping' => $assertion,
325+
], $this->mappings);
326+
327+
return $this;
328+
}
329+
330+
$mappings = array_filter(
331+
$this->mappings,
332+
fn (array $operation): bool => (
333+
$operation['index'] === $index
334+
&& (! isset($assertion) || $assertion($operation['mapping']))
335+
)
336+
);
337+
338+
PHPUnit::assertNotEmpty(
339+
$mappings,
340+
"The expected mapping for index [{$index}] was not updated."
341+
);
301342

302343
return $this;
303344
}
304345

305346
/**
306347
* Assert that the given index settings were updated.
348+
*
349+
* @param Settings|(callable(Settings): bool)|null $assertion
307350
*/
308-
public function assertSettingsPut(string $index, Settings $settings): static
351+
public function assertSettingsPut(string $index, Settings|callable|null $assertion = null): static
309352
{
310-
PHPUnit::assertContainsEquals(compact('index', 'settings'), $this->settings);
353+
if ($assertion instanceof Settings) {
354+
PHPUnit::assertContainsEquals([
355+
'index' => $index,
356+
'settings' => $assertion,
357+
], $this->settings);
358+
359+
return $this;
360+
}
361+
362+
$settings = array_filter(
363+
$this->settings,
364+
fn (array $operation): bool => (
365+
$operation['index'] === $index
366+
&& (! isset($assertion) || $assertion($operation['settings']))
367+
)
368+
);
369+
370+
PHPUnit::assertNotEmpty(
371+
$settings,
372+
"The expected settings for index [{$index}] were not updated."
373+
);
311374

312375
return $this;
313376
}

tests/Unit/Indices/FakeIndexManagerTest.php

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use DirectoryTree\OpenSearchAdapter\Indices\Mapping;
88
use DirectoryTree\OpenSearchAdapter\Indices\Settings;
99
use DirectoryTree\OpenSearchAdapter\Testing\Fakes\FakeIndexManager;
10+
use PHPUnit\Framework\ExpectationFailedException;
1011

1112
it('implements the index manager contract', function () {
1213
expect(new FakeIndexManager)->toBeInstanceOf(IndexManagerInterface::class);
@@ -38,20 +39,91 @@
3839
expect($indices->exists('posts'))->toBeTrue();
3940
});
4041

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+
4177
it('records mapping updates', function () {
4278
$indices = new FakeIndexManager;
4379

4480
$indices->putMapping('posts', $mapping = (new Mapping)->keyword('status'));
4581

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);
47102
});
48103

49104
it('records settings updates', function () {
50105
$indices = new FakeIndexManager;
51106

52107
$indices->putSettings('posts', $settings = (new Settings)->index(['refresh_interval' => -1]));
53108

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);
55127
});
56128

57129
it('records open and close operations', function () {

0 commit comments

Comments
 (0)