4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Tests\Constraints;
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;
22 abstract class CollectionValidatorTest extends AbstractConstraintValidatorTest
24 protected function getApiVersion()
26 return Validation::API_VERSION_2_5;
29 protected function createValidator()
31 return new CollectionValidator();
34 abstract protected function prepareTestData(array $contents);
36 public function testNullIsValid()
38 $this->validator->validate(null, new Collection(array('fields' => array(
39 'foo' => new Range(array('min' => 4)),
42 $this->assertNoViolation();
45 public function testFieldsAsDefaultOption()
47 $constraint = new Range(array('min' => 4));
49 $data = $this->prepareTestData(array('foo' => 'foobar'));
51 $this->expectValidateValueAt(0, '[foo]', $data['foo'], array($constraint));
53 $this->validator->validate($data, new Collection(array(
57 $this->assertNoViolation();
61 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
63 public function testThrowsExceptionIfNotTraversable()
65 $this->validator->validate('foobar', new Collection(array('fields' => array(
66 'foo' => new Range(array('min' => 4)),
70 public function testWalkSingleConstraint()
72 $constraint = new Range(array('min' => 4));
81 foreach ($array as $key => $value) {
82 $this->expectValidateValueAt($i++, '['.$key.']', $value, array($constraint));
85 $data = $this->prepareTestData($array);
87 $this->validator->validate($data, new Collection(array(
94 $this->assertNoViolation();
97 public function testWalkMultipleConstraints()
100 new Range(array('min' => 4)),
111 foreach ($array as $key => $value) {
112 $this->expectValidateValueAt($i++, '['.$key.']', $value, $constraints);
115 $data = $this->prepareTestData($array);
117 $this->validator->validate($data, new Collection(array(
119 'foo' => $constraints,
120 'bar' => $constraints,
124 $this->assertNoViolation();
127 public function testExtraFieldsDisallowed()
129 $constraint = new Range(array('min' => 4));
131 $data = $this->prepareTestData(array(
136 $this->expectValidateValueAt(0, '[foo]', $data['foo'], array($constraint));
138 $this->validator->validate($data, new Collection(array(
140 'foo' => $constraint,
142 'extraFieldsMessage' => 'myMessage',
145 $this->buildViolation('myMessage')
146 ->setParameter('{{ field }}', '"baz"')
147 ->atPath('property.path[baz]')
149 ->setCode(Collection::NO_SUCH_FIELD_ERROR)
154 public function testNullNotConsideredExtraField()
156 $data = $this->prepareTestData(array(
160 $constraint = new Range(array('min' => 4));
162 $this->expectValidateValueAt(0, '[foo]', $data['foo'], array($constraint));
164 $this->validator->validate($data, new Collection(array(
166 'foo' => $constraint,
170 $this->assertNoViolation();
173 public function testExtraFieldsAllowed()
175 $data = $this->prepareTestData(array(
180 $constraint = new Range(array('min' => 4));
182 $this->expectValidateValueAt(0, '[foo]', $data['foo'], array($constraint));
184 $this->validator->validate($data, new Collection(array(
186 'foo' => $constraint,
188 'allowExtraFields' => true,
191 $this->assertNoViolation();
194 public function testMissingFieldsDisallowed()
196 $data = $this->prepareTestData(array());
198 $constraint = new Range(array('min' => 4));
200 $this->validator->validate($data, new Collection(array(
202 'foo' => $constraint,
204 'missingFieldsMessage' => 'myMessage',
207 $this->buildViolation('myMessage')
208 ->setParameter('{{ field }}', '"foo"')
209 ->atPath('property.path[foo]')
210 ->setInvalidValue(null)
211 ->setCode(Collection::MISSING_FIELD_ERROR)
215 public function testMissingFieldsAllowed()
217 $data = $this->prepareTestData(array());
219 $constraint = new Range(array('min' => 4));
221 $this->validator->validate($data, new Collection(array(
223 'foo' => $constraint,
225 'allowMissingFields' => true,
228 $this->assertNoViolation();
231 public function testOptionalFieldPresent()
233 $data = $this->prepareTestData(array(
237 $this->validator->validate($data, new Collection(array(
238 'foo' => new Optional(),
241 $this->assertNoViolation();
244 public function testOptionalFieldNotPresent()
246 $data = $this->prepareTestData(array());
248 $this->validator->validate($data, new Collection(array(
249 'foo' => new Optional(),
252 $this->assertNoViolation();
255 public function testOptionalFieldSingleConstraint()
261 $constraint = new Range(array('min' => 4));
263 $this->expectValidateValueAt(0, '[foo]', $array['foo'], array($constraint));
265 $data = $this->prepareTestData($array);
267 $this->validator->validate($data, new Collection(array(
268 'foo' => new Optional($constraint),
271 $this->assertNoViolation();
274 public function testOptionalFieldMultipleConstraints()
280 $constraints = array(
282 new Range(array('min' => 4)),
285 $this->expectValidateValueAt(0, '[foo]', $array['foo'], $constraints);
287 $data = $this->prepareTestData($array);
289 $this->validator->validate($data, new Collection(array(
290 'foo' => new Optional($constraints),
293 $this->assertNoViolation();
296 public function testRequiredFieldPresent()
298 $data = $this->prepareTestData(array(
302 $this->validator->validate($data, new Collection(array(
303 'foo' => new Required(),
306 $this->assertNoViolation();
309 public function testRequiredFieldNotPresent()
311 $data = $this->prepareTestData(array());
313 $this->validator->validate($data, new Collection(array(
315 'foo' => new Required(),
317 'missingFieldsMessage' => 'myMessage',
320 $this->buildViolation('myMessage')
321 ->setParameter('{{ field }}', '"foo"')
322 ->atPath('property.path[foo]')
323 ->setInvalidValue(null)
324 ->setCode(Collection::MISSING_FIELD_ERROR)
328 public function testRequiredFieldSingleConstraint()
334 $constraint = new Range(array('min' => 4));
336 $this->expectValidateValueAt(0, '[foo]', $array['foo'], array($constraint));
338 $data = $this->prepareTestData($array);
340 $this->validator->validate($data, new Collection(array(
341 'foo' => new Required($constraint),
344 $this->assertNoViolation();
347 public function testRequiredFieldMultipleConstraints()
353 $constraints = array(
355 new Range(array('min' => 4)),
358 $this->expectValidateValueAt(0, '[foo]', $array['foo'], $constraints);
360 $data = $this->prepareTestData($array);
362 $this->validator->validate($data, new Collection(array(
363 'foo' => new Required($constraints),
366 $this->assertNoViolation();
369 public function testObjectShouldBeLeftUnchanged()
371 $value = new \ArrayObject(array(
375 $constraint = new Range(array('min' => 2));
377 $this->expectValidateValueAt(0, '[foo]', $value['foo'], array($constraint));
379 $this->validator->validate($value, new Collection(array(
381 'foo' => $constraint,
385 $this->assertEquals(array(