Passed
Push — dev ( 40f0b5...3a2e98 )
by Fike
02:50
created

ValidatorTest::shouldDetectUnknownParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Test\Suite\Functional\Mapping;
6
7
use AmaTeam\ElasticSearch\API\Mapping;
8
use AmaTeam\ElasticSearch\Mapping\Type\BooleanType;
9
use AmaTeam\ElasticSearch\Mapping\Type\IntegerType;
10
use AmaTeam\ElasticSearch\Mapping\Type\NestedType;
11
use AmaTeam\ElasticSearch\Mapping\Type\Parameter\DocValuesParameter;
12
use AmaTeam\ElasticSearch\Mapping\Type\Parameter\DynamicParameter;
13
use AmaTeam\ElasticSearch\Mapping\Type\RootType;
14
use AmaTeam\ElasticSearch\Mapping\Validation\Constraint\ApplicableParameter;
15
use AmaTeam\ElasticSearch\Mapping\Validation\Constraint\ValidParameterName;
16
use AmaTeam\ElasticSearch\Mapping\Validation\Constraint\ValidTypeName;
17
use AmaTeam\ElasticSearch\Mapping\Validator;
18
use Codeception\Test\Unit;
19
use PHPUnit\Framework\Assert;
20
use Symfony\Component\Validator\Constraints\NotNull;
21
use Symfony\Component\Validator\Constraints\Type;
22
use Symfony\Component\Validator\ConstraintViolation;
23
24
class ValidatorTest extends Unit
25
{
26
    /**
27
     * @test
28
     */
29
    public function shouldDetectUnknownType()
30
    {
31
        $mapping = new Mapping('unknown');
32
        $result = (new Validator())->validate($mapping);
33
        Assert::assertEquals(1, $result->count());
34
        /** @var ConstraintViolation $violation */
35
        $violation = $result[0];
36
        Assert::assertInstanceOf(ValidTypeName::class, $violation->getConstraint());
37
    }
38
39
    /**
40
     * @test
41
     */
42
    public function shouldDetectInvalidTypeName()
43
    {
44
        $mapping = new Mapping('geoPoint');
45
        $result = (new Validator())->validate($mapping);
46
        Assert::assertEquals(1, $result->count());
47
        /** @var ConstraintViolation $violation */
48
        $violation = $result[0];
49
        Assert::assertInstanceOf(ValidTypeName::class, $violation->getConstraint());
50
    }
51
52
    /**
53
     * @test
54
     */
55 View Code Duplication
    public function shouldDetectUnknownParameter()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
56
    {
57
        $mapping = (new Mapping(RootType::ID))->setParameter('unknown', true);
58
        $result = (new Validator())->validate($mapping);
59
        Assert::assertEquals(1, $result->count());
60
        /** @var ConstraintViolation $violation */
61
        $violation = $result[0];
62
        Assert::assertInstanceOf(ValidParameterName::class, $violation->getConstraint());
63
    }
64
65
    /**
66
     * @test
67
     */
68 View Code Duplication
    public function shouldDetectInvalidParameterNaming()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
69
    {
70
        $mapping = (new Mapping(BooleanType::ID))->setParameter('docValues', true);
71
        $result = (new Validator())->validate($mapping);
72
        Assert::assertEquals(1, $result->count());
73
        /** @var ConstraintViolation $violation */
74
        $violation = $result[0];
75
        Assert::assertInstanceOf(ValidParameterName::class, $violation->getConstraint());
76
    }
77
78
    /**
79
     * @test
80
     */
81 View Code Duplication
    public function shouldDetectInvalidParameterValue()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
82
    {
83
        $mapping = (new Mapping(BooleanType::ID))->setParameter(DocValuesParameter::ID, 'twenty');
84
        $result = (new Validator())->validate($mapping);
85
        Assert::assertEquals(1, $result->count());
86
        /** @var ConstraintViolation $violation */
87
        $violation = $result[0];
88
        Assert::assertInstanceOf(Type::class, $violation->getConstraint());
89
    }
90
91
    /**
92
     * @test
93
     */
94 View Code Duplication
    public function shouldDetectMisplacedParameter()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
95
    {
96
        $mapping = (new Mapping(BooleanType::ID))->setParameter(DynamicParameter::ID, false);
97
        $result = (new Validator())->validate($mapping);
98
        Assert::assertEquals(1, $result->count());
99
        /** @var ConstraintViolation $violation */
100
        $violation = $result[0];
101
        Assert::assertInstanceOf(ApplicableParameter::class, $violation->getConstraint());
102
    }
103
104
    /**
105
     * @test
106
     */
107 View Code Duplication
    public function shouldDetectIllegalNull()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
108
    {
109
        $mapping = (new Mapping(BooleanType::ID))->setParameter(DocValuesParameter::ID, null);
110
        $result = (new Validator())->validate($mapping);
111
        Assert::assertEquals(1, $result->count());
112
        /** @var ConstraintViolation $violation */
113
        $violation = $result[0];
114
        Assert::assertInstanceOf(NotNull::class, $violation->getConstraint());
115
    }
116
117
    /**
118
     * @test
119
     */
120
    public function shouldRecursivelyAnalyzeProperties()
121
    {
122
        $mapping = (new Mapping(NestedType::ID))
123
            ->setProperties([
124
                'id' => (new Mapping(IntegerType::ID))->setParameter('unknown', false)
125
            ]);
126
        $result = (new Validator())->validate($mapping);
127
        Assert::assertEquals(1, $result->count());
128
        /** @var ConstraintViolation $violation */
129
        $violation = $result[0];
130
        Assert::assertInstanceOf(ValidParameterName::class, $violation->getConstraint());
131
        Assert::assertEquals('properties.id.unknown', $violation->getPropertyPath());
132
    }
133
}
134