From 9ca0c495b295758e73e8a741361a1dc74d5126a1 Mon Sep 17 00:00:00 2001 From: Olivier Laviale Date: Sun, 25 Feb 2018 19:06:15 +0100 Subject: [PATCH] wip --- .../Validator/Tests/Fixtures/Timer.php | 13 +++++++++ .../Tests/Fixtures/TimerCollection.php | 27 +++++++++++++++++++ .../Tests/LegacyExecutionContextTest.php | 25 +++++++++++++++++ .../RecursiveContextualValidator.php | 5 ++-- 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/Validator/Tests/Fixtures/Timer.php create mode 100644 src/Symfony/Component/Validator/Tests/Fixtures/TimerCollection.php diff --git a/src/Symfony/Component/Validator/Tests/Fixtures/Timer.php b/src/Symfony/Component/Validator/Tests/Fixtures/Timer.php new file mode 100644 index 0000000000000..480afbc1de385 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Fixtures/Timer.php @@ -0,0 +1,13 @@ +duration = $duration; + } +} diff --git a/src/Symfony/Component/Validator/Tests/Fixtures/TimerCollection.php b/src/Symfony/Component/Validator/Tests/Fixtures/TimerCollection.php new file mode 100644 index 0000000000000..e3180d220b37b --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Fixtures/TimerCollection.php @@ -0,0 +1,27 @@ +timers = $timers; + } + + /** + * {@inheritdoc} + */ + public function getIterator() + { + return new \ArrayIterator($this->timers); + } +} diff --git a/src/Symfony/Component/Validator/Tests/LegacyExecutionContextTest.php b/src/Symfony/Component/Validator/Tests/LegacyExecutionContextTest.php index a6737cf5b9d61..3c245ce7e8726 100644 --- a/src/Symfony/Component/Validator/Tests/LegacyExecutionContextTest.php +++ b/src/Symfony/Component/Validator/Tests/LegacyExecutionContextTest.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Tests; +use Symfony\Component\Validator\Constraints\NotBlank; use PHPUnit\Framework\TestCase; use Symfony\Component\Validator\Constraints\Collection; use Symfony\Component\Validator\Constraints\All; @@ -19,6 +20,9 @@ use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\ExecutionContext; use Symfony\Component\Validator\Tests\Fixtures\ConstraintA; +use Symfony\Component\Validator\Tests\Fixtures\Timer; +use Symfony\Component\Validator\Tests\Fixtures\TimerCollection; +use Symfony\Component\Validator\Validation; use Symfony\Component\Validator\ValidationVisitor; /** @@ -327,6 +331,27 @@ public function testGetPropertyPathWithNestedCollectionsAndAllMixed() $this->assertEquals($expectedViolationPaths, $violationPaths); } + + public function testGetPropertyPathWithNestedCollectionsAndAllMixedWithIteratorAggregate() + { + $data = new TimerCollection(array( + new Timer(null), + new Timer('PT10M'), + new Timer(null), + )); + + $validator = Validation::createValidatorBuilder()->getValidator(); + $metadata = $validator->getMetadataFor('Symfony\Component\Validator\Tests\Fixtures\Timer'); + $metadata->addPropertyConstraint('duration', new NotBlank()); + + $violationPaths = array(); + + foreach ($validator->validate($data) as $violation) { + $violationPaths[] = $violation->getPropertyPath(); + } + + $this->assertSame(array('[0].duration', '[2].duration'), $violationPaths); + } } class ExecutionContextTest_TestClass diff --git a/src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php b/src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php index a9e9f7f0e8a12..5cfefd74c750c 100644 --- a/src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php +++ b/src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php @@ -748,9 +748,10 @@ private function validateGenericNode($value, $object, $cacheKey, MetadataInterfa // See validateClassNode() $cascadedGroups = null !== $cascadedGroups && count($cascadedGroups) > 0 ? $cascadedGroups : $groups; +// if (is_array($value) || $value instanceof \IteratorAggregate) { if (is_array($value)) { - // Arrays are always traversed, independent of the specified - // traversal strategy + // Arrays and instance of IteratorAggregate are always traversed, independent of the + // specified traversal strategy // (BC with Symfony < 2.5) $this->validateEachObjectIn( $value,