1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\ConfigValidation\Test\Unit; |
4
|
|
|
|
5
|
|
|
use Graze\ConfigValidation\ObjectValidator; |
6
|
|
|
use Graze\ConfigValidation\Test\TestCase; |
7
|
|
|
use Respect\Validation\Validator as v; |
8
|
|
|
|
9
|
|
|
class ObjectValidatorTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
public function testDefaultValidator() |
12
|
|
|
{ |
13
|
|
|
$validator = new ObjectValidator(); |
14
|
|
|
|
15
|
|
|
$this->assertTrue($validator->isAllowUnspecified()); |
16
|
|
|
$this->assertEquals('->', $validator->getSeparator()); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
View Code Duplication |
public function testSetAllowUnspecified() |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$validator = new ObjectValidator(); |
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 ObjectValidator(); |
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 object $input |
41
|
|
|
* @param object $expected |
42
|
|
|
* |
43
|
|
|
* @throws \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function testSimpleValidation($input, $expected) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$validator = (new ObjectValidator()) |
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
|
|
|
(object)['must' => 'be here'], |
66
|
|
|
(object)['must' => 'be here', 'defaults' => (object)['path' => '/some/path', 'group' => 'group']], |
67
|
|
|
], |
68
|
|
|
[ |
69
|
|
|
(object)['must' => 'be here', 'defaults' => (object)['path' => '/a/path']], |
70
|
|
|
(object)['must' => 'be here', 'defaults' => (object)['path' => '/a/path', 'group' => 'group']], |
71
|
|
|
], |
72
|
|
|
[ |
73
|
|
|
(object)['must' => 'be here', 'other' => 'cake'], |
74
|
|
|
(object)['must' => 'be here', 'defaults' => (object)['path' => '/some/path', 'group' => 'group'], 'other' => 'cake'], |
75
|
|
|
], |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @dataProvider invalidDataProvider |
81
|
|
|
* |
82
|
|
|
* @param object $input |
83
|
|
|
* |
84
|
|
|
* @throws \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException |
85
|
|
|
* @expectedException \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
public function testFailedValidation($input) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$validator = (new ObjectValidator()) |
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
|
|
|
[(object)['must' => 'be here', 'defaults' => (object)['path' => 1]]], |
105
|
|
|
[(object)['must' => 'be here', 'defaults' => (object)['group' => 2]]], |
106
|
|
|
[(object)['must' => 'be here', 'defaults' => 'monkey']], |
107
|
|
|
[(object)['must' => 'be here', 'defaults' => (object)['path' => ['idea' => 'poop']]]], |
108
|
|
|
[[]], |
109
|
|
|
]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @dataProvider doNotAllowUnspecifiedData |
114
|
|
|
* |
115
|
|
|
* @param object $input |
116
|
|
|
* |
117
|
|
|
* @throws \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException |
118
|
|
|
* @expectedException \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
public function testDoNotAllowUnspecified($input) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$validator = (new ObjectValidator(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
|
|
|
[(object)['stuff', 'monkey']], |
138
|
|
|
[(object)['stuff', 'cake' => 4, 'poop']], |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.