From 2bf1b375c16a38a193a3aa55d1e2506aafef86f0 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Mon, 28 Jul 2014 16:32:19 +0200 Subject: [PATCH] [Validator] Fixed ExpressionValidator when the validation root is not an object --- .../Constraints/ExpressionValidator.php | 16 ++++-- .../Constraints/ExpressionValidatorTest.php | 56 +++++++++++++++++++ 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/ExpressionValidator.php b/src/Symfony/Component/Validator/Constraints/ExpressionValidator.php index 243604d066..3df23d4342 100644 --- a/src/Symfony/Component/Validator/Constraints/ExpressionValidator.php +++ b/src/Symfony/Component/Validator/Constraints/ExpressionValidator.php @@ -74,14 +74,18 @@ class ExpressionValidator extends ConstraintValidator $variables['value'] = $value; $variables['this'] = $value; } else { - // Extract the object that the property belongs to from the object - // graph - $path = new PropertyPath($this->context->getPropertyPath()); - $parentPath = $path->getParent(); $root = $this->context->getRoot(); - $variables['value'] = $value; - $variables['this'] = $parentPath ? $this->getPropertyAccessor()->getValue($root, $parentPath) : $root; + + if (is_object($root)) { + // Extract the object that the property belongs to from the object + // graph + $path = new PropertyPath($this->context->getPropertyPath()); + $parentPath = $path->getParent(); + $variables['this'] = $parentPath ? $this->getPropertyAccessor()->getValue($root, $parentPath) : $root; + } else { + $variables['this'] = null; + } } if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ExpressionValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ExpressionValidatorTest.php index b71138e5f6..ac62ce2420 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ExpressionValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ExpressionValidatorTest.php @@ -194,4 +194,60 @@ class ExpressionValidatorTest extends \PHPUnit_Framework_TestCase $this->validator->validate('2', $constraint); } + + /** + * When validatePropertyValue() is called with a class name + * https://github.com/symfony/symfony/pull/11498 + */ + public function testSucceedingExpressionAtPropertyLevelWithoutRoot() + { + $constraint = new Expression('value == "1"'); + + $this->context->expects($this->any()) + ->method('getPropertyName') + ->will($this->returnValue('property')); + + $this->context->expects($this->any()) + ->method('getPropertyPath') + ->will($this->returnValue('')); + + $this->context->expects($this->any()) + ->method('getRoot') + ->will($this->returnValue('1')); + + $this->context->expects($this->never()) + ->method('addViolation'); + + $this->validator->validate('1', $constraint); + } + + /** + * When validatePropertyValue() is called with a class name + * https://github.com/symfony/symfony/pull/11498 + */ + public function testFailingExpressionAtPropertyLevelWithoutRoot() + { + $constraint = new Expression(array( + 'expression' => 'value == "1"', + 'message' => 'myMessage', + )); + + $this->context->expects($this->any()) + ->method('getPropertyName') + ->will($this->returnValue('property')); + + $this->context->expects($this->any()) + ->method('getPropertyPath') + ->will($this->returnValue('')); + + $this->context->expects($this->any()) + ->method('getRoot') + ->will($this->returnValue('2')); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage'); + + $this->validator->validate('2', $constraint); + } }