1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AmaTeam\ElasticSearch\Test\Suite\Functional\Mapping; |
6
|
|
|
|
7
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\Normalization\DefaultContext; |
8
|
|
|
use AmaTeam\ElasticSearch\Mapping\Mapping; |
9
|
|
|
use AmaTeam\ElasticSearch\Mapping\Normalizer; |
10
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\BooleanType; |
11
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\DateType; |
12
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\IntegerType; |
13
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\KeywordType; |
14
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\ObjectType; |
15
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\Parameter\DocValuesParameter; |
16
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\Parameter\DynamicParameter; |
17
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\Parameter\IgnoreMalformedParameter; |
18
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\Parameter\NormalizerParameter; |
19
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\Parameter\NullValueParameter; |
20
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\Parameter\SourceParameter; |
21
|
|
|
use AmaTeam\ElasticSearch\Mapping\Type\RootType; |
22
|
|
|
use Codeception\Test\Unit; |
23
|
|
|
use PHPUnit\Framework\Assert; |
24
|
|
|
|
25
|
|
|
class NormalizerTest extends Unit |
26
|
|
|
{ |
27
|
|
|
public function dataProvider() |
28
|
|
|
{ |
29
|
|
|
$variants = []; |
30
|
|
|
$variants[] = [ |
31
|
|
|
(new Mapping()) |
32
|
|
|
->setType(RootType::ID) |
33
|
|
|
->setParameter('dynamic', false) |
34
|
|
|
->setParameter('_source', false) |
35
|
|
|
->setProperties([ |
36
|
|
|
'id' => (new Mapping()) |
37
|
|
|
->setType(IntegerType::ID) |
38
|
|
|
->setParameter('unknown', 'twenty') |
39
|
|
|
->setParameter('reallyUnknown', 'forty') |
40
|
|
|
->setParameter('null', null) |
41
|
|
|
]), |
42
|
|
|
(new DefaultContext()) |
43
|
|
|
->setRemoveUnknownParameters(true) |
44
|
|
|
->setIgnoredParameters(['unknown']), |
45
|
|
|
(new Mapping()) |
46
|
|
|
->setType(RootType::ID) |
47
|
|
|
->setParameter('dynamic', false) |
48
|
|
|
->setParameter('_source', false) |
49
|
|
|
->setProperties([ |
50
|
|
|
'id' => (new Mapping()) |
51
|
|
|
->setType(IntegerType::ID) |
52
|
|
|
->setParameter('unknown', 'twenty') |
53
|
|
|
]), |
54
|
|
|
]; |
55
|
|
|
$variants[] = [ |
56
|
|
|
(new Mapping()) |
57
|
|
|
->setType(RootType::ID) |
58
|
|
|
->setProperties([ |
59
|
|
|
'createdAt' => (new Mapping()) |
60
|
|
|
->setType(DateType::ID) |
61
|
|
|
->setParameter(IgnoreMalformedParameter::ID, false), |
62
|
|
|
'aspects' => (new Mapping()) |
63
|
|
|
->setType(ObjectType::FRIENDLY_ID) |
64
|
|
|
->setParameter(DynamicParameter::FRIENDLY_ID, false) |
65
|
|
|
->setProperties([ |
66
|
|
|
'handsome' => (new Mapping()) |
67
|
|
|
->setType(BooleanType::FRIENDLY_ID) |
68
|
|
|
->setParameter(DocValuesParameter::ID, true), |
69
|
|
|
'brutal' => (new Mapping()) |
70
|
|
|
->setType(BooleanType::ID) |
71
|
|
|
->setParameter(DocValuesParameter::ID, true) |
72
|
|
|
->setParameter(NullValueParameter::ID, true) |
73
|
|
|
]) |
74
|
|
|
]), |
75
|
|
|
(new DefaultContext()) |
76
|
|
|
->setRemoveUnknownParameters(true), |
77
|
|
|
(new Mapping()) |
78
|
|
|
->setType(RootType::ID) |
79
|
|
|
->setProperties([ |
80
|
|
|
'createdAt' => (new Mapping()) |
81
|
|
|
->setType(DateType::ID) |
82
|
|
|
->setParameter(IgnoreMalformedParameter::ID, false), |
83
|
|
|
'aspects' => (new Mapping()) |
84
|
|
|
->setType(ObjectType::ID) |
85
|
|
|
->setParameter(DynamicParameter::ID, false) |
86
|
|
|
->setProperties([ |
87
|
|
|
'handsome' => (new Mapping()) |
88
|
|
|
->setType(BooleanType::ID) |
89
|
|
|
->setParameter(DocValuesParameter::ID, true), |
90
|
|
|
'brutal' => (new Mapping()) |
91
|
|
|
->setType(BooleanType::ID) |
92
|
|
|
->setParameter(DocValuesParameter::ID, true) |
93
|
|
|
->setParameter(NullValueParameter::ID, true) |
94
|
|
|
]) |
95
|
|
|
]), |
96
|
|
|
]; |
97
|
|
|
return $variants; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param Mapping $mapping |
102
|
|
|
* @param DefaultContext $context |
103
|
|
|
* @param Mapping $expectation |
104
|
|
|
* |
105
|
|
|
* @test |
106
|
|
|
* @dataProvider dataProvider |
107
|
|
|
*/ |
108
|
|
|
public function shouldFulfillExpectations(Mapping $mapping, DefaultContext $context, Mapping $expectation) |
109
|
|
|
{ |
110
|
|
|
$result = (new Normalizer())->normalize($mapping, $context); |
111
|
|
|
Assert::assertEquals($expectation, $result); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @test |
116
|
|
|
*/ |
117
|
|
|
public function shouldRemoveUnknownParameters() |
118
|
|
|
{ |
119
|
|
|
$mapping = (new Mapping()) |
120
|
|
|
->setType(RootType::ID) |
121
|
|
|
->setParameter('unknown', 'twenty'); |
122
|
|
|
$expectation = (new Mapping()) |
123
|
|
|
->setType(RootType::ID); |
124
|
|
|
$normalizer = new Normalizer(); |
125
|
|
|
$context = (new DefaultContext())->setRemoveUnknownParameters(true); |
126
|
|
|
$result = $normalizer->normalize($mapping, $context); |
127
|
|
|
Assert::assertEquals($expectation, $result); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @test |
132
|
|
|
*/ |
133
|
|
|
public function shouldNotRemoveUnknownParameters() |
134
|
|
|
{ |
135
|
|
|
$mapping = (new Mapping()) |
136
|
|
|
->setType(RootType::ID) |
137
|
|
|
->setParameter('unknown', 'value'); |
138
|
|
|
$context = (new DefaultContext())->setRemoveUnknownParameters(false); |
139
|
|
|
$result = (new Normalizer())->normalize($mapping, $context); |
140
|
|
|
Assert::assertEquals($mapping, $result); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @test |
145
|
|
|
*/ |
146
|
|
View Code Duplication |
public function shouldRemoveCommonNullValuedParameters() |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
$mapping = (new Mapping()) |
149
|
|
|
->setType(RootType::ID) |
150
|
|
|
->setParameter(SourceParameter::ID, null); |
151
|
|
|
$expectation = (new Mapping()) |
152
|
|
|
->setType(RootType::ID); |
153
|
|
|
$context = (new DefaultContext())->setRemoveUnknownParameters(false); |
154
|
|
|
$result = (new Normalizer())->normalize($mapping, $context); |
155
|
|
|
Assert::assertEquals($expectation, $result); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @test |
160
|
|
|
*/ |
161
|
|
|
public function shouldNotRemoveNullAllowedNullValuedParameters() |
162
|
|
|
{ |
163
|
|
|
$mapping = (new Mapping()) |
164
|
|
|
->setType(KeywordType::ID) |
165
|
|
|
->setParameter(NormalizerParameter::ID, null); |
166
|
|
|
$context = (new DefaultContext())->setRemoveUnknownParameters(false); |
167
|
|
|
$result = (new Normalizer())->normalize($mapping, $context); |
168
|
|
|
Assert::assertEquals($mapping, $result); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @test |
173
|
|
|
*/ |
174
|
|
View Code Duplication |
public function shouldNormalizeParameterNames() |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
$mapping = (new Mapping()) |
177
|
|
|
->setType(DateType::ID) |
178
|
|
|
->setParameter(IgnoreMalformedParameter::FRIENDLY_ID, true); |
179
|
|
|
$expectation = (new Mapping()) |
180
|
|
|
->setType(DateType::ID) |
181
|
|
|
->setParameter(IgnoreMalformedParameter::ID, true); |
182
|
|
|
$result = (new Normalizer())->normalize($mapping); |
183
|
|
|
Assert::assertEquals($expectation, $result); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.