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\Test\ConstraintValidatorTestCase;
18 class ComparisonTest_Class
22 public function __construct($value)
24 $this->value = $value;
27 public function __toString()
29 return (string) $this->value;
34 * @author Daniel Holmes <daniel@danielholmes.org>
36 abstract class AbstractComparisonValidatorTestCase extends ConstraintValidatorTestCase
38 protected static function addPhp5Dot5Comparisons(array $comparisons)
40 $result = $comparisons;
42 // Duplicate all tests involving DateTime objects to be tested with
43 // DateTimeImmutable objects as well
44 foreach ($comparisons as $comparison) {
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'),
54 } elseif ('DateTime' === $value) {
55 $comparison[$i] = 'DateTimeImmutable';
61 $result[] = $comparison;
68 public function provideInvalidConstraintOptions()
77 * @dataProvider provideInvalidConstraintOptions
78 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
80 public function testThrowsConstraintExceptionIfNoValueOrProperty($options)
82 $this->createConstraint($options);
86 * @dataProvider provideAllValidComparisons
88 * @param mixed $dirtyValue
89 * @param mixed $comparisonValue
91 public function testValidComparisonToValue($dirtyValue, $comparisonValue)
93 $constraint = $this->createConstraint(array('value' => $comparisonValue));
95 $this->validator->validate($dirtyValue, $constraint);
97 $this->assertNoViolation();
103 public function provideAllValidComparisons()
105 // The provider runs before setUp(), so we need to manually fix
106 // the default timezone
107 $this->setDefaultTimezone('UTC');
109 $comparisons = self::addPhp5Dot5Comparisons($this->provideValidComparisons());
111 $this->restoreDefaultTimezone();
119 abstract public function provideValidComparisons();
122 * @dataProvider provideAllInvalidComparisons
124 * @param mixed $dirtyValue
125 * @param mixed $dirtyValueAsString
126 * @param mixed $comparedValue
127 * @param mixed $comparedValueString
128 * @param string $comparedValueType
130 public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $comparedValue, $comparedValueString, $comparedValueType)
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');
138 $constraint = $this->createConstraint(array('value' => $comparedValue));
139 $constraint->message = 'Constraint Message';
141 $this->validator->validate($dirtyValue, $constraint);
143 $this->buildViolation('Constraint Message')
144 ->setParameter('{{ value }}', $dirtyValueAsString)
145 ->setParameter('{{ compared_value }}', $comparedValueString)
146 ->setParameter('{{ compared_value_type }}', $comparedValueType)
147 ->setCode($this->getErrorCode())
154 public function provideAllInvalidComparisons()
156 // The provider runs before setUp(), so we need to manually fix
157 // the default timezone
158 $this->setDefaultTimezone('UTC');
160 $comparisons = self::addPhp5Dot5Comparisons($this->provideInvalidComparisons());
162 $this->restoreDefaultTimezone();
170 abstract public function provideInvalidComparisons();
173 * @param array|null $options Options for the constraint
177 abstract protected function createConstraint(array $options = null);
180 * @return string|null
182 protected function getErrorCode()