1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Elasticsearch\Test; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Elasticsearch\Client; |
7
|
|
|
use Shopware\Core\DevOps\Environment\EnvironmentHelper; |
8
|
|
|
use Shopware\Elasticsearch\Admin\AdminElasticsearchHelper; |
9
|
|
|
use Shopware\Elasticsearch\Framework\Command\ElasticsearchAdminIndexingCommand; |
10
|
|
|
use Shopware\Elasticsearch\Framework\ElasticsearchHelper; |
11
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
12
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
14
|
|
|
|
15
|
|
|
trait AdminElasticsearchTestBehaviour |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @before |
19
|
|
|
*/ |
20
|
|
|
public function enableElasticsearch(): void |
21
|
|
|
{ |
22
|
|
|
$this->getDiContainer() |
23
|
|
|
->get(ElasticsearchHelper::class) |
24
|
|
|
->setEnabled(true); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @after |
29
|
|
|
*/ |
30
|
|
|
public function disableElasticsearch(): void |
31
|
|
|
{ |
32
|
|
|
$this->getDiContainer() |
33
|
|
|
->get(ElasticsearchHelper::class) |
34
|
|
|
->setEnabled(false); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @before |
39
|
|
|
*/ |
40
|
|
|
public function enableAdminElasticsearch(): void |
41
|
|
|
{ |
42
|
|
|
$this->getDiContainer() |
43
|
|
|
->get(AdminElasticsearchHelper::class) |
44
|
|
|
->setEnabled(true); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @after |
49
|
|
|
*/ |
50
|
|
|
public function disableAdminElasticsearch(): void |
51
|
|
|
{ |
52
|
|
|
$this->getDiContainer() |
53
|
|
|
->get(AdminElasticsearchHelper::class) |
54
|
|
|
->setEnabled(false); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array<string, mixed> $input |
59
|
|
|
*/ |
60
|
|
|
public function indexElasticSearch(array $input = []): void |
61
|
|
|
{ |
62
|
|
|
$this->getDiContainer() |
63
|
|
|
->get(ElasticsearchAdminIndexingCommand::class) |
64
|
|
|
->run(new ArrayInput(array_merge($input, ['--no-queue' => true])), new NullOutput()); |
65
|
|
|
|
66
|
|
|
$this->runWorker(); |
67
|
|
|
|
68
|
|
|
$this->refreshIndex(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function refreshIndex(): void |
72
|
|
|
{ |
73
|
|
|
$this->getDiContainer()->get(Client::class) |
74
|
|
|
->indices() |
75
|
|
|
->refresh(['index' => '_all']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
abstract protected function getDiContainer(): ContainerInterface; |
79
|
|
|
|
80
|
|
|
abstract protected function runWorker(): void; |
81
|
|
|
|
82
|
|
|
protected function clearElasticsearch(): void |
83
|
|
|
{ |
84
|
|
|
$c = $this->getDiContainer(); |
85
|
|
|
|
86
|
|
|
$client = $c->get(Client::class); |
87
|
|
|
|
88
|
|
|
$indices = $client->indices()->get(['index' => EnvironmentHelper::getVariable('SHOPWARE_ADMIN_ES_INDEX_PREFIX') . '*']); |
89
|
|
|
|
90
|
|
|
foreach ($indices as $index) { |
91
|
|
|
$client->indices()->delete(['index' => $index['settings']['index']['provided_name']]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$connection = $c->get(Connection::class); |
95
|
|
|
$connection->executeStatement('TRUNCATE admin_elasticsearch_index_task'); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|