Version 1
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / AbstractComparisonValidatorTestCase.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\Intl\Util\IntlTestHelper;
15 use Symfony\Component\Validator\Constraint;
16
17 class ComparisonTest_Class
18 {
19     protected $value;
20
21     public function __construct($value)
22     {
23         $this->value = $value;
24     }
25
26     public function __toString()
27     {
28         return (string) $this->value;
29     }
30 }
31
32 /**
33  * @author Daniel Holmes <daniel@danielholmes.org>
34  */
35 abstract class AbstractComparisonValidatorTestCase extends AbstractConstraintValidatorTest
36 {
37     protected static function addPhp5Dot5Comparisons(array $comparisons)
38     {
39         if (PHP_VERSION_ID < 50500) {
40             return $comparisons;
41         }
42
43         $result = $comparisons;
44
45         // Duplicate all tests involving DateTime objects to be tested with
46         // DateTimeImmutable objects as well
47         foreach ($comparisons as $comparison) {
48             $add = false;
49
50             foreach ($comparison as $i => $value) {
51                 if ($value instanceof \DateTime) {
52                     $comparison[$i] = new \DateTimeImmutable(
53                         $value->format('Y-m-d H:i:s.u e'),
54                         $value->getTimezone()
55                     );
56                     $add = true;
57                 } elseif ('DateTime' === $value) {
58                     $comparison[$i] = 'DateTimeImmutable';
59                     $add = true;
60                 }
61             }
62
63             if ($add) {
64                 $result[] = $comparison;
65             }
66         }
67
68         return $result;
69     }
70
71     /**
72      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
73      */
74     public function testThrowsConstraintExceptionIfNoValueOrProperty()
75     {
76         $comparison = $this->createConstraint(array());
77
78         $this->validator->validate('some value', $comparison);
79     }
80
81     /**
82      * @dataProvider provideAllValidComparisons
83      *
84      * @param mixed $dirtyValue
85      * @param mixed $comparisonValue
86      */
87     public function testValidComparisonToValue($dirtyValue, $comparisonValue)
88     {
89         $constraint = $this->createConstraint(array('value' => $comparisonValue));
90
91         $this->validator->validate($dirtyValue, $constraint);
92
93         $this->assertNoViolation();
94     }
95
96     /**
97      * @return array
98      */
99     public function provideAllValidComparisons()
100     {
101         // The provider runs before setUp(), so we need to manually fix
102         // the default timezone
103         $this->setDefaultTimezone('UTC');
104
105         $comparisons = self::addPhp5Dot5Comparisons($this->provideValidComparisons());
106
107         $this->restoreDefaultTimezone();
108
109         return $comparisons;
110     }
111
112     /**
113      * @return array
114      */
115     abstract public function provideValidComparisons();
116
117     /**
118      * @dataProvider provideAllInvalidComparisons
119      *
120      * @param mixed  $dirtyValue
121      * @param mixed  $dirtyValueAsString
122      * @param mixed  $comparedValue
123      * @param mixed  $comparedValueString
124      * @param string $comparedValueType
125      */
126     public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $comparedValue, $comparedValueString, $comparedValueType)
127     {
128         // Conversion of dates to string differs between ICU versions
129         // Make sure we have the correct version loaded
130         if ($dirtyValue instanceof \DateTime || $dirtyValue instanceof \DateTimeInterface) {
131             IntlTestHelper::requireIntl($this, '57.1');
132
133             if (PHP_VERSION_ID < 50304 && !(extension_loaded('intl') && method_exists('IntlDateFormatter', 'setTimeZone'))) {
134                 $this->markTestSkipped('Intl supports formatting DateTime objects since 5.3.4');
135             }
136         }
137
138         $constraint = $this->createConstraint(array('value' => $comparedValue));
139         $constraint->message = 'Constraint Message';
140
141         $this->validator->validate($dirtyValue, $constraint);
142
143         $this->buildViolation('Constraint Message')
144             ->setParameter('{{ value }}', $dirtyValueAsString)
145             ->setParameter('{{ compared_value }}', $comparedValueString)
146             ->setParameter('{{ compared_value_type }}', $comparedValueType)
147             ->setCode($this->getErrorCode())
148             ->assertRaised();
149     }
150
151     /**
152      * @return array
153      */
154     public function provideAllInvalidComparisons()
155     {
156         // The provider runs before setUp(), so we need to manually fix
157         // the default timezone
158         $this->setDefaultTimezone('UTC');
159
160         $comparisons = self::addPhp5Dot5Comparisons($this->provideInvalidComparisons());
161
162         $this->restoreDefaultTimezone();
163
164         return $comparisons;
165     }
166
167     /**
168      * @return array
169      */
170     abstract public function provideInvalidComparisons();
171
172     /**
173      * @param array $options Options for the constraint
174      *
175      * @return Constraint
176      */
177     abstract protected function createConstraint(array $options);
178
179     /**
180      * @return string|null
181      */
182     protected function getErrorCode()
183     {
184     }
185 }