Passed
Push — feature/initial-implementation ( fae671...591f29 )
by Fike
02:37
created

OperationsTest::conflictDataProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
c 0
b 0
f 0
rs 8.8571
cc 1
eloc 20
nc 1
nop 0
1
<?php
2
3
namespace AmaTeam\ElasticSearch\Test\Suite\Unit\Mapping;
4
5
use AmaTeam\ElasticSearch\API\Mapping;
6
use AmaTeam\ElasticSearch\Mapping\Operations;
7
use AmaTeam\ElasticSearch\Mapping\Type\BooleanType;
8
use AmaTeam\ElasticSearch\Mapping\Type\FloatType;
9
use AmaTeam\ElasticSearch\Mapping\Type\IntegerType;
10
use AmaTeam\ElasticSearch\Mapping\Type\KeywordType;
11
use AmaTeam\ElasticSearch\Mapping\Type\ObjectType;
12
use AmaTeam\ElasticSearch\Mapping\Type\Parameter\AnalyzerParameter;
13
use AmaTeam\ElasticSearch\Mapping\Type\Parameter\DocValuesParameter;
14
use AmaTeam\ElasticSearch\Mapping\Type\Parameter\DynamicParameter;
15
use AmaTeam\ElasticSearch\Mapping\Type\Parameter\IndexParameter;
16
use AmaTeam\ElasticSearch\Mapping\Type\Parameter\NormsParameter;
17
use AmaTeam\ElasticSearch\Mapping\Type\RootType;
18
use AmaTeam\ElasticSearch\Mapping\Type\TextType;
19
use Codeception\Test\Unit;
20
use PHPUnit\Framework\Assert;
21
22
class OperationsTest extends Unit
23
{
24
    public function conflictDataProvider()
25
    {
26
        return [
27
            [
28
                [
29
                    new Mapping(RootType::ID),
30
                    new Mapping(ObjectType::ID)
31
                ],
32
                true
33
            ],
34
            [
35
                [
36
                    (new Mapping(RootType::ID))->setParameter(DynamicParameter::ID, true),
37
                    (new Mapping(RootType::ID))->setParameter(DynamicParameter::ID, false),
38
                ],
39
                true
40
            ],
41
            [
42
                [
43
                    (new Mapping(RootType::ID))->setProperty('value', new Mapping(IntegerType::ID)),
44
                    (new Mapping(RootType::ID))->setProperty('value', new Mapping(FloatType::ID)),
45
                ],
46
                true
47
            ],
48
            [
49
                [
50
                    (new Mapping(RootType::ID))
51
                        ->setProperty('alpha', new Mapping(IntegerType::ID))
52
                        ->setParameter(IndexParameter::ID, false),
53
                    (new Mapping(RootType::ID))
54
                        ->setProperty('beta', new Mapping(ObjectType::ID))
55
                        ->setParameter(DynamicParameter::ID, true),
56
                    (new Mapping(RootType::ID))
57
                        ->setProperty('gamma', new Mapping(KeywordType::ID))
58
                        ->setParameter(NormsParameter::ID, true)
59
                ],
60
                false
61
            ]
62
        ];
63
    }
64
65
    /**
66
     * @param array $mappings
67
     * @param bool $expectation
68
     *
69
     * @test
70
     * @dataProvider conflictDataProvider
71
     */
72
    public function shouldCorrectlyFindConflicts(array $mappings, bool $expectation)
73
    {
74
        $result = Operations::conflict(...$mappings);
75
        Assert::assertEquals($expectation, $result);
76
    }
77
78
    public function fromArrayDataProvider()
79
    {
80
        return [
81
            [
82
                [
83
                    'dynamic' => 'strict',
84
                    'properties' => [
85
                        'id' => [
86
                            'type' => 'integer',
87
                            'index' => false,
88
                        ],
89
                        'title' => [
90
                            'type' => 'text',
91
                            'analyzer' => 'title',
92
                        ],
93
                        'category' => [
94
                            'type' => 'keyword',
95
                            'doc_values' => true,
96
                        ],
97
                        'aspects' => [
98
                            'type' => 'object',
99
                            'dynamic' => 'true',
100
                            'properties' => [
101
                                'comfortable' => ['type' => 'boolean'],
102
                                'cheap' => ['type' => 'boolean'],
103
                            ]
104
                        ]
105
                    ]
106
                ],
107
                (new Mapping())
108
                    ->setParameter(DynamicParameter::ID, DynamicParameter::VALUE_STRICT)
109
                    ->setProperties([
110
                        'id' => (new Mapping(IntegerType::ID))->setParameter(IndexParameter::ID, false),
111
                        'title' => (new Mapping(TextType::ID))->setParameter(AnalyzerParameter::ID, 'title'),
112
                        'category' => (new Mapping(KeywordType::ID))->setParameter(DocValuesParameter::ID, true),
113
                        'aspects' => (new Mapping(ObjectType::ID))
114
                            ->setParameter(DynamicParameter::ID, DynamicParameter::VALUE_TRUE)
115
                            ->setProperty('comfortable', new Mapping(BooleanType::ID))
116
                            ->setProperty('cheap', new Mapping(BooleanType::ID))
117
                    ])
118
            ]
119
        ];
120
    }
121
122
    /**
123
     * @param array $input
124
     * @param Mapping $expectation
125
     *
126
     * @test
127
     * @dataProvider fromArrayDataProvider
128
     */
129
    public function shouldCorrectlyDeserializeArray(array $input, Mapping $expectation)
130
    {
131
        $result = Operations::fromArray($input);
132
        Assert::assertEquals($expectation, $result);
133
    }
134
}
135