Security update for Core, with self-updated composer
[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\Test\ConstraintValidatorTestCase;
17
18 class ComparisonTest_Class
19 {
20     protected $value;
21
22     public function __construct($value)
23     {
24         $this->value = $value;
25     }
26
27     public function __toString()
28     {
29         return (string) $this->value;
30     }
31 }
32
33 /**
34  * @author Daniel Holmes <daniel@danielholmes.org>
35  */
36 abstract class AbstractComparisonValidatorTestCase extends ConstraintValidatorTestCase
37 {
38     protected static function addPhp5Dot5Comparisons(array $comparisons)
39     {
40         $result = $comparisons;
41
42         // Duplicate all tests involving DateTime objects to be tested with
43         // DateTimeImmutable objects as well
44         foreach ($comparisons as $comparison) {
45             $add = false;
46
47             foreach ($comparison as $i => $value) {
48                 if ($value instanceof \DateTime) {
49                     $comparison[$i] = new \DateTimeImmutable(
50                         $value->format('Y-m-d H:i:s.u e'),
51                         $value->getTimezone()
52                     );
53                     $add = true;
54                 } elseif ('DateTime' === $value) {
55                     $comparison[$i] = 'DateTimeImmutable';
56                     $add = true;
57                 }
58             }
59
60             if ($add) {
61                 $result[] = $comparison;
62             }
63         }
64
65         return $result;
66     }
67
68     public function provideInvalidConstraintOptions()
69     {
70         return array(
71             array(null),
72             array(array()),
73         );
74     }
75
76     /**
77      * @dataProvider provideInvalidConstraintOptions
78      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
79      */
80     public function testThrowsConstraintExceptionIfNoValueOrProperty($options)
81     {
82         $this->createConstraint($options);
83     }
84
85     /**
86      * @dataProvider provideAllValidComparisons
87      *
88      * @param mixed $dirtyValue
89      * @param mixed $comparisonValue
90      */
91     public function testValidComparisonToValue($dirtyValue, $comparisonValue)
92     {
93         $constraint = $this->createConstraint(array('value' => $comparisonValue));
94
95         $this->validator->validate($dirtyValue, $constraint);
96
97         $this->assertNoViolation();
98     }
99
100     /**
101      * @return array
102      */
103     public function provideAllValidComparisons()
104     {
105         // The provider runs before setUp(), so we need to manually fix
106         // the default timezone
107         $this->setDefaultTimezone('UTC');
108
109         $comparisons = self::addPhp5Dot5Comparisons($this->provideValidComparisons());
110
111         $this->restoreDefaultTimezone();
112
113         return $comparisons;
114     }
115
116     /**
117      * @return array
118      */
119     abstract public function provideValidComparisons();
120
121     /**
122      * @dataProvider provideAllInvalidComparisons
123      *
124      * @param mixed  $dirtyValue
125      * @param mixed  $dirtyValueAsString
126      * @param mixed  $comparedValue
127      * @param mixed  $comparedValueString
128      * @param string $comparedValueType
129      */
130     public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $comparedValue, $comparedValueString, $comparedValueType)
131     {
132         // Conversion of dates to string differs between ICU versions
133         // Make sure we have the correct version loaded
134         if ($dirtyValue instanceof \DateTime || $dirtyValue instanceof \DateTimeInterface) {
135             IntlTestHelper::requireIntl($this, '57.1');
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|null $options Options for the constraint
174      *
175      * @return Constraint
176      */
177     abstract protected function createConstraint(array $options = null);
178
179     /**
180      * @return string|null
181      */
182     protected function getErrorCode()
183     {
184     }
185 }