|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ValueValidators\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use ValueValidators\Error; |
|
7
|
|
|
use ValueValidators\ListValidator; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @covers ValueValidators\ListValidator |
|
11
|
|
|
* |
|
12
|
|
|
* @group ValueValidators |
|
13
|
|
|
* @group DataValueExtensions |
|
14
|
|
|
* |
|
15
|
|
|
* @license GPL-2.0-or-later |
|
16
|
|
|
* @author Thiemo Kreuz |
|
17
|
|
|
*/ |
|
18
|
|
|
class ListValidatorTest extends TestCase { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @dataProvider invalidRangeProvider |
|
22
|
|
|
*/ |
|
23
|
|
|
public function testInvalidRange( $range ) { |
|
24
|
|
|
$validator = new ListValidator(); |
|
25
|
|
|
$validator->setOptions( [ 'elementcount' => $range ] ); |
|
26
|
|
|
$this->setExpectedException( 'Exception' ); |
|
27
|
|
|
$validator->validate( [] ); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function invalidRangeProvider() { |
|
31
|
|
|
return [ |
|
32
|
|
|
[ null ], |
|
33
|
|
|
[ 0 ], |
|
34
|
|
|
[ '' ], |
|
35
|
|
|
[ [] ], |
|
36
|
|
|
[ [ 0 ] ], |
|
37
|
|
|
[ [ 0, 0, 0 ] ], |
|
38
|
|
|
]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @dataProvider valueProvider |
|
43
|
|
|
*/ |
|
44
|
|
|
public function testValidate( $value, array $options, $expectedErrors ) { |
|
45
|
|
|
$validator = new ListValidator(); |
|
46
|
|
|
$validator->setOptions( $options ); |
|
47
|
|
|
$result = $validator->validate( $value ); |
|
48
|
|
|
|
|
49
|
|
|
if ( !is_array( $expectedErrors ) ) { |
|
50
|
|
|
$expectedErrors = [ $expectedErrors ]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$this->assertEquals( $expectedErrors, $result->getErrors() ); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function valueProvider() { |
|
57
|
|
|
return [ |
|
58
|
|
|
[ |
|
59
|
|
|
'value' => null, |
|
60
|
|
|
'options' => [], |
|
61
|
|
|
'expectedErrors' => Error::newError( 'Not an array' ) |
|
62
|
|
|
], |
|
63
|
|
|
[ |
|
64
|
|
|
'value' => 0, |
|
65
|
|
|
'options' => [], |
|
66
|
|
|
'expectedErrors' => Error::newError( 'Not an array' ) |
|
67
|
|
|
], |
|
68
|
|
|
[ |
|
69
|
|
|
'value' => '', |
|
70
|
|
|
'options' => [], |
|
71
|
|
|
'expectedErrors' => Error::newError( 'Not an array' ) |
|
72
|
|
|
], |
|
73
|
|
|
[ |
|
74
|
|
|
'value' => [], |
|
75
|
|
|
'options' => [], |
|
76
|
|
|
'expectedErrors' => [] |
|
77
|
|
|
], |
|
78
|
|
|
[ |
|
79
|
|
|
'value' => [ 1 ], |
|
80
|
|
|
'options' => [], |
|
81
|
|
|
'expectedErrors' => [] |
|
82
|
|
|
], |
|
83
|
|
|
|
|
84
|
|
|
// Lower bound only |
|
85
|
|
|
[ |
|
86
|
|
|
'value' => [], |
|
87
|
|
|
'options' => [ 'minelements' => null ], |
|
88
|
|
|
'expectedErrors' => [] |
|
89
|
|
|
], |
|
90
|
|
|
[ |
|
91
|
|
|
'value' => [], |
|
92
|
|
|
'options' => [ 'minelements' => 0 ], |
|
93
|
|
|
'expectedErrors' => [] |
|
94
|
|
|
], |
|
95
|
|
|
[ |
|
96
|
|
|
'value' => [], |
|
97
|
|
|
'options' => [ 'minelements' => 1 ], |
|
98
|
|
|
'expectedErrors' => Error::newError( 'Value exceeding lower bound', 'length' ) |
|
99
|
|
|
], |
|
100
|
|
|
[ |
|
101
|
|
|
'value' => [ 1 ], |
|
102
|
|
|
'options' => [ 'minelements' => 1 ], |
|
103
|
|
|
'expectedErrors' => [] |
|
104
|
|
|
], |
|
105
|
|
|
|
|
106
|
|
|
// Upper bound only |
|
107
|
|
|
[ |
|
108
|
|
|
'value' => [], |
|
109
|
|
|
'options' => [ 'maxelements' => null ], |
|
110
|
|
|
'expectedErrors' => [] |
|
111
|
|
|
], |
|
112
|
|
|
[ |
|
113
|
|
|
'value' => [], |
|
114
|
|
|
'options' => [ 'maxelements' => 0 ], |
|
115
|
|
|
'expectedErrors' => [] |
|
116
|
|
|
], |
|
117
|
|
|
[ |
|
118
|
|
|
'value' => [ 1 ], |
|
119
|
|
|
'options' => [ 'maxelements' => 0 ], |
|
120
|
|
|
'expectedErrors' => Error::newError( 'Value exceeding upper bound', 'length' ) |
|
121
|
|
|
], |
|
122
|
|
|
[ |
|
123
|
|
|
'value' => [ 1 ], |
|
124
|
|
|
'options' => [ 'maxelements' => 1 ], |
|
125
|
|
|
'expectedErrors' => [] |
|
126
|
|
|
], |
|
127
|
|
|
|
|
128
|
|
|
// Lower and upper bound |
|
129
|
|
|
[ |
|
130
|
|
|
'value' => [], |
|
131
|
|
|
'options' => [ 'elementcount' => [ 0, 0 ] ], |
|
132
|
|
|
'expectedErrors' => [] |
|
133
|
|
|
], |
|
134
|
|
|
[ |
|
135
|
|
|
'value' => [ 1 ], |
|
136
|
|
|
'options' => [ 'elementcount' => [ 2, 2 ] ], |
|
137
|
|
|
'expectedErrors' => Error::newError( 'Value exceeding lower bound', 'length' ) |
|
138
|
|
|
], |
|
139
|
|
|
[ |
|
140
|
|
|
'value' => [ 1, 2 ], |
|
141
|
|
|
'options' => [ 'elementcount' => [ 2, 2 ] ], |
|
142
|
|
|
'expectedErrors' => [] |
|
143
|
|
|
], |
|
144
|
|
|
[ |
|
145
|
|
|
'value' => [ 1 ], |
|
146
|
|
|
'options' => [ 'elementcount' => [ 0, 0 ] ], |
|
147
|
|
|
'expectedErrors' => Error::newError( 'Value exceeding upper bound', 'length' ) |
|
148
|
|
|
], |
|
149
|
|
|
[ |
|
150
|
|
|
'value' => [], |
|
151
|
|
|
'options' => [ 'elementcount' => [ 0, 0 ] ], |
|
152
|
|
|
'expectedErrors' => [] |
|
153
|
|
|
], |
|
154
|
|
|
[ |
|
155
|
|
|
'value' => [], |
|
156
|
|
|
'options' => [ 'elementcount' => [ 2, 0 ] ], |
|
157
|
|
|
'expectedErrors' => Error::newError( 'Value exceeding lower bound', 'length' ) |
|
158
|
|
|
], |
|
159
|
|
|
[ |
|
160
|
|
|
'value' => [ 1, 2 ], |
|
161
|
|
|
'options' => [ 'elementcount' => [ 2, 0 ] ], |
|
162
|
|
|
'expectedErrors' => Error::newError( 'Value exceeding upper bound', 'length' ) |
|
163
|
|
|
], |
|
164
|
|
|
[ |
|
165
|
|
|
'value' => [ 1 ], |
|
166
|
|
|
'options' => [ 'elementcount' => [ 2, 0 ] ], |
|
167
|
|
|
'expectedErrors' => [ |
|
168
|
|
|
Error::newError( 'Value exceeding upper bound', 'length' ), |
|
169
|
|
|
Error::newError( 'Value exceeding lower bound', 'length' ), |
|
170
|
|
|
] |
|
171
|
|
|
], |
|
172
|
|
|
[ |
|
173
|
|
|
'value' => [ 1 ], |
|
174
|
|
|
'options' => [ 'minelements' => 2, 'maxelements' => 0 ], |
|
175
|
|
|
'expectedErrors' => [ |
|
176
|
|
|
Error::newError( 'Value exceeding upper bound', 'length' ), |
|
177
|
|
|
Error::newError( 'Value exceeding lower bound', 'length' ), |
|
178
|
|
|
] |
|
179
|
|
|
], |
|
180
|
|
|
|
|
181
|
|
|
// Conflicting options |
|
182
|
|
|
[ |
|
183
|
|
|
'value' => [], |
|
184
|
|
|
'options' => [ 'elementcount' => [ 1, 1 ], 'minelements' => null ], |
|
185
|
|
|
'expectedErrors' => [] |
|
186
|
|
|
], |
|
187
|
|
|
[ |
|
188
|
|
|
'value' => [], |
|
189
|
|
|
'options' => [ 'elementcount' => [ 1, 1 ], 'minelements' => false ], |
|
190
|
|
|
'expectedErrors' => [] |
|
191
|
|
|
], |
|
192
|
|
|
[ |
|
193
|
|
|
'value' => [], |
|
194
|
|
|
'options' => [ 'elementcount' => [ 1, 1 ], 'minelements' => 0 ], |
|
195
|
|
|
'expectedErrors' => [] |
|
196
|
|
|
], |
|
197
|
|
|
[ |
|
198
|
|
|
'value' => [], |
|
199
|
|
|
'options' => [ 'minelements' => 0, 'elementcount' => [ 1, 1 ] ], |
|
200
|
|
|
'expectedErrors' => [] |
|
201
|
|
|
], |
|
202
|
|
|
[ |
|
203
|
|
|
'value' => [ 1 ], |
|
204
|
|
|
'options' => [ 'elementcount' => [ 0, 0 ], 'maxelements' => false ], |
|
205
|
|
|
'expectedErrors' => [] |
|
206
|
|
|
], |
|
207
|
|
|
[ |
|
208
|
|
|
'value' => [ 1 ], |
|
209
|
|
|
'options' => [ 'elementcount' => [ 0, 0 ], 'maxelements' => 1 ], |
|
210
|
|
|
'expectedErrors' => [] |
|
211
|
|
|
], |
|
212
|
|
|
[ |
|
213
|
|
|
'value' => [ 1 ], |
|
214
|
|
|
'options' => [ 'maxelements' => 1, 'elementcount' => [ 0, 0 ] ], |
|
215
|
|
|
'expectedErrors' => [] |
|
216
|
|
|
], |
|
217
|
|
|
]; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
} |
|
221
|
|
|
|