a6ca1435ed3385da8002a9b736c148965a75dfc6
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / TimeValidatorTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Tests\Constraints;
13
14 use Symfony\Component\Validator\Constraints\Time;
15 use Symfony\Component\Validator\Constraints\TimeValidator;
16 use Symfony\Component\Validator\Validation;
17
18 class TimeValidatorTest extends AbstractConstraintValidatorTest
19 {
20     protected function getApiVersion()
21     {
22         return Validation::API_VERSION_2_5;
23     }
24
25     protected function createValidator()
26     {
27         return new TimeValidator();
28     }
29
30     public function testNullIsValid()
31     {
32         $this->validator->validate(null, new Time());
33
34         $this->assertNoViolation();
35     }
36
37     public function testEmptyStringIsValid()
38     {
39         $this->validator->validate('', new Time());
40
41         $this->assertNoViolation();
42     }
43
44     public function testDateTimeClassIsValid()
45     {
46         $this->validator->validate(new \DateTime(), new Time());
47
48         $this->assertNoViolation();
49     }
50
51     /**
52      * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
53      */
54     public function testExpectsStringCompatibleType()
55     {
56         $this->validator->validate(new \stdClass(), new Time());
57     }
58
59     /**
60      * @dataProvider getValidTimes
61      */
62     public function testValidTimes($time)
63     {
64         $this->validator->validate($time, new Time());
65
66         $this->assertNoViolation();
67     }
68
69     public function getValidTimes()
70     {
71         return array(
72             array('01:02:03'),
73             array('00:00:00'),
74             array('23:59:59'),
75         );
76     }
77
78     /**
79      * @dataProvider getInvalidTimes
80      */
81     public function testInvalidTimes($time, $code)
82     {
83         $constraint = new Time(array(
84             'message' => 'myMessage',
85         ));
86
87         $this->validator->validate($time, $constraint);
88
89         $this->buildViolation('myMessage')
90             ->setParameter('{{ value }}', '"'.$time.'"')
91             ->setCode($code)
92             ->assertRaised();
93     }
94
95     public function getInvalidTimes()
96     {
97         return array(
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),
105         );
106     }
107 }