|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of graze/config-validation. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2017 Nature Delivered Ltd. <https://www.graze.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license https://github.com/graze/config-validation/blob/master/LICENSE.md |
|
11
|
|
|
* @link https://github.com/graze/config-validation |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Graze\ConfigValidation\Test\Unit; |
|
15
|
|
|
|
|
16
|
|
|
use Graze\ConfigValidation\ObjectValidator; |
|
17
|
|
|
use Graze\ConfigValidation\Test\TestCase; |
|
18
|
|
|
use Respect\Validation\Validator as v; |
|
19
|
|
|
|
|
20
|
|
|
class ObjectValidatorTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
public function testDefaultValidator() |
|
23
|
|
|
{ |
|
24
|
|
|
$validator = new ObjectValidator(); |
|
25
|
|
|
|
|
26
|
|
|
$this->assertTrue($validator->isAllowUnspecified()); |
|
27
|
|
|
$this->assertEquals('->', $validator->getSeparator()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testSetAllowUnspecified() |
|
31
|
|
|
{ |
|
32
|
|
|
$validator = new ObjectValidator(); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertTrue($validator->isAllowUnspecified()); |
|
35
|
|
|
$this->assertSame($validator, $validator->setAllowUnspecified(false)); |
|
36
|
|
|
$this->assertFalse($validator->isAllowUnspecified()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testSetSeparator() |
|
40
|
|
|
{ |
|
41
|
|
|
$validator = new ObjectValidator(); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertEquals('->', $validator->getSeparator()); |
|
44
|
|
|
$this->assertSame($validator, $validator->setSeparator('.')); |
|
45
|
|
|
$this->assertEquals('.', $validator->getSeparator()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @dataProvider defaultsDataProvider |
|
50
|
|
|
* |
|
51
|
|
|
* @param object $input |
|
52
|
|
|
* @param object $expected |
|
53
|
|
|
* |
|
54
|
|
|
* @throws \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function testSimpleValidation($input, $expected) |
|
57
|
|
|
{ |
|
58
|
|
|
$validator = (new ObjectValidator()) |
|
59
|
|
|
->optional('defaults->path', v::stringType(), '/some/path') |
|
60
|
|
|
->optional('defaults->group', v::stringType(), 'group') |
|
61
|
|
|
->required('must', v::stringType()->equals('be here')); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertTrue($validator->isValid($input), 'The input should be valid'); |
|
64
|
|
|
$actual = $validator->validate($input); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertEquals( |
|
67
|
|
|
$expected, |
|
68
|
|
|
$actual, |
|
69
|
|
|
'the generated output should be the same as the expected object' |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
|
|
public function defaultsDataProvider() |
|
77
|
|
|
{ |
|
78
|
|
|
return [ |
|
79
|
|
|
[ |
|
80
|
|
|
(object) ['must' => 'be here'], |
|
81
|
|
|
(object) ['must' => 'be here', 'defaults' => (object) ['path' => '/some/path', 'group' => 'group']], |
|
82
|
|
|
], |
|
83
|
|
|
[ |
|
84
|
|
|
(object) ['must' => 'be here', 'defaults' => (object) ['path' => '/a/path']], |
|
85
|
|
|
(object) ['must' => 'be here', 'defaults' => (object) ['path' => '/a/path', 'group' => 'group']], |
|
86
|
|
|
], |
|
87
|
|
|
[ |
|
88
|
|
|
(object) ['must' => 'be here', 'other' => 'cake'], |
|
89
|
|
|
(object) ['must' => 'be here', 'defaults' => (object) ['path' => '/some/path', 'group' => 'group'], 'other' => 'cake'], |
|
90
|
|
|
], |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @dataProvider invalidDataProvider |
|
96
|
|
|
* |
|
97
|
|
|
* @param object $input |
|
98
|
|
|
* |
|
99
|
|
|
* @throws \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException |
|
100
|
|
|
* @expectedException \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException |
|
101
|
|
|
*/ |
|
102
|
|
|
public function testFailedValidation($input) |
|
103
|
|
|
{ |
|
104
|
|
|
$validator = (new ObjectValidator()) |
|
105
|
|
|
->optional('defaults->path', v::stringType(), '/some/path') |
|
106
|
|
|
->optional('defaults->group', v::stringType(), 'group') |
|
107
|
|
|
->required('must', v::stringType()->equals('be here')); |
|
108
|
|
|
|
|
109
|
|
|
$this->assertFalse($validator->isValid($input)); |
|
110
|
|
|
$validator->validate($input); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return array |
|
115
|
|
|
*/ |
|
116
|
|
|
public function invalidDataProvider() |
|
117
|
|
|
{ |
|
118
|
|
|
return [ |
|
119
|
|
|
[(object) ['must' => 'be here', 'defaults' => (object) ['path' => 1]]], |
|
120
|
|
|
[(object) ['must' => 'be here', 'defaults' => (object) ['group' => 2]]], |
|
121
|
|
|
[(object) ['must' => 'be here', 'defaults' => 'monkey']], |
|
122
|
|
|
[(object) ['must' => 'be here', 'defaults' => (object) ['path' => ['idea' => 'poop']]]], |
|
123
|
|
|
[[]], |
|
124
|
|
|
]; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @dataProvider doNotAllowUnspecifiedData |
|
129
|
|
|
* |
|
130
|
|
|
* @param object $input |
|
131
|
|
|
* |
|
132
|
|
|
* @throws \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException |
|
133
|
|
|
* @expectedException \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException |
|
134
|
|
|
*/ |
|
135
|
|
|
public function testDoNotAllowUnspecified($input) |
|
136
|
|
|
{ |
|
137
|
|
|
$validator = (new ObjectValidator(false)) |
|
138
|
|
|
->required('stuff') |
|
139
|
|
|
->optional('cake', v::intType()); |
|
140
|
|
|
|
|
141
|
|
|
$this->assertFalse($validator->isAllowUnspecified()); |
|
142
|
|
|
|
|
143
|
|
|
$validator->validate($input); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return array |
|
148
|
|
|
*/ |
|
149
|
|
|
public function doNotAllowUnspecifiedData() |
|
150
|
|
|
{ |
|
151
|
|
|
return [ |
|
152
|
|
|
[(object) ['stuff', 'monkey']], |
|
153
|
|
|
[(object) ['stuff', 'cake' => 4, 'poop']], |
|
154
|
|
|
]; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function testChildBuilders() |
|
158
|
|
|
{ |
|
159
|
|
|
$validator = (new ObjectValidator()) |
|
160
|
|
|
->required('default->stuff') |
|
161
|
|
|
->addChild( |
|
162
|
|
|
'default->thing', |
|
163
|
|
|
(new ObjectValidator()) |
|
164
|
|
|
->required('cake') |
|
165
|
|
|
->optional('moon') |
|
166
|
|
|
); |
|
167
|
|
|
|
|
168
|
|
|
$this->assertFalse($validator->isValid((object) ['default' => (object) ['stuff' => 1]])); |
|
169
|
|
|
$this->assertTrue($validator->isValid((object) ['default' => (object) ['stuff' => 1, 'thing' => (object) ['cake' => 'yup']]])); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|