Passed
Push — feature/initial-implementation ( 77fab8...e556a2 )
by Fike
01:48
created

MappingApplicationTest::_before()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 8
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Test\Suite\Acceptance\Mapping;
6
7
use AmaTeam\ElasticSearch\Mapping\Mapping;
8
use AmaTeam\ElasticSearch\Mapping\Normalizer;
9
use AmaTeam\ElasticSearch\Mapping\TypeEnum;
10
use Codeception\Test\Unit;
11
use Elasticsearch\Client;
12
use Elasticsearch\ClientBuilder;
13
use Ramsey\Uuid\Uuid;
14
use Symfony\Component\Yaml\Yaml;
15
use Yandex\Allure\Adapter\Support\AttachmentSupport;
16
17
class MappingApplicationTest extends Unit
18
{
19
    use AttachmentSupport;
20
    /**
21
     * @var string
22
     */
23
    private $index;
24
    /**
25
     * @var Client
26
     */
27
    private $client;
28
29
    protected function _before()
30
    {
31
        parent::_before();
32
        $this->index = Uuid::getFactory()->uuid4();
0 ignored issues
show
Documentation Bug introduced by
It seems like Ramsey\Uuid\Uuid::getFactory()->uuid4() of type Ramsey\Uuid\UuidInterface is incompatible with the declared type string of property $index.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33
        $host = getenv('ELASTICSEARCH_HOST') ?: '127.0.0.1';
34
        $port = (int) getenv('ELASTICSEARCH_PORT') ?: 9200;
35
        $this->client = ClientBuilder::create()
36
            ->setHosts([sprintf('%s:%d', $host, $port)])
37
            ->build();
38
        $this->client->indices()->create(['index' => $this->index]);
39
    }
40
41
    protected function _after()
42
    {
43
        parent::_after();
44
        $mapping = $this->client->indices()->getMapping(['index' => $this->index]);
45
        $this->addAttachment(Yaml::dump($mapping, 8, 2), 'mapping.yml');
46
        $this->client->indices()->delete(['index' => $this->index]);
47
    }
48
49
    public function dataProvider(): array
50
    {
51
        $variants = [];
52
        $variants[] = [
53
            (new Mapping())
54
                ->setDynamic(false)
55
                ->setProperties([
56
                    'flag' => (new Mapping())
57
                        ->setType(TypeEnum::BOOLEAN)
58
                        ->setDocValues(true)
59
                        ->setStore(true),
60
                    'object' => (new Mapping())
61
                        ->setType(TypeEnum::OBJECT)
62
                        ->setEnabled(false)
63
                ]),
64
            [
65
                'type' => 'object',
66
                'properties' => [
67
                    'flag' => [
68
                        'type' => 'boolean',
69
                        'doc_values' => true,
70
                        'store' => true
71
                    ],
72
                    'object' => [
73
                        'type' => 'object',
74
                        'enabled' => false
75
                    ]
76
                ]
77
            ]
78
        ];
79
        return $variants;
80
    }
81
82
    /**
83
     * @param Mapping $mapping
84
     * @param array $expectation
85
     *
86
     * @test
87
     * @dataProvider dataProvider
88
     */
89
    public function shouldApplyMapping(Mapping $mapping, array $expectation): void
90
    {
91
        $normalizer = new Normalizer();
92
        $body = $normalizer->normalize($mapping);
93
        $this
94
            ->client
95
            ->indices()
96
            ->putMapping(['index' => $this->index, 'type' => '-', 'body' => $body]);
97
    }
98
}
99