diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index 7dbb82ac4cce4..7ab8051c1a92b 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -8,6 +8,7 @@ CHANGELOG * copied the constraints `Optional` and `Required` to the `Symfony\Component\Validator\Constraints\` namespace and deprecated the original classes. + * added comparison validators (EqualTo, NotEqualTo, LessThan, LessThanOrEqualTo, GreaterThan, GreaterThanOrEqualTo, IdenticalTo, NotIdenticalTo) 2.2.0 ----- diff --git a/src/Symfony/Component/Validator/Constraints/AbstractComparison.php b/src/Symfony/Component/Validator/Constraints/AbstractComparison.php new file mode 100644 index 0000000000000..b1d9ec67fe7a6 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/AbstractComparison.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\Exception\ConstraintDefinitionException; + +/** + * Used for the comparison of values. + * + * @author Daniel Holmes + */ +abstract class AbstractComparison extends Constraint +{ + public $message; + public $value; + + /** + * {@inheritDoc} + */ + public function __construct($options = null) + { + if (!isset($options['value'])) { + throw new ConstraintDefinitionException(sprintf( + 'The %s constraint requires the "value" option to be set.', + get_class($this) + )); + } + + parent::__construct($options); + } + + /** + * {@inheritDoc} + */ + public function getDefaultOption() + { + return 'value'; + } +} diff --git a/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php b/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php new file mode 100644 index 0000000000000..929283eaebfd2 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php @@ -0,0 +1,75 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintValidator; + +/** + * Provides a base class for the validation of property comparisons. + * + * @author Daniel Holmes + */ +abstract class AbstractComparisonValidator extends ConstraintValidator +{ + /** + * {@inheritDoc} + */ + public function validate($value, Constraint $constraint) + { + if (!$this->compareValues($value, $constraint->value, $constraint)) { + $this->context->addViolation($constraint->message, array( + '{{ value }}' => $this->valueToString($constraint->value), + '{{ compared_value }}' => $this->valueToString($constraint->value), + '{{ compared_value_type }}' => $this->valueToType($constraint->value) + )); + } + } + + /** + * Returns a string representation of the type of the value. + * + * @param mixed $value + * + * @return string + */ + private function valueToType($value) + { + return is_object($value) ? get_class($value) : gettype($value); + } + + /** + * Returns a string representation of the value. + * + * @param mixed $value + * + * @return string + */ + private function valueToString($value) + { + if ($value instanceof \DateTime) { + return $value->format('Y-m-d H:i:s'); + } + + return var_export($value, true); + } + + /** + * Compares the two given values to find if their relationship is valid + * + * @param mixed $value1 The first value to compare + * @param mixed $value2 The second value to compare + * + * @return Boolean true if the relationship is valid, false otherwise + */ + abstract protected function compareValues($value1, $value2); +} diff --git a/src/Symfony/Component/Validator/Constraints/EqualTo.php b/src/Symfony/Component/Validator/Constraints/EqualTo.php new file mode 100644 index 0000000000000..3805dc71eda9a --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/EqualTo.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +/** + * @Annotation + * + * @author Daniel Holmes + */ +class EqualTo extends AbstractComparison +{ + public $message = 'This value should be equal to {{ compared_value }}'; +} diff --git a/src/Symfony/Component/Validator/Constraints/EqualToValidator.php b/src/Symfony/Component/Validator/Constraints/EqualToValidator.php new file mode 100644 index 0000000000000..2919f13633514 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/EqualToValidator.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +/** + * Validates values are equal (==). + * + * @author Daniel Holmes + */ +class EqualToValidator extends AbstractComparisonValidator +{ + /** + * @inheritDoc + */ + protected function compareValues($value1, $value2) + { + return $value1 == $value2; + } +} diff --git a/src/Symfony/Component/Validator/Constraints/GreaterThan.php b/src/Symfony/Component/Validator/Constraints/GreaterThan.php new file mode 100644 index 0000000000000..c1b02038fbb08 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/GreaterThan.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +/** + * @Annotation + * + * @author Daniel Holmes + */ +class GreaterThan extends AbstractComparison +{ + public $message = 'This value should be greater than {{ compared_value }}'; +} diff --git a/src/Symfony/Component/Validator/Constraints/GreaterThanOrEqual.php b/src/Symfony/Component/Validator/Constraints/GreaterThanOrEqual.php new file mode 100644 index 0000000000000..3af31342e1b56 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/GreaterThanOrEqual.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +/** + * @Annotation + * + * @author Daniel Holmes + */ +class GreaterThanOrEqual extends AbstractComparison +{ + public $message = 'This value should be greater than or equal to {{ compared_value }}'; +} diff --git a/src/Symfony/Component/Validator/Constraints/GreaterThanOrEqualValidator.php b/src/Symfony/Component/Validator/Constraints/GreaterThanOrEqualValidator.php new file mode 100644 index 0000000000000..f3b8210a3c398 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/GreaterThanOrEqualValidator.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +/** + * Validates values are greater than or equal to the previous (>=). + * + * @author Daniel Holmes + */ +class GreaterThanOrEqualValidator extends AbstractComparisonValidator +{ + /** + * @inheritDoc + */ + protected function compareValues($value1, $value2) + { + return $value1 >= $value2; + } +} diff --git a/src/Symfony/Component/Validator/Constraints/GreaterThanValidator.php b/src/Symfony/Component/Validator/Constraints/GreaterThanValidator.php new file mode 100644 index 0000000000000..c37ba44ddec44 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/GreaterThanValidator.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +/** + * Validates values are greater than the previous (>). + * + * @author Daniel Holmes + */ +class GreaterThanValidator extends AbstractComparisonValidator +{ + /** + * @inheritDoc + */ + protected function compareValues($value1, $value2) + { + return $value1 > $value2; + } +} diff --git a/src/Symfony/Component/Validator/Constraints/IdenticalTo.php b/src/Symfony/Component/Validator/Constraints/IdenticalTo.php new file mode 100644 index 0000000000000..7683c12f0028d --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/IdenticalTo.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +/** + * @Annotation + * + * @author Daniel Holmes + */ +class IdenticalTo extends AbstractComparison +{ + public $message = 'This value should be identical to {{ compared_value_type }} {{ compared_value }}'; +} diff --git a/src/Symfony/Component/Validator/Constraints/IdenticalToValidator.php b/src/Symfony/Component/Validator/Constraints/IdenticalToValidator.php new file mode 100644 index 0000000000000..3d979513e3d2e --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/IdenticalToValidator.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +/** + * Validates values are identical (===). + * + * @author Daniel Holmes + */ +class IdenticalToValidator extends AbstractComparisonValidator +{ + /** + * @inheritDoc + */ + protected function compareValues($value1, $value2) + { + return $value1 === $value2; + } +} diff --git a/src/Symfony/Component/Validator/Constraints/LessThan.php b/src/Symfony/Component/Validator/Constraints/LessThan.php new file mode 100644 index 0000000000000..7ec5fa47cbb9a --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/LessThan.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +/** + * @Annotation + * + * @author Daniel Holmes + */ +class LessThan extends AbstractComparison +{ + public $message = 'This value should be less than {{ compared_value }}'; +} diff --git a/src/Symfony/Component/Validator/Constraints/LessThanOrEqual.php b/src/Symfony/Component/Validator/Constraints/LessThanOrEqual.php new file mode 100644 index 0000000000000..d2c6b48585dcd --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/LessThanOrEqual.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +/** + * @Annotation + * + * @author Daniel Holmes + */ +class LessThanOrEqual extends AbstractComparison +{ + public $message = 'This value should be less than or equal to {{ compared_value }}'; +} diff --git a/src/Symfony/Component/Validator/Constraints/LessThanOrEqualValidator.php b/src/Symfony/Component/Validator/Constraints/LessThanOrEqualValidator.php new file mode 100644 index 0000000000000..46204b41094a5 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/LessThanOrEqualValidator.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +/** + * Validates values are less than or equal to the previous (<=). + * + * @author Daniel Holmes + */ +class LessThanOrEqualValidator extends AbstractComparisonValidator +{ + /** + * @inheritDoc + */ + protected function compareValues($value1, $value2) + { + return $value1 <= $value2; + } +} diff --git a/src/Symfony/Component/Validator/Constraints/LessThanValidator.php b/src/Symfony/Component/Validator/Constraints/LessThanValidator.php new file mode 100644 index 0000000000000..3d86ef366a43a --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/LessThanValidator.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +/** + * Validates values are less than the previous (<). + * + * @author Daniel Holmes + */ +class LessThanValidator extends AbstractComparisonValidator +{ + /** + * @inheritDoc + */ + protected function compareValues($value1, $value2) + { + return $value1 < $value2; + } +} diff --git a/src/Symfony/Component/Validator/Constraints/NotEqualTo.php b/src/Symfony/Component/Validator/Constraints/NotEqualTo.php new file mode 100644 index 0000000000000..6dc271a1fa233 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/NotEqualTo.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +/** + * @Annotation + * + * @author Daniel Holmes + */ +class NotEqualTo extends AbstractComparison +{ + public $message = 'This value should not be equal to {{ compared_value }}'; +} diff --git a/src/Symfony/Component/Validator/Constraints/NotEqualToValidator.php b/src/Symfony/Component/Validator/Constraints/NotEqualToValidator.php new file mode 100644 index 0000000000000..6618f31e402f3 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/NotEqualToValidator.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +/** + * Validates values are all unequal (!=). + * + * @author Daniel Holmes + */ +class NotEqualToValidator extends AbstractComparisonValidator +{ + /** + * @inheritDoc + */ + protected function compareValues($value1, $value2) + { + return $value1 != $value2; + } +} diff --git a/src/Symfony/Component/Validator/Constraints/NotIdenticalTo.php b/src/Symfony/Component/Validator/Constraints/NotIdenticalTo.php new file mode 100644 index 0000000000000..e09e8c69adea8 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/NotIdenticalTo.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +/** + * @Annotation + * + * @author Daniel Holmes + */ +class NotIdenticalTo extends AbstractComparison +{ + public $message = 'This value should not be identical to {{ compared_value_type }} {{ compared_value }}'; +} diff --git a/src/Symfony/Component/Validator/Constraints/NotIdenticalToValidator.php b/src/Symfony/Component/Validator/Constraints/NotIdenticalToValidator.php new file mode 100644 index 0000000000000..ffcb8738dfa21 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/NotIdenticalToValidator.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +/** + * Validates values aren't identical (!==). + * + * @author Daniel Holmes + */ +class NotIdenticalToValidator extends AbstractComparisonValidator +{ + /** + * @inheritDoc + */ + protected function compareValues($value1, $value2) + { + return $value1 !== $value2; + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php b/src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php new file mode 100644 index 0000000000000..d72eaf23fabd3 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php @@ -0,0 +1,108 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Constraints; + +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\Constraints\AbstractComparisonValidator; + +/** + * @author Daniel Holmes + */ +abstract class AbstractComparisonValidatorTestCase extends \PHPUnit_Framework_TestCase +{ + private $validator; + private $context; + + protected function setUp() + { + $this->validator = $this->createValidator(); + $this->context = $this->getMockBuilder('Symfony\Component\Validator\ExecutionContext') + ->disableOriginalConstructor() + ->getMock(); + $this->validator->initialize($this->context); + } + + /** + * @return AbstractComparisonValidator + */ + abstract protected function createValidator(); + + public function testThrowsConstraintExceptionIfNoValueOrProperty() + { + $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException'); + + $comparison = $this->createConstraint(array()); + $this->validator->validate('some value', $comparison); + } + + /** + * @dataProvider provideValidComparisons + * @param mixed $dirtyValue + * @param mixed $comparisonValue + */ + public function testValidComparisonToValue($dirtyValue, $comparisonValue) + { + $this->context->expects($this->never()) + ->method('addViolation'); + + $constraint = $this->createConstraint(array('value' => $comparisonValue)); + + $this->context->expects($this->any()) + ->method('getPropertyPath') + ->will($this->returnValue('property1')); + + $this->validator->validate($dirtyValue, $constraint); + } + + /** + * @return array + */ + abstract public function provideValidComparisons(); + + /** + * @dataProvider provideInvalidComparisons + * @param mixed $dirtyValue + * @param mixed $comparedValue + * @param mixed $comparedValueString + * @param string $comparedValueType + */ + public function testInvalidComparisonToValue($dirtyValue, $comparedValue, $comparedValueString, $comparedValueType) + { + $constraint = $this->createConstraint(array('value' => $comparedValue)); + $constraint->message = 'Constraint Message'; + + $this->context->expects($this->any()) + ->method('getPropertyPath') + ->will($this->returnValue('property1')); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('Constraint Message', array( + '{{ value }}' => $comparedValueString, + '{{ compared_value }}' => $comparedValueString, + '{{ compared_value_type }}' => $comparedValueType + )); + + $this->validator->validate($dirtyValue, $constraint); + } + + /** + * @return array + */ + abstract public function provideInvalidComparisons(); + + /** + * @param array $options Options for the constraint + * @return Constraint + */ + abstract protected function createConstraint(array $options); +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/EqualToValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/EqualToValidatorTest.php new file mode 100644 index 0000000000000..61189ed780b19 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/EqualToValidatorTest.php @@ -0,0 +1,57 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Constraints; + +use Symfony\Component\Validator\Constraints\EqualTo; +use Symfony\Component\Validator\Constraints\EqualToValidator; +use Symfony\Component\Validator\Tests\Constraints\AbstractComparisonValidatorTestCase; + +/** + * @author Daniel Holmes + */ +class EqualToValidatorTest extends AbstractComparisonValidatorTestCase +{ + protected function createValidator() + { + return new EqualToValidator(); + } + + protected function createConstraint(array $options) + { + return new EqualTo($options); + } + + /** + * {@inheritDoc} + */ + public function provideValidComparisons() + { + return array( + array(3, 3), + array(3, '3'), + array('a', 'a'), + array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01')) + ); + } + + /** + * {@inheritDoc} + */ + public function provideInvalidComparisons() + { + return array( + array(1, 2, '2', 'integer'), + array('22', '333', "'333'", 'string'), + array(new \DateTime('2001-01-01'), new \DateTime('2000-01-01'), '2000-01-01 00:00:00', 'DateTime') + ); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorTest.php new file mode 100644 index 0000000000000..71bcd7195492f --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorTest.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Constraints; + +use Symfony\Component\Validator\Constraints\GreaterThanOrEqual; +use Symfony\Component\Validator\Constraints\GreaterThanOrEqualValidator; + +/** + * @author Daniel Holmes + */ +class GreaterThanOrEqualValidatorTest extends AbstractComparisonValidatorTestCase +{ + protected function createValidator() + { + return new GreaterThanOrEqualValidator(); + } + + protected function createConstraint(array $options) + { + return new GreaterThanOrEqual($options); + } + + /** + * {@inheritDoc} + */ + public function provideValidComparisons() + { + return array( + array(3, 2), + array(1, 1), + array(new \DateTime('2010/01/01'), new \DateTime('2000/01/01')), + array(new \DateTime('2000/01/01'), new \DateTime('2000/01/01')), + array('a', 'a'), + array('z', 'a'), + ); + } + + /** + * {@inheritDoc} + */ + public function provideInvalidComparisons() + { + return array( + array(1, 2, '2', 'integer'), + array(new \DateTime('2000/01/01'), new \DateTime('2005/01/01'), '2005-01-01 00:00:00', 'DateTime'), + array('b', 'c', "'c'", 'string') + ); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorTest.php new file mode 100644 index 0000000000000..a838c58b7c65d --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorTest.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Constraints; + +use Symfony\Component\Validator\Constraints\GreaterThan; +use Symfony\Component\Validator\Constraints\GreaterThanValidator; + +/** + * @author Daniel Holmes + */ +class GreaterThanValidatorTest extends AbstractComparisonValidatorTestCase +{ + protected function createValidator() + { + return new GreaterThanValidator(); + } + + protected function createConstraint(array $options) + { + return new GreaterThan($options); + } + + /** + * {@inheritDoc} + */ + public function provideValidComparisons() + { + return array( + array(2, 1), + array(new \DateTime('2005/01/01'), new \DateTime('2001/01/01')), + array('333', '22') + ); + } + + /** + * {@inheritDoc} + */ + public function provideInvalidComparisons() + { + return array( + array(1, 2, '2', 'integer'), + array(2, 2, '2', 'integer'), + array(new \DateTime('2000/01/01'), new \DateTime('2005/01/01'), '2005-01-01 00:00:00', 'DateTime'), + array(new \DateTime('2000/01/01'), new \DateTime('2000/01/01'), '2000-01-01 00:00:00', 'DateTime'), + array('22', '333', "'333'", 'string'), + array('22', '22', "'22'", 'string') + ); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/IdenticalToValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/IdenticalToValidatorTest.php new file mode 100644 index 0000000000000..58fdb7e5895be --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/IdenticalToValidatorTest.php @@ -0,0 +1,60 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Constraints; + +use Symfony\Component\Validator\Constraints\IdenticalTo; +use Symfony\Component\Validator\Constraints\IdenticalToValidator; +use Symfony\Component\Validator\Tests\Constraints\AbstractComparisonValidatorTestCase; + +/** + * @author Daniel Holmes + */ +class IdenticalToValidatorTest extends AbstractComparisonValidatorTestCase +{ + protected function createValidator() + { + return new IdenticalToValidator(); + } + + protected function createConstraint(array $options) + { + return new IdenticalTo($options); + } + + /** + * {@inheritDoc} + */ + public function provideValidComparisons() + { + $date = new \DateTime('2000-01-01'); + + return array( + array(3, 3), + array('a', 'a'), + array($date, $date) + ); + } + + /** + * {@inheritDoc} + */ + public function provideInvalidComparisons() + { + return array( + array(1, 2, '2', 'integer'), + array(2, '2', "'2'", 'string'), + array('22', '333', "'333'", 'string'), + array(new \DateTime('2001-01-01'), new \DateTime('2001-01-01'), '2001-01-01 00:00:00', 'DateTime'), + array(new \DateTime('2001-01-01'), new \DateTime('1999-01-01'), '1999-01-01 00:00:00', 'DateTime') + ); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorTest.php new file mode 100644 index 0000000000000..2614905a88b5a --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorTest.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Constraints; + +use Symfony\Component\Validator\Constraints\LessThanOrEqual; +use Symfony\Component\Validator\Constraints\LessThanOrEqualValidator; + +/** + * @author Daniel Holmes + */ +class LessThanOrEqualValidatorTest extends AbstractComparisonValidatorTestCase +{ + protected function createValidator() + { + return new LessThanOrEqualValidator(); + } + + protected function createConstraint(array $options) + { + return new LessThanOrEqual($options); + } + + /** + * {@inheritDoc} + */ + public function provideValidComparisons() + { + return array( + array(1, 2), + array(1, 1), + array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01')), + array(new \DateTime('2000-01-01'), new \DateTime('2020-01-01')), + array('a', 'a'), + array('a', 'z'), + ); + } + + /** + * {@inheritDoc} + */ + public function provideInvalidComparisons() + { + return array( + array(2, 1, '1', 'integer'), + array(new \DateTime('2010-01-01'), new \DateTime('2000-01-01'), '2000-01-01 00:00:00', 'DateTime'), + array('c', 'b', "'b'", 'string') + ); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorTest.php new file mode 100644 index 0000000000000..61af9c6a2e69e --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorTest.php @@ -0,0 +1,57 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Constraints; + +use Symfony\Component\Validator\Constraints\LessThan; +use Symfony\Component\Validator\Constraints\LessThanValidator; + +/** + * @author Daniel Holmes + */ +class LessThanValidatorTest extends AbstractComparisonValidatorTestCase +{ + protected function createValidator() + { + return new LessThanValidator(); + } + + protected function createConstraint(array $options) + { + return new LessThan($options); + } + + /** + * {@inheritDoc} + */ + public function provideValidComparisons() + { + return array( + array(1, 2), + array(new \DateTime('2000-01-01'), new \DateTime('2010-01-01')), + array('22', '333') + ); + } + + /** + * {@inheritDoc} + */ + public function provideInvalidComparisons() + { + return array( + array(3, 2, '2', 'integer'), + array(2, 2, '2', 'integer'), + array(new \DateTime('2010-01-01'), new \DateTime('2000-01-01'), '2000-01-01 00:00:00', 'DateTime'), + array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01'), '2000-01-01 00:00:00', 'DateTime'), + array('333', '22', "'22'", 'string') + ); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NotEqualToValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NotEqualToValidatorTest.php new file mode 100644 index 0000000000000..d7c446a1e277e --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/NotEqualToValidatorTest.php @@ -0,0 +1,57 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Constraints; + +use Symfony\Component\Validator\Constraints\NotEqualTo; +use Symfony\Component\Validator\Constraints\NotEqualToValidator; +use Symfony\Component\Validator\Tests\Constraints\AbstractComparisonValidatorTestCase; + +/** + * @author Daniel Holmes + */ +class NotEqualToValidatorTest extends AbstractComparisonValidatorTestCase +{ + protected function createValidator() + { + return new NotEqualToValidator(); + } + + protected function createConstraint(array $options) + { + return new NotEqualTo($options); + } + + /** + * {@inheritDoc} + */ + public function provideValidComparisons() + { + return array( + array(1, 2), + array('22', '333'), + array(new \DateTime('2001-01-01'), new \DateTime('2000-01-01')) + ); + } + + /** + * {@inheritDoc} + */ + public function provideInvalidComparisons() + { + return array( + array(3, 3, '3', 'integer'), + array('2', 2, '2', 'integer'), + array('a', 'a', "'a'", 'string'), + array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01'), '2000-01-01 00:00:00', 'DateTime') + ); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/NotIdenticalToValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/NotIdenticalToValidatorTest.php new file mode 100644 index 0000000000000..48b1931d9f193 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/NotIdenticalToValidatorTest.php @@ -0,0 +1,60 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Constraints; + +use Symfony\Component\Validator\Constraints\NotIdenticalTo; +use Symfony\Component\Validator\Constraints\NotIdenticalToValidator; +use Symfony\Component\Validator\Tests\Constraints\AbstractComparisonValidatorTestCase; + +/** + * @author Daniel Holmes + */ +class NotIdenticalToValidatorTest extends AbstractComparisonValidatorTestCase +{ + protected function createValidator() + { + return new NotIdenticalToValidator(); + } + + protected function createConstraint(array $options) + { + return new NotIdenticalTo($options); + } + + /** + * {@inheritDoc} + */ + public function provideValidComparisons() + { + return array( + array(1, 2), + array('2', 2), + array('22', '333'), + array(new \DateTime('2001-01-01'), new \DateTime('2000-01-01')), + array(new \DateTime('2000-01-01'), new \DateTime('2000-01-01')) + ); + } + + /** + * {@inheritDoc} + */ + public function provideInvalidComparisons() + { + $date = new \DateTime('2000-01-01'); + + return array( + array(3, 3, '3', 'integer'), + array('a', 'a', "'a'", 'string'), + array($date, $date, '2000-01-01 00:00:00', 'DateTime') + ); + } +} diff --git a/src/Symfony/Component/Validator/Tests/ValidatorTest.php b/src/Symfony/Component/Validator/Tests/ValidatorTest.php index dbfa4f298d319..85a61e4816da8 100644 --- a/src/Symfony/Component/Validator/Tests/ValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/ValidatorTest.php @@ -212,7 +212,7 @@ public function testValidateValue() 'Failed', 'Failed', array(), - '', + 'Bernhard', '', 'Bernhard' )); diff --git a/src/Symfony/Component/Validator/Validator.php b/src/Symfony/Component/Validator/Validator.php index b8869984d8c6d..b0b80e42f4f0e 100644 --- a/src/Symfony/Component/Validator/Validator.php +++ b/src/Symfony/Component/Validator/Validator.php @@ -160,7 +160,7 @@ public function validatePropertyValue($containingValue, $property, $value, $grou */ public function validateValue($value, $constraints, $groups = null) { - $context = new ExecutionContext($this->createVisitor(null), $this->translator, $this->translationDomain); + $context = new ExecutionContext($this->createVisitor($value), $this->translator, $this->translationDomain); $constraints = is_array($constraints) ? $constraints : array($constraints);