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\Time;
15 use Symfony\Component\Validator\Constraints\TimeValidator;
16 use Symfony\Component\Validator\Validation;
18 class TimeValidatorTest extends AbstractConstraintValidatorTest
20 protected function getApiVersion()
22 return Validation::API_VERSION_2_5;
25 protected function createValidator()
27 return new TimeValidator();
30 public function testNullIsValid()
32 $this->validator->validate(null, new Time());
34 $this->assertNoViolation();
37 public function testEmptyStringIsValid()
39 $this->validator->validate('', new Time());
41 $this->assertNoViolation();
44 public function testDateTimeClassIsValid()
46 $this->validator->validate(new \DateTime(), new Time());
48 $this->assertNoViolation();
52 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
54 public function testExpectsStringCompatibleType()
56 $this->validator->validate(new \stdClass(), new Time());
60 * @dataProvider getValidTimes
62 public function testValidTimes($time)
64 $this->validator->validate($time, new Time());
66 $this->assertNoViolation();
69 public function getValidTimes()
79 * @dataProvider getInvalidTimes
81 public function testInvalidTimes($time, $code)
83 $constraint = new Time(array(
84 'message' => 'myMessage',
87 $this->validator->validate($time, $constraint);
89 $this->buildViolation('myMessage')
90 ->setParameter('{{ value }}', '"'.$time.'"')
95 public function getInvalidTimes()
98 array('foobar', Time::INVALID_FORMAT_ERROR),
99 array('foobar 12:34:56', Time::INVALID_FORMAT_ERROR),
100 array('12:34:56 foobar', Time::INVALID_FORMAT_ERROR),
101 array('00:00', Time::INVALID_FORMAT_ERROR),
102 array('24:00:00', Time::INVALID_TIME_ERROR),
103 array('00:60:00', Time::INVALID_TIME_ERROR),
104 array('00:00:60', Time::INVALID_TIME_ERROR),