13
13
14
14
use Symfony \Component \Validator \ExecutionContext ;
15
15
use Symfony \Component \Validator \Constraints \Min ;
16
+ use Symfony \Component \Validator \Constraints \NotBlank ;
17
+ use Symfony \Component \Validator \Constraints \NotNull ;
16
18
use Symfony \Component \Validator \Constraints \Collection ;
17
19
use Symfony \Component \Validator \Constraints \CollectionValidator ;
18
20
21
+ /**
22
+ * This class is a hand written simplified version of PHP native `ArrayObject`
23
+ * class, to show that it behaves different than PHP native implementation.
24
+ */
25
+ class TestArrayObject implements \ArrayAccess, \IteratorAggregate, \Countable, \Serializable
26
+ {
27
+ private $ array ;
28
+
29
+ public function __construct (array $ array = null )
30
+ {
31
+ $ this ->array = (array ) ($ array ?: array ());
32
+ }
33
+
34
+ public function offsetExists ($ offset )
35
+ {
36
+ return array_key_exists ($ offset , $ this ->array );
37
+ }
38
+
39
+ public function offsetGet ($ offset )
40
+ {
41
+ return $ this ->array [$ offset ];
42
+ }
43
+
44
+ public function offsetSet ($ offset , $ value )
45
+ {
46
+ if (null === $ offset ) {
47
+ $ this ->array [] = $ value ;
48
+ } else {
49
+ $ this ->array [$ offset ] = $ value ;
50
+ }
51
+ }
52
+
53
+ public function offsetUnset ($ offset )
54
+ {
55
+ if (array_key_exists ($ offset , $ this ->array )) {
56
+ unset($ this ->array [$ offset ]);
57
+ }
58
+ }
59
+
60
+ public function getIterator ()
61
+ {
62
+ return new \ArrayIterator ($ this ->array );
63
+ }
64
+
65
+ public function count ()
66
+ {
67
+ return count ($ this ->array );
68
+ }
69
+
70
+ public function serialize ()
71
+ {
72
+ return serialize ($ this ->array );
73
+ }
74
+
75
+ public function unserialize ($ serialized )
76
+ {
77
+ $ this ->array = (array ) unserialize ((string ) $ serialized );
78
+ }
79
+ }
80
+
19
81
class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
20
82
{
21
83
protected $ validator ;
@@ -49,15 +111,22 @@ public function testNullIsValid()
49
111
50
112
public function testFieldsAsDefaultOption ()
51
113
{
52
- $ this ->validator ->isValid (array ('foo ' => 'foobar ' ), new Collection (array (
114
+ $ this ->assertTrue ( $ this -> validator ->isValid (array ('foo ' => 'foobar ' ), new Collection (array (
53
115
'foo ' => new Min (4 ),
54
- )));
116
+ ))));
117
+ $ this ->assertTrue ($ this ->validator ->isValid (new \ArrayObject (array ('foo ' => 'foobar ' )), new Collection (array (
118
+ 'foo ' => new Min (4 ),
119
+ ))));
120
+ $ this ->assertTrue ($ this ->validator ->isValid (new TestArrayObject (array ('foo ' => 'foobar ' )), new Collection (array (
121
+ 'foo ' => new Min (4 ),
122
+ ))));
55
123
}
56
124
125
+ /**
126
+ * @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException
127
+ */
57
128
public function testThrowsExceptionIfNotTraversable ()
58
129
{
59
- $ this ->setExpectedException ('Symfony\Component\Validator\Exception\UnexpectedTypeException ' );
60
-
61
130
$ this ->validator ->isValid ('foobar ' , new Collection (array ('fields ' => array (
62
131
'foo ' => new Min (4 ),
63
132
))));
@@ -112,13 +181,11 @@ public function testWalkMultipleConstraints($array)
112
181
))));
113
182
}
114
183
115
- public function testExtraFieldsDisallowed ()
184
+ /**
185
+ * @dataProvider getArgumentsWithExtraFields
186
+ */
187
+ public function testExtraFieldsDisallowed ($ array )
116
188
{
117
- $ array = array (
118
- 'foo ' => 5 ,
119
- 'bar ' => 6 ,
120
- );
121
-
122
189
$ this ->assertFalse ($ this ->validator ->isValid ($ array , new Collection (array (
123
190
'fields ' => array (
124
191
'foo ' => new Min (4 ),
@@ -132,12 +199,15 @@ public function testNullNotConsideredExtraField()
132
199
$ array = array (
133
200
'foo ' => null ,
134
201
);
135
-
136
- $ this ->assertTrue ($ this ->validator ->isValid ($ array , new Collection (array (
202
+ $ collection = new Collection (array (
137
203
'fields ' => array (
138
204
'foo ' => new Min (4 ),
139
205
),
140
- ))));
206
+ ));
207
+
208
+ $ this ->assertTrue ($ this ->validator ->isValid ($ array , $ collection ));
209
+ $ this ->assertTrue ($ this ->validator ->isValid (new \ArrayObject ($ array ), $ collection ));
210
+ $ this ->assertTrue ($ this ->validator ->isValid (new TestArrayObject ($ array ), $ collection ));
141
211
}
142
212
143
213
public function testExtraFieldsAllowed ()
@@ -146,13 +216,16 @@ public function testExtraFieldsAllowed()
146
216
'foo ' => 5 ,
147
217
'bar ' => 6 ,
148
218
);
149
-
150
- $ this ->assertTrue ($ this ->validator ->isValid ($ array , new Collection (array (
219
+ $ collection = new Collection (array (
151
220
'fields ' => array (
152
221
'foo ' => new Min (4 ),
153
222
),
154
223
'allowExtraFields ' => true ,
155
- ))));
224
+ ));
225
+
226
+ $ this ->assertTrue ($ this ->validator ->isValid ($ array , $ collection ));
227
+ $ this ->assertTrue ($ this ->validator ->isValid (new \ArrayObject ($ array ), $ collection ));
228
+ $ this ->assertTrue ($ this ->validator ->isValid (new TestArrayObject ($ array ), $ collection ));
156
229
}
157
230
158
231
public function testMissingFieldsDisallowed ()
@@ -162,6 +235,16 @@ public function testMissingFieldsDisallowed()
162
235
'foo ' => new Min (4 ),
163
236
),
164
237
))));
238
+ $ this ->assertFalse ($ this ->validator ->isValid (new \ArrayObject (array ()), new Collection (array (
239
+ 'fields ' => array (
240
+ 'foo ' => new Min (4 ),
241
+ ),
242
+ ))));
243
+ $ this ->assertFalse ($ this ->validator ->isValid (new TestArrayObject (array ()), new Collection (array (
244
+ 'fields ' => array (
245
+ 'foo ' => new Min (4 ),
246
+ ),
247
+ ))));
165
248
}
166
249
167
250
public function testMissingFieldsAllowed ()
@@ -172,16 +255,58 @@ public function testMissingFieldsAllowed()
172
255
),
173
256
'allowMissingFields ' => true ,
174
257
))));
258
+ $ this ->assertTrue ($ this ->validator ->isValid (new \ArrayObject (array ()), new Collection (array (
259
+ 'fields ' => array (
260
+ 'foo ' => new Min (4 ),
261
+ ),
262
+ 'allowMissingFields ' => true ,
263
+ ))));
264
+ $ this ->assertTrue ($ this ->validator ->isValid (new TestArrayObject (array ()), new Collection (array (
265
+ 'fields ' => array (
266
+ 'foo ' => new Min (4 ),
267
+ ),
268
+ 'allowMissingFields ' => true ,
269
+ ))));
175
270
}
176
271
177
- public function getValidArguments ()
178
- {
179
- return array (
180
- // can only test for one entry, because PHPUnits mocking does not allow
181
- // to expect multiple method calls with different arguments
182
- array (array ('foo ' => 3 )),
183
- array (new \ArrayObject (array ('foo ' => 3 ))),
184
- );
272
+ public function testArrayAccessObject () {
273
+ $ value = new TestArrayObject ();
274
+ $ value ['foo ' ] = 12 ;
275
+ $ value ['asdf ' ] = 'asdfaf ' ;
276
+
277
+ $ this ->assertTrue (isset ($ value ['asdf ' ]));
278
+ $ this ->assertTrue (isset ($ value ['foo ' ]));
279
+ $ this ->assertFalse (empty ($ value ['asdf ' ]));
280
+ $ this ->assertFalse (empty ($ value ['foo ' ]));
281
+
282
+ $ result = $ this ->validator ->isValid ($ value , new Collection (array (
283
+ 'fields ' => array (
284
+ 'foo ' => new NotBlank (),
285
+ 'asdf ' => new NotBlank ()
286
+ )
287
+ )));
288
+
289
+ $ this ->assertTrue ($ result );
290
+ }
291
+
292
+ public function testArrayObject () {
293
+ $ value = new \ArrayObject (array ());
294
+ $ value ['foo ' ] = 12 ;
295
+ $ value ['asdf ' ] = 'asdfaf ' ;
296
+
297
+ $ this ->assertTrue (isset ($ value ['asdf ' ]));
298
+ $ this ->assertTrue (isset ($ value ['foo ' ]));
299
+ $ this ->assertFalse (empty ($ value ['asdf ' ]));
300
+ $ this ->assertFalse (empty ($ value ['foo ' ]));
301
+
302
+ $ result = $ this ->validator ->isValid ($ value , new Collection (array (
303
+ 'fields ' => array (
304
+ 'foo ' => new NotBlank (),
305
+ 'asdf ' => new NotBlank ()
306
+ )
307
+ )));
308
+
309
+ $ this ->assertTrue ($ result );
185
310
}
186
311
187
312
public function testObjectShouldBeLeftUnchanged ()
@@ -199,4 +324,34 @@ public function testObjectShouldBeLeftUnchanged()
199
324
'foo ' => 3
200
325
), (array ) $ value );
201
326
}
327
+
328
+ public function getValidArguments ()
329
+ {
330
+ return array (
331
+ // can only test for one entry, because PHPUnits mocking does not allow
332
+ // to expect multiple method calls with different arguments
333
+ array (array ('foo ' => 3 )),
334
+ array (new \ArrayObject (array ('foo ' => 3 ))),
335
+ array (new TestArrayObject (array ('foo ' => 3 ))),
336
+ );
337
+ }
338
+
339
+ public function getArgumentsWithExtraFields ()
340
+ {
341
+ return array (
342
+ array (array (
343
+ 'foo ' => 5 ,
344
+ 'bar ' => 6 ,
345
+ )),
346
+ array (new \ArrayObject (array (
347
+ 'foo ' => 5 ,
348
+ 'bar ' => 6 ,
349
+ ))),
350
+ array (new TestArrayObject (array (
351
+ 'foo ' => 5 ,
352
+ 'bar ' => 6 ,
353
+ )))
354
+ );
355
+ }
202
356
}
357
+
0 commit comments