21f0a2dcc3d605b491c19265bc5a15f04a05c540
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / DateValidatorTest.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\Date;
15 use Symfony\Component\Validator\Constraints\DateValidator;
16 use Symfony\Component\Validator\Validation;
17
18 class DateValidatorTest 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 DateValidator();
28     }
29
30     public function testNullIsValid()
31     {
32         $this->validator->validate(null, new Date());
33
34         $this->assertNoViolation();
35     }
36
37     public function testEmptyStringIsValid()
38     {
39         $this->validator->validate('', new Date());
40
41         $this->assertNoViolation();
42     }
43
44     public function testDateTimeClassIsValid()
45     {
46         $this->validator->validate(new \DateTime(), new Date());
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 Date());
57     }
58
59     /**
60      * @dataProvider getValidDates
61      */
62     public function testValidDates($date)
63     {
64         $this->validator->validate($date, new Date());
65
66         $this->assertNoViolation();
67     }
68
69     public function getValidDates()
70     {
71         return array(
72             array('2010-01-01'),
73             array('1955-12-12'),
74             array('2030-05-31'),
75         );
76     }
77
78     /**
79      * @dataProvider getInvalidDates
80      */
81     public function testInvalidDates($date, $code)
82     {
83         $constraint = new Date(array(
84             'message' => 'myMessage',
85         ));
86
87         $this->validator->validate($date, $constraint);
88
89         $this->buildViolation('myMessage')
90             ->setParameter('{{ value }}', '"'.$date.'"')
91             ->setCode($code)
92             ->assertRaised();
93     }
94
95     public function getInvalidDates()
96     {
97         return array(
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),
104         );
105     }
106 }