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\Test\ConstraintValidatorTestCase;
18 class TimeValidatorTest extends ConstraintValidatorTestCase
20 protected function createValidator()
22 return new TimeValidator();
25 public function testNullIsValid()
27 $this->validator->validate(null, new Time());
29 $this->assertNoViolation();
32 public function testEmptyStringIsValid()
34 $this->validator->validate('', new Time());
36 $this->assertNoViolation();
39 public function testDateTimeClassIsValid()
41 $this->validator->validate(new \DateTime(), new Time());
43 $this->assertNoViolation();
47 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
49 public function testExpectsStringCompatibleType()
51 $this->validator->validate(new \stdClass(), new Time());
55 * @dataProvider getValidTimes
57 public function testValidTimes($time)
59 $this->validator->validate($time, new Time());
61 $this->assertNoViolation();
64 public function getValidTimes()
74 * @dataProvider getInvalidTimes
76 public function testInvalidTimes($time, $code)
78 $constraint = new Time(array(
79 'message' => 'myMessage',
82 $this->validator->validate($time, $constraint);
84 $this->buildViolation('myMessage')
85 ->setParameter('{{ value }}', '"'.$time.'"')
90 public function getInvalidTimes()
93 array('foobar', Time::INVALID_FORMAT_ERROR),
94 array('foobar 12:34:56', Time::INVALID_FORMAT_ERROR),
95 array('12:34:56 foobar', Time::INVALID_FORMAT_ERROR),
96 array('00:00', Time::INVALID_FORMAT_ERROR),
97 array('24:00:00', Time::INVALID_TIME_ERROR),
98 array('00:60:00', Time::INVALID_TIME_ERROR),
99 array('00:00:60', Time::INVALID_TIME_ERROR),
103 public function testDateTimeImmutableIsValid()
105 $this->validator->validate(new \DateTimeImmutable(), new Time());
107 $this->assertNoViolation();