Passed
Push — 6.4 ( be76f2...e19c4d )
by Christian
14:08 queued 13s
created

AdminElasticsearchTestBehaviour   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 81
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A enableElasticsearch() 0 5 1
A refreshIndex() 0 5 1
A enableAdminElasticsearch() 0 5 1
A disableAdminElasticsearch() 0 5 1
A indexElasticSearch() 0 9 1
A disableElasticsearch() 0 5 1
A clearElasticsearch() 0 14 2
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