From 217aa52a0fe10addc08d4f490e2992ba9c20396d Mon Sep 17 00:00:00 2001 From: Eduardo Oliveira Date: Sun, 12 Jan 2014 15:15:04 +0000 Subject: [PATCH] [Validator] Fixed constraint violation to string handles array --- .../Validator/ConstraintViolation.php | 5 ++++- .../Tests/ConstraintViolationTest.php | 19 +++++++++++++++++++ .../Validator/Tests/ValidatorTest.php | 11 +++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/ConstraintViolation.php b/src/Symfony/Component/Validator/ConstraintViolation.php index ca8d525f67da..140fb17f4642 100644 --- a/src/Symfony/Component/Validator/ConstraintViolation.php +++ b/src/Symfony/Component/Validator/ConstraintViolation.php @@ -95,7 +95,10 @@ public function __construct($message, $messageTemplate, array $messageParameters */ public function __toString() { - $class = (string) (is_object($this->root) ? get_class($this->root) : $this->root); + $class = is_object($this->root) + ? get_class($this->root) + : (is_array($this->root) ? 'Array' : (string) $this->root); + $propertyPath = (string) $this->propertyPath; $code = $this->code; diff --git a/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php b/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php index e1f06c2428c1..6eb7f1144fb1 100644 --- a/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php +++ b/src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php @@ -33,4 +33,23 @@ public function testToStringHandlesArrays() $this->assertSame($expected, (string) $violation); } + + public function testToStringHandlesArraysRootAsValue() + { + $violation = new ConstraintViolation( + 'Array', + '{{ value }}', + array('{{ value }}' => array(1, 2, 3)), + array(), + 'property.path', + null + ); + + $expected = <<assertSame($expected, (string) $violation); + } } diff --git a/src/Symfony/Component/Validator/Tests/ValidatorTest.php b/src/Symfony/Component/Validator/Tests/ValidatorTest.php index 85a61e4816da..5ae26a1bdefe 100644 --- a/src/Symfony/Component/Validator/Tests/ValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/ValidatorTest.php @@ -22,6 +22,7 @@ use Symfony\Component\Validator\ConstraintValidatorFactory; use Symfony\Component\Validator\Constraints\Valid; use Symfony\Component\Validator\Mapping\ClassMetadata; +use Symfony\Component\Validator\Constraints\Type; class ValidatorTest extends \PHPUnit_Framework_TestCase { @@ -232,6 +233,16 @@ public function testValidateValueRejectsValid() $this->validator->validateValue($entity, new Valid()); } + public function testValidateValueViolationsToStringHandlesArrays() + { + $violations = $this->validator->validateValue( + array(), + array(new Type(array('type' => 'string'))) + ); + + $this->assertInternalType('string', (string) $violations); + } + /** * @expectedException \Symfony\Component\Validator\Exception\ValidatorException */