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\Date;
15 use Symfony\Component\Validator\Constraints\DateValidator;
16 use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
18 class DateValidatorTest extends ConstraintValidatorTestCase
20 protected function createValidator()
22 return new DateValidator();
25 public function testNullIsValid()
27 $this->validator->validate(null, new Date());
29 $this->assertNoViolation();
32 public function testEmptyStringIsValid()
34 $this->validator->validate('', new Date());
36 $this->assertNoViolation();
39 public function testDateTimeClassIsValid()
41 $this->validator->validate(new \DateTime(), new Date());
43 $this->assertNoViolation();
46 public function testDateTimeImmutableClassIsValid()
48 $this->validator->validate(new \DateTimeImmutable(), new Date());
50 $this->assertNoViolation();
54 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
56 public function testExpectsStringCompatibleType()
58 $this->validator->validate(new \stdClass(), new Date());
62 * @dataProvider getValidDates
64 public function testValidDates($date)
66 $this->validator->validate($date, new Date());
68 $this->assertNoViolation();
71 public function getValidDates()
81 * @dataProvider getInvalidDates
83 public function testInvalidDates($date, $code)
85 $constraint = new Date(array(
86 'message' => 'myMessage',
89 $this->validator->validate($date, $constraint);
91 $this->buildViolation('myMessage')
92 ->setParameter('{{ value }}', '"'.$date.'"')
97 public function getInvalidDates()
100 array('foobar', Date::INVALID_FORMAT_ERROR),
101 array('foobar 2010-13-01', Date::INVALID_FORMAT_ERROR),
102 array('2010-13-01 foobar', Date::INVALID_FORMAT_ERROR),
103 array('2010-13-01', Date::INVALID_DATE_ERROR),
104 array('2010-04-32', Date::INVALID_DATE_ERROR),
105 array('2010-02-29', Date::INVALID_DATE_ERROR),