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;
17 class ComparisonTest_Class
21 public function __construct($value)
23 $this->value = $value;
26 public function __toString()
28 return (string) $this->value;
33 * @author Daniel Holmes <daniel@danielholmes.org>
35 abstract class AbstractComparisonValidatorTestCase extends AbstractConstraintValidatorTest
37 protected static function addPhp5Dot5Comparisons(array $comparisons)
39 if (PHP_VERSION_ID < 50500) {
43 $result = $comparisons;
45 // Duplicate all tests involving DateTime objects to be tested with
46 // DateTimeImmutable objects as well
47 foreach ($comparisons as $comparison) {
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'),
57 } elseif ('DateTime' === $value) {
58 $comparison[$i] = 'DateTimeImmutable';
64 $result[] = $comparison;
72 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
74 public function testThrowsConstraintExceptionIfNoValueOrProperty()
76 $comparison = $this->createConstraint(array());
78 $this->validator->validate('some value', $comparison);
82 * @dataProvider provideAllValidComparisons
84 * @param mixed $dirtyValue
85 * @param mixed $comparisonValue
87 public function testValidComparisonToValue($dirtyValue, $comparisonValue)
89 $constraint = $this->createConstraint(array('value' => $comparisonValue));
91 $this->validator->validate($dirtyValue, $constraint);
93 $this->assertNoViolation();
99 public function provideAllValidComparisons()
101 // The provider runs before setUp(), so we need to manually fix
102 // the default timezone
103 $this->setDefaultTimezone('UTC');
105 $comparisons = self::addPhp5Dot5Comparisons($this->provideValidComparisons());
107 $this->restoreDefaultTimezone();
115 abstract public function provideValidComparisons();
118 * @dataProvider provideAllInvalidComparisons
120 * @param mixed $dirtyValue
121 * @param mixed $dirtyValueAsString
122 * @param mixed $comparedValue
123 * @param mixed $comparedValueString
124 * @param string $comparedValueType
126 public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $comparedValue, $comparedValueString, $comparedValueType)
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');
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');
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 $options Options for the constraint
177 abstract protected function createConstraint(array $options);
180 * @return string|null
182 protected function getErrorCode()