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..906011297bfcf --- /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, self::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); + } +} 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'), ); } }