123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Validator\Tests\Constraints;
- use Symfony\Component\Validator\Constraints\Collection;
- use Symfony\Component\Validator\Constraints\CollectionValidator;
- use Symfony\Component\Validator\Constraints\NotNull;
- use Symfony\Component\Validator\Constraints\Optional;
- use Symfony\Component\Validator\Constraints\Range;
- use Symfony\Component\Validator\Constraints\Required;
- use Symfony\Component\Validator\Validation;
- abstract class CollectionValidatorTest extends AbstractConstraintValidatorTest
- {
- protected function getApiVersion()
- {
- return Validation::API_VERSION_2_5;
- }
- protected function createValidator()
- {
- return new CollectionValidator();
- }
- abstract protected function prepareTestData(array $contents);
- public function testNullIsValid()
- {
- $this->validator->validate(null, new Collection(array('fields' => array(
- 'foo' => new Range(array('min' => 4)),
- ))));
- $this->assertNoViolation();
- }
- public function testFieldsAsDefaultOption()
- {
- $constraint = new Range(array('min' => 4));
- $data = $this->prepareTestData(array('foo' => 'foobar'));
- $this->expectValidateValueAt(0, '[foo]', $data['foo'], array($constraint));
- $this->validator->validate($data, new Collection(array(
- 'foo' => $constraint,
- )));
- $this->assertNoViolation();
- }
- /**
- * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
- */
- public function testThrowsExceptionIfNotTraversable()
- {
- $this->validator->validate('foobar', new Collection(array('fields' => array(
- 'foo' => new Range(array('min' => 4)),
- ))));
- }
- public function testWalkSingleConstraint()
- {
- $constraint = new Range(array('min' => 4));
- $array = array(
- 'foo' => 3,
- 'bar' => 5,
- );
- $i = 0;
- foreach ($array as $key => $value) {
- $this->expectValidateValueAt($i++, '['.$key.']', $value, array($constraint));
- }
- $data = $this->prepareTestData($array);
- $this->validator->validate($data, new Collection(array(
- 'fields' => array(
- 'foo' => $constraint,
- 'bar' => $constraint,
- ),
- )));
- $this->assertNoViolation();
- }
- public function testWalkMultipleConstraints()
- {
- $constraints = array(
- new Range(array('min' => 4)),
- new NotNull(),
- );
- $array = array(
- 'foo' => 3,
- 'bar' => 5,
- );
- $i = 0;
- foreach ($array as $key => $value) {
- $this->expectValidateValueAt($i++, '['.$key.']', $value, $constraints);
- }
- $data = $this->prepareTestData($array);
- $this->validator->validate($data, new Collection(array(
- 'fields' => array(
- 'foo' => $constraints,
- 'bar' => $constraints,
- ),
- )));
- $this->assertNoViolation();
- }
- public function testExtraFieldsDisallowed()
- {
- $constraint = new Range(array('min' => 4));
- $data = $this->prepareTestData(array(
- 'foo' => 5,
- 'baz' => 6,
- ));
- $this->expectValidateValueAt(0, '[foo]', $data['foo'], array($constraint));
- $this->validator->validate($data, new Collection(array(
- 'fields' => array(
- 'foo' => $constraint,
- ),
- 'extraFieldsMessage' => 'myMessage',
- )));
- $this->buildViolation('myMessage')
- ->setParameter('{{ field }}', '"baz"')
- ->atPath('property.path[baz]')
- ->setInvalidValue(6)
- ->setCode(Collection::NO_SUCH_FIELD_ERROR)
- ->assertRaised();
- }
- // bug fix
- public function testNullNotConsideredExtraField()
- {
- $data = $this->prepareTestData(array(
- 'foo' => null,
- ));
- $constraint = new Range(array('min' => 4));
- $this->expectValidateValueAt(0, '[foo]', $data['foo'], array($constraint));
- $this->validator->validate($data, new Collection(array(
- 'fields' => array(
- 'foo' => $constraint,
- ),
- )));
- $this->assertNoViolation();
- }
- public function testExtraFieldsAllowed()
- {
- $data = $this->prepareTestData(array(
- 'foo' => 5,
- 'bar' => 6,
- ));
- $constraint = new Range(array('min' => 4));
- $this->expectValidateValueAt(0, '[foo]', $data['foo'], array($constraint));
- $this->validator->validate($data, new Collection(array(
- 'fields' => array(
- 'foo' => $constraint,
- ),
- 'allowExtraFields' => true,
- )));
- $this->assertNoViolation();
- }
- public function testMissingFieldsDisallowed()
- {
- $data = $this->prepareTestData(array());
- $constraint = new Range(array('min' => 4));
- $this->validator->validate($data, new Collection(array(
- 'fields' => array(
- 'foo' => $constraint,
- ),
- 'missingFieldsMessage' => 'myMessage',
- )));
- $this->buildViolation('myMessage')
- ->setParameter('{{ field }}', '"foo"')
- ->atPath('property.path[foo]')
- ->setInvalidValue(null)
- ->setCode(Collection::MISSING_FIELD_ERROR)
- ->assertRaised();
- }
- public function testMissingFieldsAllowed()
- {
- $data = $this->prepareTestData(array());
- $constraint = new Range(array('min' => 4));
- $this->validator->validate($data, new Collection(array(
- 'fields' => array(
- 'foo' => $constraint,
- ),
- 'allowMissingFields' => true,
- )));
- $this->assertNoViolation();
- }
- public function testOptionalFieldPresent()
- {
- $data = $this->prepareTestData(array(
- 'foo' => null,
- ));
- $this->validator->validate($data, new Collection(array(
- 'foo' => new Optional(),
- )));
- $this->assertNoViolation();
- }
- public function testOptionalFieldNotPresent()
- {
- $data = $this->prepareTestData(array());
- $this->validator->validate($data, new Collection(array(
- 'foo' => new Optional(),
- )));
- $this->assertNoViolation();
- }
- public function testOptionalFieldSingleConstraint()
- {
- $array = array(
- 'foo' => 5,
- );
- $constraint = new Range(array('min' => 4));
- $this->expectValidateValueAt(0, '[foo]', $array['foo'], array($constraint));
- $data = $this->prepareTestData($array);
- $this->validator->validate($data, new Collection(array(
- 'foo' => new Optional($constraint),
- )));
- $this->assertNoViolation();
- }
- public function testOptionalFieldMultipleConstraints()
- {
- $array = array(
- 'foo' => 5,
- );
- $constraints = array(
- new NotNull(),
- new Range(array('min' => 4)),
- );
- $this->expectValidateValueAt(0, '[foo]', $array['foo'], $constraints);
- $data = $this->prepareTestData($array);
- $this->validator->validate($data, new Collection(array(
- 'foo' => new Optional($constraints),
- )));
- $this->assertNoViolation();
- }
- public function testRequiredFieldPresent()
- {
- $data = $this->prepareTestData(array(
- 'foo' => null,
- ));
- $this->validator->validate($data, new Collection(array(
- 'foo' => new Required(),
- )));
- $this->assertNoViolation();
- }
- public function testRequiredFieldNotPresent()
- {
- $data = $this->prepareTestData(array());
- $this->validator->validate($data, new Collection(array(
- 'fields' => array(
- 'foo' => new Required(),
- ),
- 'missingFieldsMessage' => 'myMessage',
- )));
- $this->buildViolation('myMessage')
- ->setParameter('{{ field }}', '"foo"')
- ->atPath('property.path[foo]')
- ->setInvalidValue(null)
- ->setCode(Collection::MISSING_FIELD_ERROR)
- ->assertRaised();
- }
- public function testRequiredFieldSingleConstraint()
- {
- $array = array(
- 'foo' => 5,
- );
- $constraint = new Range(array('min' => 4));
- $this->expectValidateValueAt(0, '[foo]', $array['foo'], array($constraint));
- $data = $this->prepareTestData($array);
- $this->validator->validate($data, new Collection(array(
- 'foo' => new Required($constraint),
- )));
- $this->assertNoViolation();
- }
- public function testRequiredFieldMultipleConstraints()
- {
- $array = array(
- 'foo' => 5,
- );
- $constraints = array(
- new NotNull(),
- new Range(array('min' => 4)),
- );
- $this->expectValidateValueAt(0, '[foo]', $array['foo'], $constraints);
- $data = $this->prepareTestData($array);
- $this->validator->validate($data, new Collection(array(
- 'foo' => new Required($constraints),
- )));
- $this->assertNoViolation();
- }
- public function testObjectShouldBeLeftUnchanged()
- {
- $value = new \ArrayObject(array(
- 'foo' => 3,
- ));
- $constraint = new Range(array('min' => 2));
- $this->expectValidateValueAt(0, '[foo]', $value['foo'], array($constraint));
- $this->validator->validate($value, new Collection(array(
- 'fields' => array(
- 'foo' => $constraint,
- ),
- )));
- $this->assertEquals(array(
- 'foo' => 3,
- ), (array) $value);
- }
- }
|