Yaffs site version 1.1
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / CollectionValidatorTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Tests\Constraints;
13
14 use Symfony\Component\Validator\Constraints\Collection;
15 use Symfony\Component\Validator\Constraints\CollectionValidator;
16 use Symfony\Component\Validator\Constraints\NotNull;
17 use Symfony\Component\Validator\Constraints\Optional;
18 use Symfony\Component\Validator\Constraints\Range;
19 use Symfony\Component\Validator\Constraints\Required;
20 use Symfony\Component\Validator\Validation;
21
22 abstract class CollectionValidatorTest extends AbstractConstraintValidatorTest
23 {
24     protected function getApiVersion()
25     {
26         return Validation::API_VERSION_2_5;
27     }
28
29     protected function createValidator()
30     {
31         return new CollectionValidator();
32     }
33
34     abstract protected function prepareTestData(array $contents);
35
36     public function testNullIsValid()
37     {
38         $this->validator->validate(null, new Collection(array('fields' => array(
39             'foo' => new Range(array('min' => 4)),
40         ))));
41
42         $this->assertNoViolation();
43     }
44
45     public function testFieldsAsDefaultOption()
46     {
47         $constraint = new Range(array('min' => 4));
48
49         $data = $this->prepareTestData(array('foo' => 'foobar'));
50
51         $this->expectValidateValueAt(0, '[foo]', $data['foo'], array($constraint));
52
53         $this->validator->validate($data, new Collection(array(
54             'foo' => $constraint,
55         )));
56
57         $this->assertNoViolation();
58     }
59
60     /**
61      * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
62      */
63     public function testThrowsExceptionIfNotTraversable()
64     {
65         $this->validator->validate('foobar', new Collection(array('fields' => array(
66             'foo' => new Range(array('min' => 4)),
67         ))));
68     }
69
70     public function testWalkSingleConstraint()
71     {
72         $constraint = new Range(array('min' => 4));
73
74         $array = array(
75             'foo' => 3,
76             'bar' => 5,
77         );
78
79         $i = 0;
80
81         foreach ($array as $key => $value) {
82             $this->expectValidateValueAt($i++, '['.$key.']', $value, array($constraint));
83         }
84
85         $data = $this->prepareTestData($array);
86
87         $this->validator->validate($data, new Collection(array(
88             'fields' => array(
89                 'foo' => $constraint,
90                 'bar' => $constraint,
91             ),
92         )));
93
94         $this->assertNoViolation();
95     }
96
97     public function testWalkMultipleConstraints()
98     {
99         $constraints = array(
100             new Range(array('min' => 4)),
101             new NotNull(),
102         );
103
104         $array = array(
105             'foo' => 3,
106             'bar' => 5,
107         );
108
109         $i = 0;
110
111         foreach ($array as $key => $value) {
112             $this->expectValidateValueAt($i++, '['.$key.']', $value, $constraints);
113         }
114
115         $data = $this->prepareTestData($array);
116
117         $this->validator->validate($data, new Collection(array(
118             'fields' => array(
119                 'foo' => $constraints,
120                 'bar' => $constraints,
121             ),
122         )));
123
124         $this->assertNoViolation();
125     }
126
127     public function testExtraFieldsDisallowed()
128     {
129         $constraint = new Range(array('min' => 4));
130
131         $data = $this->prepareTestData(array(
132             'foo' => 5,
133             'baz' => 6,
134         ));
135
136         $this->expectValidateValueAt(0, '[foo]', $data['foo'], array($constraint));
137
138         $this->validator->validate($data, new Collection(array(
139             'fields' => array(
140                 'foo' => $constraint,
141             ),
142             'extraFieldsMessage' => 'myMessage',
143         )));
144
145         $this->buildViolation('myMessage')
146             ->setParameter('{{ field }}', '"baz"')
147             ->atPath('property.path[baz]')
148             ->setInvalidValue(6)
149             ->setCode(Collection::NO_SUCH_FIELD_ERROR)
150             ->assertRaised();
151     }
152
153     // bug fix
154     public function testNullNotConsideredExtraField()
155     {
156         $data = $this->prepareTestData(array(
157             'foo' => null,
158         ));
159
160         $constraint = new Range(array('min' => 4));
161
162         $this->expectValidateValueAt(0, '[foo]', $data['foo'], array($constraint));
163
164         $this->validator->validate($data, new Collection(array(
165             'fields' => array(
166                 'foo' => $constraint,
167             ),
168         )));
169
170         $this->assertNoViolation();
171     }
172
173     public function testExtraFieldsAllowed()
174     {
175         $data = $this->prepareTestData(array(
176             'foo' => 5,
177             'bar' => 6,
178         ));
179
180         $constraint = new Range(array('min' => 4));
181
182         $this->expectValidateValueAt(0, '[foo]', $data['foo'], array($constraint));
183
184         $this->validator->validate($data, new Collection(array(
185             'fields' => array(
186                 'foo' => $constraint,
187             ),
188             'allowExtraFields' => true,
189         )));
190
191         $this->assertNoViolation();
192     }
193
194     public function testMissingFieldsDisallowed()
195     {
196         $data = $this->prepareTestData(array());
197
198         $constraint = new Range(array('min' => 4));
199
200         $this->validator->validate($data, new Collection(array(
201             'fields' => array(
202                 'foo' => $constraint,
203             ),
204             'missingFieldsMessage' => 'myMessage',
205         )));
206
207         $this->buildViolation('myMessage')
208             ->setParameter('{{ field }}', '"foo"')
209             ->atPath('property.path[foo]')
210             ->setInvalidValue(null)
211             ->setCode(Collection::MISSING_FIELD_ERROR)
212             ->assertRaised();
213     }
214
215     public function testMissingFieldsAllowed()
216     {
217         $data = $this->prepareTestData(array());
218
219         $constraint = new Range(array('min' => 4));
220
221         $this->validator->validate($data, new Collection(array(
222             'fields' => array(
223                 'foo' => $constraint,
224             ),
225             'allowMissingFields' => true,
226         )));
227
228         $this->assertNoViolation();
229     }
230
231     public function testOptionalFieldPresent()
232     {
233         $data = $this->prepareTestData(array(
234             'foo' => null,
235         ));
236
237         $this->validator->validate($data, new Collection(array(
238             'foo' => new Optional(),
239         )));
240
241         $this->assertNoViolation();
242     }
243
244     public function testOptionalFieldNotPresent()
245     {
246         $data = $this->prepareTestData(array());
247
248         $this->validator->validate($data, new Collection(array(
249             'foo' => new Optional(),
250         )));
251
252         $this->assertNoViolation();
253     }
254
255     public function testOptionalFieldSingleConstraint()
256     {
257         $array = array(
258             'foo' => 5,
259         );
260
261         $constraint = new Range(array('min' => 4));
262
263         $this->expectValidateValueAt(0, '[foo]', $array['foo'], array($constraint));
264
265         $data = $this->prepareTestData($array);
266
267         $this->validator->validate($data, new Collection(array(
268             'foo' => new Optional($constraint),
269         )));
270
271         $this->assertNoViolation();
272     }
273
274     public function testOptionalFieldMultipleConstraints()
275     {
276         $array = array(
277             'foo' => 5,
278         );
279
280         $constraints = array(
281             new NotNull(),
282             new Range(array('min' => 4)),
283         );
284
285         $this->expectValidateValueAt(0, '[foo]', $array['foo'], $constraints);
286
287         $data = $this->prepareTestData($array);
288
289         $this->validator->validate($data, new Collection(array(
290             'foo' => new Optional($constraints),
291         )));
292
293         $this->assertNoViolation();
294     }
295
296     public function testRequiredFieldPresent()
297     {
298         $data = $this->prepareTestData(array(
299             'foo' => null,
300         ));
301
302         $this->validator->validate($data, new Collection(array(
303             'foo' => new Required(),
304         )));
305
306         $this->assertNoViolation();
307     }
308
309     public function testRequiredFieldNotPresent()
310     {
311         $data = $this->prepareTestData(array());
312
313         $this->validator->validate($data, new Collection(array(
314             'fields' => array(
315                 'foo' => new Required(),
316             ),
317             'missingFieldsMessage' => 'myMessage',
318         )));
319
320         $this->buildViolation('myMessage')
321             ->setParameter('{{ field }}', '"foo"')
322             ->atPath('property.path[foo]')
323             ->setInvalidValue(null)
324             ->setCode(Collection::MISSING_FIELD_ERROR)
325             ->assertRaised();
326     }
327
328     public function testRequiredFieldSingleConstraint()
329     {
330         $array = array(
331             'foo' => 5,
332         );
333
334         $constraint = new Range(array('min' => 4));
335
336         $this->expectValidateValueAt(0, '[foo]', $array['foo'], array($constraint));
337
338         $data = $this->prepareTestData($array);
339
340         $this->validator->validate($data, new Collection(array(
341             'foo' => new Required($constraint),
342         )));
343
344         $this->assertNoViolation();
345     }
346
347     public function testRequiredFieldMultipleConstraints()
348     {
349         $array = array(
350             'foo' => 5,
351         );
352
353         $constraints = array(
354             new NotNull(),
355             new Range(array('min' => 4)),
356         );
357
358         $this->expectValidateValueAt(0, '[foo]', $array['foo'], $constraints);
359
360         $data = $this->prepareTestData($array);
361
362         $this->validator->validate($data, new Collection(array(
363             'foo' => new Required($constraints),
364         )));
365
366         $this->assertNoViolation();
367     }
368
369     public function testObjectShouldBeLeftUnchanged()
370     {
371         $value = new \ArrayObject(array(
372             'foo' => 3,
373         ));
374
375         $constraint = new Range(array('min' => 2));
376
377         $this->expectValidateValueAt(0, '[foo]', $value['foo'], array($constraint));
378
379         $this->validator->validate($value, new Collection(array(
380             'fields' => array(
381                 'foo' => $constraint,
382             ),
383         )));
384
385         $this->assertEquals(array(
386             'foo' => 3,
387         ), (array) $value);
388     }
389 }