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