1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AmaTeam\ElasticSearch\Test\Suite\Functional\Indexing; |
4
|
|
|
|
5
|
|
|
use AmaTeam\ElasticSearch\API\Indexing; |
6
|
|
|
use AmaTeam\ElasticSearch\API\Indexing\Validation\Context; |
7
|
|
|
use AmaTeam\ElasticSearch\API\Indexing\ValidatorInterface; |
8
|
|
|
use AmaTeam\ElasticSearch\Indexing\Operations; |
9
|
|
|
use AmaTeam\ElasticSearch\Indexing\Option\NumberOfReplicasOption; |
10
|
|
|
use AmaTeam\ElasticSearch\Indexing\Option\NumberOfShardsOption; |
11
|
|
|
use AmaTeam\ElasticSearch\Indexing\Validation\Constraint\ValidOptionName; |
12
|
|
|
use AmaTeam\ElasticSearch\Indexing\Validator; |
13
|
|
|
use Codeception\Test\Unit; |
14
|
|
|
use PHPUnit\Framework\Assert; |
15
|
|
|
use Symfony\Component\Validator\Constraints\Type; |
16
|
|
|
use Symfony\Component\Validator\ConstraintViolation; |
17
|
|
|
|
18
|
|
|
class ValidatorTest extends Unit |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Indexing |
22
|
|
|
*/ |
23
|
|
|
private $dummy; |
24
|
|
|
/** |
25
|
|
|
* @var ValidatorInterface |
26
|
|
|
*/ |
27
|
|
|
private $validator; |
28
|
|
|
|
29
|
|
|
protected function _before() |
30
|
|
|
{ |
31
|
|
|
$this->dummy = (new Indexing()) |
32
|
|
|
->setWriteIndices([]) |
33
|
|
|
->setReadIndices([]) |
34
|
|
|
->setType('doc'); |
35
|
|
|
$this->validator = new Validator(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @test |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function shouldUseOptionConstraints() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$input = Operations::from($this->dummy) |
44
|
|
|
->setOption(NumberOfReplicasOption::ID, 'twelve replicas, please'); |
45
|
|
|
$violations = $this->validator->validate($input); |
46
|
|
|
Assert::assertEquals(1, $violations->count()); |
47
|
|
|
/** @var ConstraintViolation $violation */ |
48
|
|
|
$violation = $violations[0]; |
49
|
|
|
Assert::assertInstanceOf(Type::class, $violation->getConstraint()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @test |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function shouldStopUnknownOptions() |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$input = Operations::from($this->dummy) |
58
|
|
|
->setOption('index.wharrgarbl.option', 273); |
59
|
|
|
$violations = $this->validator->validate($input, (new Context())->setPreserveUnknownEntries(false)); |
60
|
|
|
Assert::assertEquals(1, $violations->count()); |
61
|
|
|
/** @var ConstraintViolation $violation */ |
62
|
|
|
$violation = $violations[0]; |
63
|
|
|
Assert::assertInstanceOf(ValidOptionName::class, $violation->getConstraint()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @test |
68
|
|
|
*/ |
69
|
|
|
public function shouldIgnoreUnknownOptionsIfAsked() |
70
|
|
|
{ |
71
|
|
|
$input = Operations::from($this->dummy) |
72
|
|
|
->setOption('index.wharrgarbl.option', 273); |
73
|
|
|
$violations = $this->validator->validate($input, (new Context())->setPreserveUnknownEntries(true)); |
74
|
|
|
Assert::assertEquals(0, $violations->count()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @test |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function shouldStopFriendlyIds() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$input = Operations::from($this->dummy) |
83
|
|
|
->setOption(NumberOfShardsOption::FRIENDLY_ID, 3); |
84
|
|
|
$violations = $this->validator->validate($input); |
85
|
|
|
Assert::assertEquals(1, $violations->count()); |
86
|
|
|
/** @var ConstraintViolation $violation */ |
87
|
|
|
$violation = $violations[0]; |
88
|
|
|
Assert::assertInstanceOf(ValidOptionName::class, $violation->getConstraint()); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.