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() |
|
|
|
|
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() |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
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() |
|
|
|
|
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
|
|
|
|
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.