1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AmaTeam\ElasticSearch\Test\Suite\Functional\Indexing; |
4
|
|
|
|
5
|
|
|
use AmaTeam\ElasticSearch\API\Indexing; |
6
|
|
|
use AmaTeam\ElasticSearch\API\Indexing\Normalization\Context; |
7
|
|
|
use AmaTeam\ElasticSearch\API\Indexing\OptionInterface; |
8
|
|
|
use AmaTeam\ElasticSearch\Indexing\Normalizer; |
9
|
|
|
use AmaTeam\ElasticSearch\Indexing\Operations; |
10
|
|
|
use AmaTeam\ElasticSearch\Indexing\Option\Infrastructure\Registry; |
11
|
|
|
use AmaTeam\ElasticSearch\Indexing\Option\NumberOfReplicasOption; |
12
|
|
|
use Codeception\Test\Unit; |
13
|
|
|
use PHPUnit\Framework\Assert; |
14
|
|
|
use PHPUnit_Framework_MockObject_MockObject as Mock; |
15
|
|
|
|
16
|
|
|
class NormalizerTest extends Unit |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Indexing |
20
|
|
|
*/ |
21
|
|
|
private $dummy; |
22
|
|
|
|
23
|
|
|
protected function _before() |
24
|
|
|
{ |
25
|
|
|
$this->dummy = (new Indexing()) |
26
|
|
|
->setReadIndices([]) |
27
|
|
|
->setWriteIndices([]) |
28
|
|
|
->setType('doc'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @test |
33
|
|
|
*/ |
34
|
|
|
public function shouldNormalizeKnownOptionName() |
35
|
|
|
{ |
36
|
|
|
$id = 'alpha'; |
37
|
|
|
$friendlyId = 'beta'; |
38
|
|
|
/** @var Mock|OptionInterface $option */ |
39
|
|
|
$option = $this->createMock(OptionInterface::class); |
40
|
|
|
$option |
41
|
|
|
->expects($this->any()) |
42
|
|
|
->method('getId') |
43
|
|
|
->willReturn($id); |
44
|
|
|
$option |
45
|
|
|
->expects($this->any()) |
46
|
|
|
->method('getFriendlyId') |
47
|
|
|
->willReturn($friendlyId); |
48
|
|
|
$registry = (new Registry())->register($option); |
49
|
|
|
$input = Operations::from($this->dummy)->setOption($friendlyId, 'value'); |
50
|
|
|
$expectation = Operations::from($this->dummy)->setOption($id, 'value'); |
51
|
|
|
$output = (new Normalizer($registry))->normalize($input); |
52
|
|
|
Assert::assertEquals($expectation, $output); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @test |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function shouldStripUnknownOption() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$option = 'alpha'; |
61
|
|
|
$context = (new Context())->setPreserveUnknownOptions(false); |
62
|
|
|
$input = Operations::from($this->dummy)->setOption($option, 'value'); |
63
|
|
|
$output = (new Normalizer())->normalize($input, $context); |
64
|
|
|
Assert::assertEquals($this->dummy, $output); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @test |
69
|
|
|
*/ |
70
|
|
|
public function shouldPreserveUnknownOptions() |
71
|
|
|
{ |
72
|
|
|
$option = 'alpha'; |
73
|
|
|
$context = (new Context())->setPreserveUnknownOptions(true); |
74
|
|
|
$input = Operations::from($this->dummy)->setOption($option, 'value'); |
75
|
|
|
$output = (new Normalizer())->normalize($input, $context); |
76
|
|
|
Assert::assertEquals($input, $output); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @test |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function shouldPreserveUnknownOption() |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$option = 'alpha'; |
85
|
|
|
$context = (new Context()) |
86
|
|
|
->setPreserveUnknownOptions(false) |
87
|
|
|
->setPreservedOptions([$option]); |
88
|
|
|
$input = Operations::from($this->dummy)->setOption($option, 'value'); |
89
|
|
|
$output = (new Normalizer())->normalize($input, $context); |
90
|
|
|
Assert::assertEquals($input, $output); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @test |
95
|
|
|
*/ |
96
|
|
|
public function shouldDeduplicateIndices() |
97
|
|
|
{ |
98
|
|
|
$index = 'curly'; |
99
|
|
|
$indices = [$index, $index, $index]; |
100
|
|
|
$input = Operations::from($this->dummy) |
101
|
|
|
->setReadIndices($indices) |
102
|
|
|
->setWriteIndices($indices); |
103
|
|
|
$expectation = Operations::from($this->dummy) |
104
|
|
|
->setReadIndices([$index]) |
105
|
|
|
->setWriteIndices([$index]); |
106
|
|
|
$output = (new Normalizer())->normalize($input); |
107
|
|
|
Assert::assertEquals($expectation, $output); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @test |
112
|
|
|
*/ |
113
|
|
|
public function shouldRemoveNullValues() |
114
|
|
|
{ |
115
|
|
|
$input = Operations::from($this->dummy); |
116
|
|
|
$input->setOption(NumberOfReplicasOption::ID, null); |
117
|
|
|
$expectation = Operations::from($this->dummy); |
118
|
|
|
$output = (new Normalizer())->normalize($input); |
119
|
|
|
Assert::assertEquals($expectation, $output); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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.