[Validator] fixed duplicate constraints with parent class interfaces

This fixes https://github.com/symfony/symfony/issues/19516
This commit is contained in:
David Maicher 2016-08-10 21:49:44 +02:00
parent 5e601b95a2
commit fb36c5a575
4 changed files with 27 additions and 16 deletions

View File

@ -116,9 +116,9 @@ class LazyLoadingMetadataFactory implements MetadataFactoryInterface
$metadata->mergeConstraints($this->getMetadataFor($parent->name)); $metadata->mergeConstraints($this->getMetadataFor($parent->name));
} }
// Include constraints from all implemented interfaces // Include constraints from all implemented interfaces that have not been processed via parent class yet
foreach ($metadata->getReflectionClass()->getInterfaces() as $interface) { foreach ($metadata->getReflectionClass()->getInterfaces() as $interface) {
if ('Symfony\Component\Validator\GroupSequenceProviderInterface' === $interface->name) { if ('Symfony\Component\Validator\GroupSequenceProviderInterface' === $interface->name || ($parent && $parent->implementsInterface($interface->name))) {
continue; continue;
} }
$metadata->mergeConstraints($this->getMetadataFor($interface->name)); $metadata->mergeConstraints($this->getMetadataFor($interface->name));

View File

@ -19,7 +19,7 @@ use Symfony\Component\Validator\ExecutionContextInterface;
* @Assert\GroupSequence({"Foo", "Entity"}) * @Assert\GroupSequence({"Foo", "Entity"})
* @Assert\Callback({"Symfony\Component\Validator\Tests\Fixtures\CallbackClass", "callback"}) * @Assert\Callback({"Symfony\Component\Validator\Tests\Fixtures\CallbackClass", "callback"})
*/ */
class Entity extends EntityParent implements EntityInterface class Entity extends EntityParent
{ {
/** /**
* @Assert\NotNull * @Assert\NotNull

View File

@ -13,7 +13,7 @@ namespace Symfony\Component\Validator\Tests\Fixtures;
use Symfony\Component\Validator\Constraints\NotNull; use Symfony\Component\Validator\Constraints\NotNull;
class EntityParent class EntityParent implements EntityInterface
{ {
protected $firstName; protected $firstName;
private $internal; private $internal;

View File

@ -20,13 +20,15 @@ class LazyLoadingMetadataFactoryTest extends \PHPUnit_Framework_TestCase
{ {
const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity'; const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity';
const PARENTCLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityParent'; const PARENTCLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityParent';
const INTERFACECLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityInterface';
public function testLoadClassMetadata() public function testLoadClassMetadataWithInterface()
{ {
$factory = new LazyLoadingMetadataFactory(new TestLoader()); $factory = new LazyLoadingMetadataFactory(new TestLoader());
$metadata = $factory->getMetadataFor(self::PARENTCLASS); $metadata = $factory->getMetadataFor(self::PARENTCLASS);
$constraints = array( $constraints = array(
new ConstraintA(array('groups' => array('Default', 'EntityInterface', 'EntityParent'))),
new ConstraintA(array('groups' => array('Default', 'EntityParent'))), new ConstraintA(array('groups' => array('Default', 'EntityParent'))),
); );
@ -41,12 +43,13 @@ class LazyLoadingMetadataFactoryTest extends \PHPUnit_Framework_TestCase
$constraints = array( $constraints = array(
new ConstraintA(array('groups' => array( new ConstraintA(array('groups' => array(
'Default', 'Default',
'EntityInterface',
'EntityParent', 'EntityParent',
'Entity', 'Entity',
))), ))),
new ConstraintA(array('groups' => array( new ConstraintA(array('groups' => array(
'Default', 'Default',
'EntityInterface', 'EntityParent',
'Entity', 'Entity',
))), ))),
new ConstraintA(array('groups' => array( new ConstraintA(array('groups' => array(
@ -63,27 +66,36 @@ class LazyLoadingMetadataFactoryTest extends \PHPUnit_Framework_TestCase
$cache = $this->getMock('Symfony\Component\Validator\Mapping\Cache\CacheInterface'); $cache = $this->getMock('Symfony\Component\Validator\Mapping\Cache\CacheInterface');
$factory = new LazyLoadingMetadataFactory(new TestLoader(), $cache); $factory = new LazyLoadingMetadataFactory(new TestLoader(), $cache);
$tester = $this; $parentClassConstraints = array(
$constraints = array( new ConstraintA(array('groups' => array('Default', 'EntityInterface', 'EntityParent'))),
new ConstraintA(array('groups' => array('Default', 'EntityParent'))), new ConstraintA(array('groups' => array('Default', 'EntityParent'))),
); );
$interfaceConstraints = array(new ConstraintA(array('groups' => array('Default', 'EntityInterface'))));
$cache->expects($this->never()) $cache->expects($this->never())
->method('has'); ->method('has');
$cache->expects($this->once()) $cache->expects($this->exactly(2))
->method('read') ->method('read')
->with($this->equalTo(self::PARENTCLASS)) ->withConsecutive(
array($this->equalTo(self::PARENTCLASS)),
array($this->equalTo(self::INTERFACECLASS))
)
->will($this->returnValue(false)); ->will($this->returnValue(false));
$cache->expects($this->once()) $cache->expects($this->exactly(2))
->method('write') ->method('write')
->will($this->returnCallback(function ($metadata) use ($tester, $constraints) { ->withConsecutive(
$tester->assertEquals($constraints, $metadata->getConstraints()); $this->callback(function ($metadata) use ($interfaceConstraints) {
})); return $interfaceConstraints == $metadata->getConstraints();
}),
$this->callback(function ($metadata) use ($parentClassConstraints) {
return $parentClassConstraints == $metadata->getConstraints();
})
);
$metadata = $factory->getMetadataFor(self::PARENTCLASS); $metadata = $factory->getMetadataFor(self::PARENTCLASS);
$this->assertEquals(self::PARENTCLASS, $metadata->getClassName()); $this->assertEquals(self::PARENTCLASS, $metadata->getClassName());
$this->assertEquals($constraints, $metadata->getConstraints()); $this->assertEquals($parentClassConstraints, $metadata->getConstraints());
} }
public function testReadMetadataFromCache() public function testReadMetadataFromCache()
@ -92,7 +104,6 @@ class LazyLoadingMetadataFactoryTest extends \PHPUnit_Framework_TestCase
$cache = $this->getMock('Symfony\Component\Validator\Mapping\Cache\CacheInterface'); $cache = $this->getMock('Symfony\Component\Validator\Mapping\Cache\CacheInterface');
$factory = new LazyLoadingMetadataFactory($loader, $cache); $factory = new LazyLoadingMetadataFactory($loader, $cache);
$tester = $this;
$metadata = new ClassMetadata(self::PARENTCLASS); $metadata = new ClassMetadata(self::PARENTCLASS);
$metadata->addConstraint(new ConstraintA()); $metadata->addConstraint(new ConstraintA());