Completed
Push — master ( d38252...509746 )
by Harry
03:13
created

ArrayValidatorTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 152
Duplicated Lines 36.18 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 55
loc 152
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testDefaultValidator() 0 7 1
A testSetAllowUnspecified() 8 8 1
A testSetSeparator() 8 8 1
A testSimpleValidation() 12 12 1
A defaultsDataProvider() 0 17 1
A testFailedValidation() 10 10 1
A invalidDataProvider() 0 10 1
A testDoNotAllowUnspecified() 10 10 1
A doNotAllowUnspecifiedData() 7 7 1
A testDefaultValidations() 0 8 1
A testChildRequireDependencies() 0 9 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
namespace Graze\ConfigValidation\Test\Unit;
4
5
use Graze\ConfigValidation\ArrayValidator;
6
use Graze\ConfigValidation\Test\TestCase;
7
use Respect\Validation\Validator as v;
8
9
class ArrayValidatorTest extends TestCase
10
{
11
    public function testDefaultValidator()
12
    {
13
        $validator = new ArrayValidator();
14
15
        $this->assertTrue($validator->isAllowUnspecified());
16
        $this->assertEquals('.', $validator->getSeparator());
17
    }
18
19 View Code Duplication
    public function testSetAllowUnspecified()
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...
20
    {
21
        $validator = new ArrayValidator();
22
23
        $this->assertTrue($validator->isAllowUnspecified());
24
        $this->assertSame($validator, $validator->setAllowUnspecified(false));
25
        $this->assertFalse($validator->isAllowUnspecified());
26
    }
27
28 View Code Duplication
    public function testSetSeparator()
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...
29
    {
30
        $validator = new ArrayValidator();
31
32
        $this->assertEquals('.', $validator->getSeparator());
33
        $this->assertSame($validator, $validator->setSeparator('->'));
34
        $this->assertEquals('->', $validator->getSeparator());
35
    }
36
37
    /**
38
     * @dataProvider defaultsDataProvider
39
     *
40
     * @param array $input
41
     * @param array $expected
42
     *
43
     * @throws \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException
44
     */
45 View Code Duplication
    public function testSimpleValidation(array $input, array $expected)
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...
46
    {
47
        $validator = (new ArrayValidator())
48
            ->optional('defaults.path', v::stringType(), '/some/path')
49
            ->optional('defaults.group', v::stringType(), 'group')
50
            ->required('must', v::stringType()->equals('be here'));
51
52
        $this->assertTrue($validator->isValid($input));
53
        $actual = $validator->validate($input);
54
55
        $this->assertEquals($expected, $actual);
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function defaultsDataProvider()
62
    {
63
        return [
64
            [
65
                ['must' => 'be here'],
66
                ['must' => 'be here', 'defaults' => ['path' => '/some/path', 'group' => 'group']],
67
            ],
68
            [
69
                ['must' => 'be here', 'defaults' => ['path' => '/a/path']],
70
                ['must' => 'be here', 'defaults' => ['path' => '/a/path', 'group' => 'group']],
71
            ],
72
            [
73
                ['must' => 'be here', 'other' => 'cake'],
74
                ['must' => 'be here', 'defaults' => ['path' => '/some/path', 'group' => 'group'], 'other' => 'cake'],
75
            ],
76
        ];
77
    }
78
79
    /**
80
     * @dataProvider invalidDataProvider
81
     *
82
     * @param array $input
83
     *
84
     * @throws \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException
85
     * @expectedException \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException
86
     */
87 View Code Duplication
    public function testFailedValidation(array $input)
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...
88
    {
89
        $validator = (new ArrayValidator())
90
            ->optional('defaults.path', v::stringType(), '/some/path')
91
            ->optional('defaults.group', v::stringType(), 'group')
92
            ->required('must', v::stringType()->equals('be here'));
93
94
        $this->assertFalse($validator->isValid($input));
95
        $validator->validate($input);
96
    }
97
98
    /**
99
     * @return array
100
     */
101
    public function invalidDataProvider()
102
    {
103
        return [
104
            [['must' => 'be here', 'defaults' => ['path' => 1]]],
105
            [['must' => 'be here', 'defaults' => ['group' => 2]]],
106
            [['must' => 'be here', 'defaults' => 'monkey']],
107
            [['must' => 'be here', 'defaults' => ['path' => ['idea' => 'poop']]]],
108
            [[]],
109
        ];
110
    }
111
112
    /**
113
     * @dataProvider doNotAllowUnspecifiedData
114
     *
115
     * @param array $input
116
     *
117
     * @throws \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException
118
     * @expectedException \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException
119
     */
120 View Code Duplication
    public function testDoNotAllowUnspecified(array $input)
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...
121
    {
122
        $validator = (new ArrayValidator(false))
123
            ->required('stuff')
124
            ->optional('cake', v::intType());
125
126
        $this->assertFalse($validator->isAllowUnspecified());
127
128
        $validator->validate($input);
129
    }
130
131
    /**
132
     * @return array
133
     */
134 View Code Duplication
    public function doNotAllowUnspecifiedData()
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...
135
    {
136
        return [
137
            [['stuff', 'monkey']],
138
            [['stuff', 'cake' => 4, 'poop']],
139
        ];
140
    }
141
142
    public function testDefaultValidations()
143
    {
144
        $validator = (new ArrayValidator())
145
            ->required('stuff')
146
            ->optional('cake');
147
148
        $this->assertTrue($validator->isValid(['stuff' => 'yup']));
149
    }
150
151
    public function testChildRequireDependencies()
152
    {
153
        $validator = (new ArrayValidator())
154
            ->required('default.stuff.option')
155
            ->optional('default.thing');
156
157
        $this->assertFalse($validator->isValid(['default' => 'nope']));
158
        $this->assertFalse($validator->isValid([]));
159
    }
160
}
161