Skip to content

Commit 2fbdbb5

Browse files
authored
Merge pull request #3 from ensi-platform/task-clear-testindex
Test index rebuild setup.
2 parents 6eb4b09 + e6cf5ec commit 2fbdbb5

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

phpunit.xml.dist

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
3+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
44
backupGlobals="false"
55
backupStaticAttributes="false"
66
bootstrap="vendor/autoload.php"
@@ -24,6 +24,7 @@
2424

2525
<php>
2626
<server name="ELASTICSEARCH_HOSTS" value="http://127.0.0.1:9200"/>
27+
<server name="RECREATE_INDEX" value="true"/>
2728
</php>
2829

2930
<coverage processUncoveredFiles="true">

tests/Functional/ElasticTestCase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ protected function getPackageProviders($app): array
1818
];
1919
}
2020

21+
protected function getEnvironmentSetUp($app)
22+
{
23+
config()->set('tests.recreate_index', env('RECREATE_INDEX', true));
24+
}
25+
2126
protected function tearDown(): void
2227
{
2328
ElasticQuery::disableQueryLog();

tests/Seeds/IndexSeeder.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ abstract class IndexSeeder
1111
protected array $settings = [];
1212
protected array $fixtures = [];
1313

14-
protected bool $recreate = false;
14+
protected bool $recreate;
1515

1616
protected ?Client $client;
1717

18+
public function __construct()
19+
{
20+
$this->recreate = config('tests.recreate_index', true);
21+
}
22+
1823
public function setClient(Client $client): void
1924
{
2025
$this->client = $client;
@@ -75,23 +80,32 @@ protected function loadFixtures(): void
7580
{
7681
$baseDir = __DIR__.'/fixtures/';
7782

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

83-
protected function loadFixture(string $path): void
94+
protected function loadFixture(string $path): bool
8495
{
8596
$documents = json_decode(file_get_contents($path), true);
97+
8698
if (empty($documents)) {
87-
return;
99+
return false;
88100
}
89101

90102
$body = collect($documents)
91103
->flatMap(fn (array $document, int $index) => $this->documentToCommand($document, $index))
92104
->toArray();
93105

94106
$this->client->bulk(['body' => $body]);
107+
108+
return true;
95109
}
96110

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

0 commit comments

Comments
 (0)