Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 | #19516
| License       | MIT
| Doc PR        | -

This fixes #19516

Commits
-------

fb36c5a [Validator] fixed duplicate constraints with parent class interfaces
  • Loading branch information
fabpot committed Aug 23, 2016
2 parents 5e5e1db + fb36c5a commit 0544117
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
Expand Up @@ -116,9 +116,9 @@ public function getMetadataFor($value)
$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));
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Validator/Tests/Fixtures/Entity.php
Expand Up @@ -19,7 +19,7 @@
* @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
Expand Down
Expand Up @@ -13,7 +13,7 @@

use Symfony\Component\Validator\Constraints\NotNull;

class EntityParent
class EntityParent implements EntityInterface
{
protected $firstName;
private $internal;
Expand Down
Expand Up @@ -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'))),
);

Expand All @@ -41,12 +43,13 @@ public function testMergeParentConstraints()
$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(
Expand All @@ -63,27 +66,36 @@ public function testWriteMetadataToCache()
$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()
Expand All @@ -92,7 +104,6 @@ public function testReadMetadataFromCache()
$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());

Expand Down

0 comments on commit 0544117

Please sign in to comment.