From 767dc23a47382cafe737e1e3a191f0eff6b70206 Mon Sep 17 00:00:00 2001 From: rchoquet Date: Fri, 2 Jun 2017 18:32:03 +0200 Subject: [PATCH] [Validator] Removes exception when validating non-traversable with Composite constraints --- .../Component/Validator/Constraints/All.php | 7 +++++++ .../Validator/Constraints/AllValidator.php | 6 +++++- .../Validator/Constraints/Collection.php | 3 +++ .../Constraints/CollectionValidator.php | 6 +++++- .../Tests/Constraints/AllValidatorTest.php | 14 +++++++++----- .../Constraints/CollectionValidatorTest.php | 18 +++++++++++------- 6 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/All.php b/src/Symfony/Component/Validator/Constraints/All.php index b531a1d645af3..a37a9b30de77b 100644 --- a/src/Symfony/Component/Validator/Constraints/All.php +++ b/src/Symfony/Component/Validator/Constraints/All.php @@ -19,7 +19,14 @@ */ class All extends Composite { + const WRONG_TYPE_ERROR = '824268b5-91c0-4730-983f-896fb0f971f0'; + + protected static $errorNames = array( + self::WRONG_TYPE_ERROR => 'WRONG_TYPE_ERROR', + ); + public $constraints = array(); + public $wrongTypeMessage = 'This value should be an array.'; public function getDefaultOption() { diff --git a/src/Symfony/Component/Validator/Constraints/AllValidator.php b/src/Symfony/Component/Validator/Constraints/AllValidator.php index 94ff8190e47cb..58cf905ce6377 100644 --- a/src/Symfony/Component/Validator/Constraints/AllValidator.php +++ b/src/Symfony/Component/Validator/Constraints/AllValidator.php @@ -35,7 +35,11 @@ public function validate($value, Constraint $constraint) } if (!is_array($value) && !$value instanceof \Traversable) { - throw new UnexpectedTypeException($value, 'array or Traversable'); + $this->context->buildViolation($constraint->wrongTypeMessage) + ->setCode(All::WRONG_TYPE_ERROR) + ->addViolation(); + + return; } $context = $this->context; diff --git a/src/Symfony/Component/Validator/Constraints/Collection.php b/src/Symfony/Component/Validator/Constraints/Collection.php index ac1edd3b59272..25db186a6e557 100644 --- a/src/Symfony/Component/Validator/Constraints/Collection.php +++ b/src/Symfony/Component/Validator/Constraints/Collection.php @@ -23,10 +23,12 @@ class Collection extends Composite { const MISSING_FIELD_ERROR = '2fa2158c-2a7f-484b-98aa-975522539ff8'; const NO_SUCH_FIELD_ERROR = '7703c766-b5d5-4cef-ace7-ae0dd82304e9'; + const WRONG_TYPE_ERROR = '212f9e68-bff4-404f-ab6d-9f29055139e2'; protected static $errorNames = array( self::MISSING_FIELD_ERROR => 'MISSING_FIELD_ERROR', self::NO_SUCH_FIELD_ERROR => 'NO_SUCH_FIELD_ERROR', + self::WRONG_TYPE_ERROR => 'WRONG_TYPE_ERROR', ); public $fields = array(); @@ -34,6 +36,7 @@ class Collection extends Composite public $allowMissingFields = false; public $extraFieldsMessage = 'This field was not expected.'; public $missingFieldsMessage = 'This field is missing.'; + public $wrongTypeMessage = 'This value should be an array.'; /** * {@inheritdoc} diff --git a/src/Symfony/Component/Validator/Constraints/CollectionValidator.php b/src/Symfony/Component/Validator/Constraints/CollectionValidator.php index 737e880968aa0..282d7ca996f86 100644 --- a/src/Symfony/Component/Validator/Constraints/CollectionValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CollectionValidator.php @@ -35,7 +35,11 @@ public function validate($value, Constraint $constraint) } if (!is_array($value) && !($value instanceof \Traversable && $value instanceof \ArrayAccess)) { - throw new UnexpectedTypeException($value, 'array or Traversable and ArrayAccess'); + $this->context->buildViolation($constraint->wrongTypeMessage) + ->setCode(Collection::WRONG_TYPE_ERROR) + ->addViolation(); + + return; } // We need to keep the initialized context when CollectionValidator diff --git a/src/Symfony/Component/Validator/Tests/Constraints/AllValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/AllValidatorTest.php index 57dd6006974b0..98f738b888a0b 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/AllValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/AllValidatorTest.php @@ -36,12 +36,16 @@ public function testNullIsValid() $this->assertNoViolation(); } - /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException - */ - public function testThrowsExceptionIfNotTraversable() + public function testAddViolationIfNotTraversable() { - $this->validator->validate('foo.barbar', new All(new Range(array('min' => 4)))); + $this->validator->validate('foo.barbar', new All(array( + 'constraints' => array(new Range(array('min' => 4))), + 'wrongTypeMessage' => 'myMessage', + ))); + + $this->buildViolation('myMessage') + ->setCode(All::WRONG_TYPE_ERROR) + ->assertRaised(); } /** diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php index 0376814341fb8..719b1dbdc4b94 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php @@ -57,14 +57,18 @@ public function testFieldsAsDefaultOption() $this->assertNoViolation(); } - /** - * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException - */ - public function testThrowsExceptionIfNotTraversable() + public function testAddViolationIfNotTraversable() { - $this->validator->validate('foobar', new Collection(array('fields' => array( - 'foo' => new Range(array('min' => 4)), - )))); + $this->validator->validate('foobar', new Collection(array( + 'fields' => array( + 'foo' => new Range(array('min' => 4)), + ), + 'wrongTypeMessage' => 'myMessage', + ))); + + $this->buildViolation('myMessage') + ->setCode(Collection::WRONG_TYPE_ERROR) + ->assertRaised(); } public function testWalkSingleConstraint()