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\Test\ConstraintValidatorTestCase;
22 abstract class CollectionValidatorTest extends ConstraintValidatorTestCase
24 protected function createValidator()
26 return new CollectionValidator();
29 abstract protected function prepareTestData(array $contents);
31 public function testNullIsValid()
33 $this->validator->validate(null, new Collection(array('fields' => array(
34 'foo' => new Range(array('min' => 4)),
37 $this->assertNoViolation();
40 public function testFieldsAsDefaultOption()
42 $constraint = new Range(array('min' => 4));
44 $data = $this->prepareTestData(array('foo' => 'foobar'));
46 $this->expectValidateValueAt(0, '[foo]', $data['foo'], array($constraint));
48 $this->validator->validate($data, new Collection(array(
52 $this->assertNoViolation();
56 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
58 public function testThrowsExceptionIfNotTraversable()
60 $this->validator->validate('foobar', new Collection(array('fields' => array(
61 'foo' => new Range(array('min' => 4)),
65 public function testWalkSingleConstraint()
67 $constraint = new Range(array('min' => 4));
76 foreach ($array as $key => $value) {
77 $this->expectValidateValueAt($i++, '['.$key.']', $value, array($constraint));
80 $data = $this->prepareTestData($array);
82 $this->validator->validate($data, new Collection(array(
89 $this->assertNoViolation();
92 public function testWalkMultipleConstraints()
95 new Range(array('min' => 4)),
106 foreach ($array as $key => $value) {
107 $this->expectValidateValueAt($i++, '['.$key.']', $value, $constraints);
110 $data = $this->prepareTestData($array);
112 $this->validator->validate($data, new Collection(array(
114 'foo' => $constraints,
115 'bar' => $constraints,
119 $this->assertNoViolation();
122 public function testExtraFieldsDisallowed()
124 $constraint = new Range(array('min' => 4));
126 $data = $this->prepareTestData(array(
131 $this->expectValidateValueAt(0, '[foo]', $data['foo'], array($constraint));
133 $this->validator->validate($data, new Collection(array(
135 'foo' => $constraint,
137 'extraFieldsMessage' => 'myMessage',
140 $this->buildViolation('myMessage')
141 ->setParameter('{{ field }}', '"baz"')
142 ->atPath('property.path[baz]')
144 ->setCode(Collection::NO_SUCH_FIELD_ERROR)
149 public function testNullNotConsideredExtraField()
151 $data = $this->prepareTestData(array(
155 $constraint = new Range(array('min' => 4));
157 $this->expectValidateValueAt(0, '[foo]', $data['foo'], array($constraint));
159 $this->validator->validate($data, new Collection(array(
161 'foo' => $constraint,
165 $this->assertNoViolation();
168 public function testExtraFieldsAllowed()
170 $data = $this->prepareTestData(array(
175 $constraint = new Range(array('min' => 4));
177 $this->expectValidateValueAt(0, '[foo]', $data['foo'], array($constraint));
179 $this->validator->validate($data, new Collection(array(
181 'foo' => $constraint,
183 'allowExtraFields' => true,
186 $this->assertNoViolation();
189 public function testMissingFieldsDisallowed()
191 $data = $this->prepareTestData(array());
193 $constraint = new Range(array('min' => 4));
195 $this->validator->validate($data, new Collection(array(
197 'foo' => $constraint,
199 'missingFieldsMessage' => 'myMessage',
202 $this->buildViolation('myMessage')
203 ->setParameter('{{ field }}', '"foo"')
204 ->atPath('property.path[foo]')
205 ->setInvalidValue(null)
206 ->setCode(Collection::MISSING_FIELD_ERROR)
210 public function testMissingFieldsAllowed()
212 $data = $this->prepareTestData(array());
214 $constraint = new Range(array('min' => 4));
216 $this->validator->validate($data, new Collection(array(
218 'foo' => $constraint,
220 'allowMissingFields' => true,
223 $this->assertNoViolation();
226 public function testOptionalFieldPresent()
228 $data = $this->prepareTestData(array(
232 $this->validator->validate($data, new Collection(array(
233 'foo' => new Optional(),
236 $this->assertNoViolation();
239 public function testOptionalFieldNotPresent()
241 $data = $this->prepareTestData(array());
243 $this->validator->validate($data, new Collection(array(
244 'foo' => new Optional(),
247 $this->assertNoViolation();
250 public function testOptionalFieldSingleConstraint()
256 $constraint = new Range(array('min' => 4));
258 $this->expectValidateValueAt(0, '[foo]', $array['foo'], array($constraint));
260 $data = $this->prepareTestData($array);
262 $this->validator->validate($data, new Collection(array(
263 'foo' => new Optional($constraint),
266 $this->assertNoViolation();
269 public function testOptionalFieldMultipleConstraints()
275 $constraints = array(
277 new Range(array('min' => 4)),
280 $this->expectValidateValueAt(0, '[foo]', $array['foo'], $constraints);
282 $data = $this->prepareTestData($array);
284 $this->validator->validate($data, new Collection(array(
285 'foo' => new Optional($constraints),
288 $this->assertNoViolation();
291 public function testRequiredFieldPresent()
293 $data = $this->prepareTestData(array(
297 $this->validator->validate($data, new Collection(array(
298 'foo' => new Required(),
301 $this->assertNoViolation();
304 public function testRequiredFieldNotPresent()
306 $data = $this->prepareTestData(array());
308 $this->validator->validate($data, new Collection(array(
310 'foo' => new Required(),
312 'missingFieldsMessage' => 'myMessage',
315 $this->buildViolation('myMessage')
316 ->setParameter('{{ field }}', '"foo"')
317 ->atPath('property.path[foo]')
318 ->setInvalidValue(null)
319 ->setCode(Collection::MISSING_FIELD_ERROR)
323 public function testRequiredFieldSingleConstraint()
329 $constraint = new Range(array('min' => 4));
331 $this->expectValidateValueAt(0, '[foo]', $array['foo'], array($constraint));
333 $data = $this->prepareTestData($array);
335 $this->validator->validate($data, new Collection(array(
336 'foo' => new Required($constraint),
339 $this->assertNoViolation();
342 public function testRequiredFieldMultipleConstraints()
348 $constraints = array(
350 new Range(array('min' => 4)),
353 $this->expectValidateValueAt(0, '[foo]', $array['foo'], $constraints);
355 $data = $this->prepareTestData($array);
357 $this->validator->validate($data, new Collection(array(
358 'foo' => new Required($constraints),
361 $this->assertNoViolation();
364 public function testObjectShouldBeLeftUnchanged()
366 $value = new \ArrayObject(array(
370 $constraint = new Range(array('min' => 2));
372 $this->expectValidateValueAt(0, '[foo]', $value['foo'], array($constraint));
374 $this->validator->validate($value, new Collection(array(
376 'foo' => $constraint,
380 $this->assertEquals(array(