Passed
Push — feature/initial-implementation ( 3b7c72...40c5a6 )
by Fike
01:49
created

ValidatorTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 108
Duplicated Lines 41.67 %

Importance

Changes 0
Metric Value
wmc 8
dl 45
loc 108
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A shouldDetectUnknownParameter() 8 8 1
A shouldRecursivelyAnalyzeProperties() 0 12 1
A shouldDetectIllegalNull() 8 8 1
A shouldDetectInvalidParameterValue() 8 8 1
A shouldDetectInvalidTypeName() 0 8 1
A shouldDetectUnknownType() 0 8 1
A shouldDetectInvalidParameterNaming() 8 8 1
A shouldDetectMisplacedParameter() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Test\Suite\Functional\Mapping;
6
7
use AmaTeam\ElasticSearch\Mapping\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\ExistingParameter;
16
use AmaTeam\ElasticSearch\Mapping\Validation\Constraint\ExistingType;
17
use AmaTeam\ElasticSearch\Mapping\Validation\Constraint\ValidParameterName;
18
use AmaTeam\ElasticSearch\Mapping\Validation\Constraint\ValidTypeName;
19
use AmaTeam\ElasticSearch\Mapping\Validator;
20
use Codeception\Test\Unit;
21
use PHPUnit\Framework\Assert;
22
use Symfony\Component\Validator\Constraints\NotNull;
23
use Symfony\Component\Validator\Constraints\Type;
24
use Symfony\Component\Validator\ConstraintViolation;
25
26
class ValidatorTest extends Unit
27
{
28
    /**
29
     * @test
30
     */
31
    public function shouldDetectUnknownType()
32
    {
33
        $mapping = new Mapping('unknown');
34
        $result = (new Validator())->validate($mapping);
35
        Assert::assertEquals(1, $result->count());
36
        /** @var ConstraintViolation $violation */
37
        $violation = $result[0];
38
        Assert::assertInstanceOf(ExistingType::class, $violation->getConstraint());
39
    }
40
41
    /**
42
     * @test
43
     */
44
    public function shouldDetectInvalidTypeName()
45
    {
46
        $mapping = new Mapping('geoPoint');
47
        $result = (new Validator())->validate($mapping);
48
        Assert::assertEquals(1, $result->count());
49
        /** @var ConstraintViolation $violation */
50
        $violation = $result[0];
51
        Assert::assertInstanceOf(ValidTypeName::class, $violation->getConstraint());
52
    }
53
54
    /**
55
     * @test
56
     */
57 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...
58
    {
59
        $mapping = (new Mapping(RootType::ID))->setParameter('unknown', true);
60
        $result = (new Validator())->validate($mapping);
61
        Assert::assertEquals(1, $result->count());
62
        /** @var ConstraintViolation $violation */
63
        $violation = $result[0];
64
        Assert::assertInstanceOf(ExistingParameter::class, $violation->getConstraint());
65
    }
66
67
    /**
68
     * @test
69
     */
70 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...
71
    {
72
        $mapping = (new Mapping(BooleanType::ID))->setParameter('docValues', true);
73
        $result = (new Validator())->validate($mapping);
74
        Assert::assertEquals(1, $result->count());
75
        /** @var ConstraintViolation $violation */
76
        $violation = $result[0];
77
        Assert::assertInstanceOf(ValidParameterName::class, $violation->getConstraint());
78
    }
79
80
    /**
81
     * @test
82
     */
83 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...
84
    {
85
        $mapping = (new Mapping(BooleanType::ID))->setParameter(DocValuesParameter::ID, 'twenty');
86
        $result = (new Validator())->validate($mapping);
87
        Assert::assertEquals(1, $result->count());
88
        /** @var ConstraintViolation $violation */
89
        $violation = $result[0];
90
        Assert::assertInstanceOf(Type::class, $violation->getConstraint());
91
    }
92
93
    /**
94
     * @test
95
     */
96 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...
97
    {
98
        $mapping = (new Mapping(BooleanType::ID))->setParameter(DynamicParameter::ID, false);
99
        $result = (new Validator())->validate($mapping);
100
        Assert::assertEquals(1, $result->count());
101
        /** @var ConstraintViolation $violation */
102
        $violation = $result[0];
103
        Assert::assertInstanceOf(ApplicableParameter::class, $violation->getConstraint());
104
    }
105
106
    /**
107
     * @test
108
     */
109 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...
110
    {
111
        $mapping = (new Mapping(BooleanType::ID))->setParameter(DocValuesParameter::ID, null);
112
        $result = (new Validator())->validate($mapping);
113
        Assert::assertEquals(1, $result->count());
114
        /** @var ConstraintViolation $violation */
115
        $violation = $result[0];
116
        Assert::assertInstanceOf(NotNull::class, $violation->getConstraint());
117
    }
118
119
    /**
120
     * @test
121
     */
122
    public function shouldRecursivelyAnalyzeProperties()
123
    {
124
        $mapping = (new Mapping(NestedType::ID))
125
            ->setProperties([
126
                'id' => (new Mapping(IntegerType::ID))->setParameter('unknown', false)
127
            ]);
128
        $result = (new Validator())->validate($mapping);
129
        Assert::assertEquals(1, $result->count());
130
        /** @var ConstraintViolation $violation */
131
        $violation = $result[0];
132
        Assert::assertInstanceOf(ExistingParameter::class, $violation->getConstraint());
133
        Assert::assertEquals('properties.id.unknown', $violation->getPropertyPath());
134
    }
135
}
136