Skip to content

Commit 0209d5c

Browse files
committed
Fix fake index assertion callbacks
1 parent f6afad2 commit 0209d5c

3 files changed

Lines changed: 127 additions & 30 deletions

File tree

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
"require": {
2929
"php": "^8.2",
30-
"directorytree/opensearch-adapter": "^1.0",
30+
"directorytree/opensearch-adapter": "^1.2.1",
3131
"directorytree/opensearch-client": "^1.0",
3232
"illuminate/console": "^11.0|^12.0|^13.0",
3333
"illuminate/database": "^11.0|^12.0|^13.0",
@@ -47,7 +47,7 @@
4747
"url": "../OpenSearchAdapter",
4848
"options": {
4949
"versions": {
50-
"directorytree/opensearch-adapter": "1.0.1"
50+
"directorytree/opensearch-adapter": "1.2.1"
5151
}
5252
}
5353
},

src/Testing/Fakes/FakeIndexManager.php

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,22 @@ public function assertChecked(string $index): static
144144

145145
/**
146146
* Assert that the given index was created.
147+
*
148+
* @param (callable(?Mapping, ?Settings): bool)|null $callback
147149
*/
148-
public function assertCreated(string $index, ?callable $modifier = null): static
150+
public function assertCreated(string $index, ?callable $callback = null): static
149151
{
150-
$this->manager->assertCreated($this->blueprint($index, $modifier));
152+
$assertion = isset($callback)
153+
? fn (IndexBlueprint $index): bool => $callback(
154+
$index->mapping(),
155+
$index->settings(),
156+
)
157+
: null;
158+
159+
$this->manager->assertCreated(
160+
MigrationPrefix::index($index),
161+
$assertion,
162+
);
151163

152164
return $this;
153165
}
@@ -164,24 +176,30 @@ public function assertNotCreated(string $index): static
164176

165177
/**
166178
* Assert that the given index mapping was updated.
179+
*
180+
* @param (callable(Mapping): bool)|null $callback
167181
*/
168-
public function assertMappingPut(string $index, callable $modifier): static
182+
public function assertMappingPut(string $index, ?callable $callback = null): static
169183
{
170-
$modifier($mapping = new Mapping);
171-
172-
$this->manager->assertMappingPut(MigrationPrefix::index($index), $mapping);
184+
$this->manager->assertMappingPut(
185+
MigrationPrefix::index($index),
186+
$callback,
187+
);
173188

174189
return $this;
175190
}
176191

177192
/**
178193
* Assert that the given index settings were updated.
194+
*
195+
* @param (callable(Settings): bool)|null $callback
179196
*/
180-
public function assertSettingsPut(string $index, callable $modifier): static
197+
public function assertSettingsPut(string $index, ?callable $callback = null): static
181198
{
182-
$modifier($settings = new Settings);
183-
184-
$this->manager->assertSettingsPut(MigrationPrefix::index($index), $settings);
199+
$this->manager->assertSettingsPut(
200+
MigrationPrefix::index($index),
201+
$callback,
202+
);
185203

186204
return $this;
187205
}
@@ -243,21 +261,4 @@ public function assertAliasDeleted(string $index, string $alias): static
243261

244262
return $this;
245263
}
246-
247-
/**
248-
* Create an index blueprint for an assertion.
249-
*/
250-
protected function blueprint(string $index, ?callable $modifier = null): IndexBlueprint
251-
{
252-
if (isset($modifier)) {
253-
$modifier(
254-
$mapping = new Mapping,
255-
$settings = new Settings,
256-
);
257-
258-
return new IndexBlueprint(MigrationPrefix::index($index), $mapping, $settings);
259-
}
260-
261-
return new IndexBlueprint(MigrationPrefix::index($index));
262-
}
263264
}

tests/Unit/Facades/IndexTest.php

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22

3+
use DirectoryTree\OpenSearchAdapter\Indices\Mapping;
4+
use DirectoryTree\OpenSearchAdapter\Indices\Settings;
35
use DirectoryTree\OpenSearchMigrations\Facades\Index;
46
use DirectoryTree\OpenSearchMigrations\IndexManagerInterface;
57
use DirectoryTree\OpenSearchMigrations\Testing\Fakes\FakeIndexManager;
8+
use PHPUnit\Framework\ExpectationFailedException;
69

710
it('resolves the index manager interface', function (): void {
811
expect(Index::getFacadeRoot())->toBeInstanceOf(IndexManagerInterface::class);
@@ -14,7 +17,10 @@
1417

1518
$fake = Index::fake();
1619

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+
});
1824
Index::putAlias('posts', 'published_posts');
1925

2026
expect(Index::getFacadeRoot())->toBe($fake)
@@ -26,6 +32,96 @@
2632
->assertAliasPut('posts', 'published_posts');
2733
});
2834

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+
29125
it('fakes existing indices', function (): void {
30126
config()->set('opensearch-migrations.index_name_prefix', 'tenant_');
31127

0 commit comments

Comments
 (0)