bug #19580 [Validator] fixed duplicate constraints with parent class interfaces (dmaicher)

This PR was merged into the 2.7 branch.

Discussion
----------

[Validator] fixed duplicate constraints with parent class interfaces

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | https://github.com/symfony/symfony/issues/19516
| License       | MIT
| Doc PR        | -

This fixes https://github.com/symfony/symfony/issues/19516

Commits
-------

fb36c5a [Validator] fixed duplicate constraints with parent class interfaces
This commit is contained in:
Fabien Potencier 2016-08-23 09:51:32 -07:00
commit 0544117434
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));
}
// 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) {
if ('Symfony\Component\Validator\GroupSequenceProviderInterface' === $interface->name) {
if ('Symfony\Component\Validator\GroupSequenceProviderInterface' === $interface->name || ($parent && $parent->implementsInterface($interface->name))) {
continue;
}
$metadata->mergeConstraints($this->getMetadataFor($interface->name));

View File

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

View File

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

View File

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