4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Tests\Constraints;
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;
19 class ComparisonTest_Class
23 public function __construct($value)
25 $this->value = $value;
28 public function __toString()
30 return (string) $this->value;
33 public function getValue()
40 * @author Daniel Holmes <daniel@danielholmes.org>
42 abstract class AbstractComparisonValidatorTestCase extends ConstraintValidatorTestCase
44 protected static function addPhp5Dot5Comparisons(array $comparisons)
46 $result = $comparisons;
48 // Duplicate all tests involving DateTime objects to be tested with
49 // DateTimeImmutable objects as well
50 foreach ($comparisons as $comparison) {
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'),
60 } elseif ('DateTime' === $value) {
61 $comparison[$i] = 'DateTimeImmutable';
67 $result[] = $comparison;
74 public function provideInvalidConstraintOptions()
83 * @dataProvider provideInvalidConstraintOptions
84 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
85 * @expectedExceptionMessage requires either the "value" or "propertyPath" option to be set.
87 public function testThrowsConstraintExceptionIfNoValueOrPropertyPath($options)
89 $this->createConstraint($options);
93 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
94 * @expectedExceptionMessage requires only one of the "value" or "propertyPath" options to be set, not both.
96 public function testThrowsConstraintExceptionIfBothValueAndPropertyPath()
98 $this->createConstraint((array(
100 'propertyPath' => 'propertyPath',
105 * @dataProvider provideAllValidComparisons
107 * @param mixed $dirtyValue
108 * @param mixed $comparisonValue
110 public function testValidComparisonToValue($dirtyValue, $comparisonValue)
112 $constraint = $this->createConstraint(array('value' => $comparisonValue));
114 $this->validator->validate($dirtyValue, $constraint);
116 $this->assertNoViolation();
122 public function provideAllValidComparisons()
124 // The provider runs before setUp(), so we need to manually fix
125 // the default timezone
126 $this->setDefaultTimezone('UTC');
128 $comparisons = self::addPhp5Dot5Comparisons($this->provideValidComparisons());
130 $this->restoreDefaultTimezone();
136 * @dataProvider provideValidComparisonsToPropertyPath
138 public function testValidComparisonToPropertyPath($comparedValue)
140 $constraint = $this->createConstraint(array('propertyPath' => 'value'));
142 $object = new ComparisonTest_Class(5);
144 $this->setObject($object);
146 $this->validator->validate($comparedValue, $constraint);
148 $this->assertNoViolation();
152 * @dataProvider provideValidComparisonsToPropertyPath
154 public function testValidComparisonToPropertyPathOnArray($comparedValue)
156 $constraint = $this->createConstraint(array('propertyPath' => '[root][value]'));
158 $this->setObject(array('root' => array('value' => 5)));
160 $this->validator->validate($comparedValue, $constraint);
162 $this->assertNoViolation();
165 public function testNoViolationOnNullObjectWithPropertyPath()
167 $constraint = $this->createConstraint(array('propertyPath' => 'propertyPath'));
169 $this->setObject(null);
171 $this->validator->validate('some data', $constraint);
173 $this->assertNoViolation();
176 public function testInvalidValuePath()
178 $constraint = $this->createConstraint(array('propertyPath' => 'foo'));
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)));
184 $this->setExpectedException(ConstraintDefinitionException::class, sprintf('Invalid property path "foo" provided to "%s" constraint', \get_class($constraint)));
187 $object = new ComparisonTest_Class(5);
189 $this->setObject($object);
191 $this->validator->validate(5, $constraint);
197 abstract public function provideValidComparisons();
202 abstract public function provideValidComparisonsToPropertyPath();
205 * @dataProvider provideAllInvalidComparisons
207 * @param mixed $dirtyValue
208 * @param mixed $dirtyValueAsString
209 * @param mixed $comparedValue
210 * @param mixed $comparedValueString
211 * @param string $comparedValueType
213 public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $comparedValue, $comparedValueString, $comparedValueType)
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');
221 $constraint = $this->createConstraint(array('value' => $comparedValue));
222 $constraint->message = 'Constraint Message';
224 $this->validator->validate($dirtyValue, $constraint);
226 $this->buildViolation('Constraint Message')
227 ->setParameter('{{ value }}', $dirtyValueAsString)
228 ->setParameter('{{ compared_value }}', $comparedValueString)
229 ->setParameter('{{ compared_value_type }}', $comparedValueType)
230 ->setCode($this->getErrorCode())
237 public function provideAllInvalidComparisons()
239 // The provider runs before setUp(), so we need to manually fix
240 // the default timezone
241 $this->setDefaultTimezone('UTC');
243 $comparisons = self::addPhp5Dot5Comparisons($this->provideInvalidComparisons());
245 $this->restoreDefaultTimezone();
253 abstract public function provideInvalidComparisons();
256 * @param array|null $options Options for the constraint
260 abstract protected function createConstraint(array $options = null);
263 * @return string|null
265 protected function getErrorCode()