Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
17 use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
18
19 class ComparisonTest_Class
20 {
21     protected $value;
22
23     public function __construct($value)
24     {
25         $this->value = $value;
26     }
27
28     public function __toString()
29     {
30         return (string) $this->value;
31     }
32
33     public function getValue()
34     {
35         return $this->value;
36     }
37 }
38
39 /**
40  * @author Daniel Holmes <daniel@danielholmes.org>
41  */
42 abstract class AbstractComparisonValidatorTestCase extends ConstraintValidatorTestCase
43 {
44     protected static function addPhp5Dot5Comparisons(array $comparisons)
45     {
46         $result = $comparisons;
47
48         // Duplicate all tests involving DateTime objects to be tested with
49         // DateTimeImmutable objects as well
50         foreach ($comparisons as $comparison) {
51             $add = false;
52
53             foreach ($comparison as $i => $value) {
54                 if ($value instanceof \DateTime) {
55                     $comparison[$i] = new \DateTimeImmutable(
56                         $value->format('Y-m-d H:i:s.u e'),
57                         $value->getTimezone()
58                     );
59                     $add = true;
60                 } elseif ('DateTime' === $value) {
61                     $comparison[$i] = 'DateTimeImmutable';
62                     $add = true;
63                 }
64             }
65
66             if ($add) {
67                 $result[] = $comparison;
68             }
69         }
70
71         return $result;
72     }
73
74     public function provideInvalidConstraintOptions()
75     {
76         return array(
77             array(null),
78             array(array()),
79         );
80     }
81
82     /**
83      * @dataProvider provideInvalidConstraintOptions
84      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
85      * @expectedExceptionMessage requires either the "value" or "propertyPath" option to be set.
86      */
87     public function testThrowsConstraintExceptionIfNoValueOrPropertyPath($options)
88     {
89         $this->createConstraint($options);
90     }
91
92     /**
93      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
94      * @expectedExceptionMessage requires only one of the "value" or "propertyPath" options to be set, not both.
95      */
96     public function testThrowsConstraintExceptionIfBothValueAndPropertyPath()
97     {
98         $this->createConstraint((array(
99             'value' => 'value',
100             'propertyPath' => 'propertyPath',
101         )));
102     }
103
104     /**
105      * @dataProvider provideAllValidComparisons
106      *
107      * @param mixed $dirtyValue
108      * @param mixed $comparisonValue
109      */
110     public function testValidComparisonToValue($dirtyValue, $comparisonValue)
111     {
112         $constraint = $this->createConstraint(array('value' => $comparisonValue));
113
114         $this->validator->validate($dirtyValue, $constraint);
115
116         $this->assertNoViolation();
117     }
118
119     /**
120      * @return array
121      */
122     public function provideAllValidComparisons()
123     {
124         // The provider runs before setUp(), so we need to manually fix
125         // the default timezone
126         $this->setDefaultTimezone('UTC');
127
128         $comparisons = self::addPhp5Dot5Comparisons($this->provideValidComparisons());
129
130         $this->restoreDefaultTimezone();
131
132         return $comparisons;
133     }
134
135     /**
136      * @dataProvider provideValidComparisonsToPropertyPath
137      */
138     public function testValidComparisonToPropertyPath($comparedValue)
139     {
140         $constraint = $this->createConstraint(array('propertyPath' => 'value'));
141
142         $object = new ComparisonTest_Class(5);
143
144         $this->setObject($object);
145
146         $this->validator->validate($comparedValue, $constraint);
147
148         $this->assertNoViolation();
149     }
150
151     /**
152      * @dataProvider provideValidComparisonsToPropertyPath
153      */
154     public function testValidComparisonToPropertyPathOnArray($comparedValue)
155     {
156         $constraint = $this->createConstraint(array('propertyPath' => '[root][value]'));
157
158         $this->setObject(array('root' => array('value' => 5)));
159
160         $this->validator->validate($comparedValue, $constraint);
161
162         $this->assertNoViolation();
163     }
164
165     public function testNoViolationOnNullObjectWithPropertyPath()
166     {
167         $constraint = $this->createConstraint(array('propertyPath' => 'propertyPath'));
168
169         $this->setObject(null);
170
171         $this->validator->validate('some data', $constraint);
172
173         $this->assertNoViolation();
174     }
175
176     public function testInvalidValuePath()
177     {
178         $constraint = $this->createConstraint(array('propertyPath' => 'foo'));
179
180         if (method_exists($this, 'expectException')) {
181             $this->expectException(ConstraintDefinitionException::class);
182             $this->expectExceptionMessage(sprintf('Invalid property path "foo" provided to "%s" constraint', \get_class($constraint)));
183         } else {
184             $this->setExpectedException(ConstraintDefinitionException::class, sprintf('Invalid property path "foo" provided to "%s" constraint', \get_class($constraint)));
185         }
186
187         $object = new ComparisonTest_Class(5);
188
189         $this->setObject($object);
190
191         $this->validator->validate(5, $constraint);
192     }
193
194     /**
195      * @return array
196      */
197     abstract public function provideValidComparisons();
198
199     /**
200      * @return array
201      */
202     abstract public function provideValidComparisonsToPropertyPath();
203
204     /**
205      * @dataProvider provideAllInvalidComparisons
206      *
207      * @param mixed  $dirtyValue
208      * @param mixed  $dirtyValueAsString
209      * @param mixed  $comparedValue
210      * @param mixed  $comparedValueString
211      * @param string $comparedValueType
212      */
213     public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $comparedValue, $comparedValueString, $comparedValueType)
214     {
215         // Conversion of dates to string differs between ICU versions
216         // Make sure we have the correct version loaded
217         if ($dirtyValue instanceof \DateTime || $dirtyValue instanceof \DateTimeInterface) {
218             IntlTestHelper::requireIntl($this, '57.1');
219         }
220
221         $constraint = $this->createConstraint(array('value' => $comparedValue));
222         $constraint->message = 'Constraint Message';
223
224         $this->validator->validate($dirtyValue, $constraint);
225
226         $this->buildViolation('Constraint Message')
227             ->setParameter('{{ value }}', $dirtyValueAsString)
228             ->setParameter('{{ compared_value }}', $comparedValueString)
229             ->setParameter('{{ compared_value_type }}', $comparedValueType)
230             ->setCode($this->getErrorCode())
231             ->assertRaised();
232     }
233
234     /**
235      * @return array
236      */
237     public function provideAllInvalidComparisons()
238     {
239         // The provider runs before setUp(), so we need to manually fix
240         // the default timezone
241         $this->setDefaultTimezone('UTC');
242
243         $comparisons = self::addPhp5Dot5Comparisons($this->provideInvalidComparisons());
244
245         $this->restoreDefaultTimezone();
246
247         return $comparisons;
248     }
249
250     /**
251      * @return array
252      */
253     abstract public function provideInvalidComparisons();
254
255     /**
256      * @param array|null $options Options for the constraint
257      *
258      * @return Constraint
259      */
260     abstract protected function createConstraint(array $options = null);
261
262     /**
263      * @return string|null
264      */
265     protected function getErrorCode()
266     {
267     }
268 }