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\Validation;
18 class DateValidatorTest extends AbstractConstraintValidatorTest
20 protected function getApiVersion()
22 return Validation::API_VERSION_2_5;
25 protected function createValidator()
27 return new DateValidator();
30 public function testNullIsValid()
32 $this->validator->validate(null, new Date());
34 $this->assertNoViolation();
37 public function testEmptyStringIsValid()
39 $this->validator->validate('', new Date());
41 $this->assertNoViolation();
44 public function testDateTimeClassIsValid()
46 $this->validator->validate(new \DateTime(), new Date());
48 $this->assertNoViolation();
52 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
54 public function testExpectsStringCompatibleType()
56 $this->validator->validate(new \stdClass(), new Date());
60 * @dataProvider getValidDates
62 public function testValidDates($date)
64 $this->validator->validate($date, new Date());
66 $this->assertNoViolation();
69 public function getValidDates()
79 * @dataProvider getInvalidDates
81 public function testInvalidDates($date, $code)
83 $constraint = new Date(array(
84 'message' => 'myMessage',
87 $this->validator->validate($date, $constraint);
89 $this->buildViolation('myMessage')
90 ->setParameter('{{ value }}', '"'.$date.'"')
95 public function getInvalidDates()
98 array('foobar', Date::INVALID_FORMAT_ERROR),
99 array('foobar 2010-13-01', Date::INVALID_FORMAT_ERROR),
100 array('2010-13-01 foobar', Date::INVALID_FORMAT_ERROR),
101 array('2010-13-01', Date::INVALID_DATE_ERROR),
102 array('2010-04-32', Date::INVALID_DATE_ERROR),
103 array('2010-02-29', Date::INVALID_DATE_ERROR),