From 9b591a56455c5e5d96ecb625f447bedcd96cfa00 Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Sat, 7 Jun 2014 12:44:53 +0200 Subject: [PATCH 1/2] [Validator] Fix property path handling with nested collections in the LegacyExecutionContext. --- .../Context/LegacyExecutionContext.php | 2 +- .../Context/LegacyExecutionContextTest.php | 84 +++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/Validator/Tests/Context/LegacyExecutionContextTest.php diff --git a/src/Symfony/Component/Validator/Context/LegacyExecutionContext.php b/src/Symfony/Component/Validator/Context/LegacyExecutionContext.php index de34b1fc2cae6..b175c487c6f0a 100644 --- a/src/Symfony/Component/Validator/Context/LegacyExecutionContext.php +++ b/src/Symfony/Component/Validator/Context/LegacyExecutionContext.php @@ -140,7 +140,7 @@ public function validateValue($value, $constraints, $subPath = '', $groups = nul { return $this ->getValidator() - ->inContext($this) + ->inContext(clone $this) ->atPath($subPath) ->validate($value, $constraints, $groups) ; diff --git a/src/Symfony/Component/Validator/Tests/Context/LegacyExecutionContextTest.php b/src/Symfony/Component/Validator/Tests/Context/LegacyExecutionContextTest.php new file mode 100644 index 0000000000000..24861b1ad786a --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Context/LegacyExecutionContextTest.php @@ -0,0 +1,84 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Context; + +use Symfony\Component\Validator\Constraints\Collection; +use Symfony\Component\Validator\Constraints\All; +use Symfony\Component\Validator\ConstraintValidatorFactory; +use Symfony\Component\Validator\Context\ExecutionContextFactory; +use Symfony\Component\Validator\Context\LegacyExecutionContext; +use Symfony\Component\Validator\Tests\Fixtures\ConstraintA; +use Symfony\Component\Validator\Validator\RecursiveValidator; + +class LegacyExecutionContextTest extends \PHPUnit_Framework_TestCase +{ + const TRANS_DOMAIN = 'trans_domain'; + + private $metadataFactory; + private $translator; + private $validator; + + protected function setUp() + { + $this->metadataFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface'); + $this->translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface'); + + $validatorFactory = new ConstraintValidatorFactory(); + $executionContextFactory = new ExecutionContextFactory($this->translator, 'trans_domain'); + $this->validator = new RecursiveValidator($executionContextFactory, $this->metadataFactory, $validatorFactory); + } + + public function testGetPropertyPathWithNestedCollectionsAndAllMixed() + { + $constraints = new Collection(array( + 'shelves' => new All(array('constraints' => array( + new Collection(array( + 'name' => new ConstraintA(), + 'books' => new All(array('constraints' => array( + new ConstraintA() + ))) + )) + ))), + 'name' => new ConstraintA() + )); + $data = array( + 'shelves' => array( + array( + 'name' => 'Research', + 'books' => array('foo', 'bar'), + ), + array( + 'name' => 'VALID', + 'books' => array('foozy', 'VALID', 'bazzy'), + ), + ), + 'name' => 'Library', + ); + $expectedViolationPaths = array( + '[shelves][0][name]', + '[shelves][0][books][0]', + '[shelves][0][books][1]', + '[shelves][1][books][0]', + '[shelves][1][books][2]', + '[name]' + ); + + $context = new LegacyExecutionContext($this->validator, 'Root', $this->metadataFactory, $this->translator, self::TRANS_DOMAIN); + $context->validateValue($data, $constraints); + + foreach ($context->getViolations() as $violation) { + $violationPaths[] = $violation->getPropertyPath(); + } + + $this->assertEquals($expectedViolationPaths, $violationPaths); + } +} From 2d2c6fcda5de723e85509fce9864fbfffaa7255e Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Sat, 7 Jun 2014 15:06:49 +0200 Subject: [PATCH 2/2] Address typos and cs issues. --- .../Context/LegacyExecutionContextTest.php | 18 +++++++++--------- .../Validator/Tests/Util/PropertyPathTest.php | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Validator/Tests/Context/LegacyExecutionContextTest.php b/src/Symfony/Component/Validator/Tests/Context/LegacyExecutionContextTest.php index 24861b1ad786a..906011297bfcf 100644 --- a/src/Symfony/Component/Validator/Tests/Context/LegacyExecutionContextTest.php +++ b/src/Symfony/Component/Validator/Tests/Context/LegacyExecutionContextTest.php @@ -33,7 +33,7 @@ protected function setUp() $this->translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface'); $validatorFactory = new ConstraintValidatorFactory(); - $executionContextFactory = new ExecutionContextFactory($this->translator, 'trans_domain'); + $executionContextFactory = new ExecutionContextFactory($this->translator, self::TRANS_DOMAIN); $this->validator = new RecursiveValidator($executionContextFactory, $this->metadataFactory, $validatorFactory); } @@ -41,13 +41,13 @@ public function testGetPropertyPathWithNestedCollectionsAndAllMixed() { $constraints = new Collection(array( 'shelves' => new All(array('constraints' => array( - new Collection(array( - 'name' => new ConstraintA(), - 'books' => new All(array('constraints' => array( - new ConstraintA() - ))) - )) - ))), + new Collection(array( + 'name' => new ConstraintA(), + 'books' => new All(array('constraints' => array( + new ConstraintA() + ))) + )) + ))), 'name' => new ConstraintA() )); $data = array( @@ -69,7 +69,7 @@ public function testGetPropertyPathWithNestedCollectionsAndAllMixed() '[shelves][0][books][1]', '[shelves][1][books][0]', '[shelves][1][books][2]', - '[name]' + '[name]', ); $context = new LegacyExecutionContext($this->validator, 'Root', $this->metadataFactory, $this->translator, self::TRANS_DOMAIN); diff --git a/src/Symfony/Component/Validator/Tests/Util/PropertyPathTest.php b/src/Symfony/Component/Validator/Tests/Util/PropertyPathTest.php index fcb202e0e065d..dfd83821ad379 100644 --- a/src/Symfony/Component/Validator/Tests/Util/PropertyPathTest.php +++ b/src/Symfony/Component/Validator/Tests/Util/PropertyPathTest.php @@ -28,8 +28,8 @@ public function provideAppendPaths() return array( array('foo', '', 'foo', 'It returns the basePath if subPath is empty'), array('', 'bar', 'bar', 'It returns the subPath if basePath is empty'), - array('foo', 'bar', 'foo.bar', 'It append the subPath to the basePath'), - array('foo', '[bar]', 'foo[bar]', 'It does not include the dot separator if subPath uses the array notation') + array('foo', 'bar', 'foo.bar', 'It appends the subPath to the basePath'), + array('foo', '[bar]', 'foo[bar]', 'It does not include the dot separator if subPath uses an array notation'), ); } }