Skip to content

Commit 5e5e1e5

Browse files
authored
Merge pull request #3 from ensi-platform/dev-clear-testindex
Test index rebuild setup.
2 parents 0bab241 + 81513ce commit 5e5e1e5

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
</testsuites>
2626
<php>
2727
<server name="ELASTICSEARCH_HOSTS" value="http://127.0.0.1:9200"/>
28+
<server name="RECREATE_INDEX" value="true"/>
2829
</php>
2930
<coverage>
3031
<include>

tests/Data/IndexSeeder.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ abstract class IndexSeeder
99
protected string $indexName = '';
1010
protected array $mappings = [];
1111
protected array $settings = [];
12+
1213
protected array $fixtures = [];
1314

14-
protected bool $recreate = false;
15+
protected bool $recreate;
1516

1617
protected ?Client $client;
1718

19+
public function __construct()
20+
{
21+
$this->recreate = config('tests.recreate_index', true);
22+
}
23+
1824
public function setClient(Client $client): void
1925
{
2026
$this->client = $client;
@@ -75,23 +81,32 @@ protected function loadFixtures(): void
7581
{
7682
$baseDir = __DIR__.'/fixtures/';
7783

78-
foreach ($this->fixtures as $fixture) {
79-
$this->loadFixture($baseDir.$fixture);
84+
$hasChanges = collect($this->fixtures)
85+
->reduce(
86+
fn (bool $carry, string $fixture) => $this->loadFixture($baseDir.$fixture) || $carry,
87+
false
88+
);
89+
90+
if ($hasChanges) {
91+
$this->client->indices()->refresh(['index' => $this->indexName]);
8092
}
8193
}
8294

83-
protected function loadFixture(string $path): void
95+
protected function loadFixture(string $path): bool
8496
{
8597
$documents = json_decode(file_get_contents($path), true);
98+
8699
if (empty($documents)) {
87-
return;
100+
return false;
88101
}
89102

90103
$body = collect($documents)
91104
->flatMap(fn (array $document, int $index) => $this->documentToCommand($document, $index))
92105
->toArray();
93106

94107
$this->client->bulk(['body' => $body]);
108+
109+
return true;
95110
}
96111

97112
protected function documentToCommand(array $document, int $id): array

tests/Data/ProductsIndex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class ProductsIndex extends ElasticIndex
88
{
9-
protected string $name = 'test_products';
9+
protected string $name = 'test_spec_products';
1010

1111
protected string $tiebreaker = 'product_id';
1212
}

tests/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ protected function getPackageProviders($app): array
3232
public function getEnvironmentSetUp($app)
3333
{
3434
config()->set('laravel-elastic-query.connection.hosts', explode(',', env('ELASTICSEARCH_HOSTS')));
35+
config()->set('tests.recreate_index', env('RECREATE_INDEX', true));
3536
}
3637
}

0 commit comments

Comments
 (0)