Skip to content

Commit

Permalink
Merge branch '2.8' into 3.1
Browse files Browse the repository at this point in the history
* 2.8:
  [Validator][GroupSequence] fixed GroupSequence validation ignores PropertyMetadata of parent classes
  [FrameworkBundle][Security] Remove useless mocks
  [DoctrineBridge] Enhance exception message in EntityUserProvider
  added friendly exception when constraint validator does not exist or it is not enabled
  remove duplicate instruction
  [FrameworkBundle] Remove TranslatorBagInterface check
  [FrameworkBundle] Remove duplicated code in RouterDebugCommand
  [Validator] fixed duplicate constraints with parent class interfaces
  SecurityBundle:BasicAuthenticationListener: removed a default argument on getting a header value
  • Loading branch information
nicolas-grekas committed Aug 26, 2016
2 parents 073edf1 + f0df329 commit 96cad1c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 23 deletions.
1 change: 1 addition & 0 deletions Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ public function mergeConstraints(ClassMetadata $source)
$member = clone $member;

foreach ($member->getConstraints() as $constraint) {
$member->constraintsByGroup[$this->getDefaultGroup()][] = $constraint;
$constraint->addImplicitGroupName($this->getDefaultGroup());
}

Expand Down
4 changes: 2 additions & 2 deletions Mapping/Factory/LazyLoadingMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,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 Tests/Fixtures/Entity.php
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion Tests/Fixtures/EntityParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Symfony\Component\Validator\Constraints\NotNull;

class EntityParent
class EntityParent implements EntityInterface
{
protected $firstName;
private $internal;
Expand Down
36 changes: 27 additions & 9 deletions Tests/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,41 @@ public function testMergeConstraintsMergesMemberConstraints()
$this->metadata->mergeConstraints($parent);
$this->metadata->addPropertyConstraint('firstName', new ConstraintA());

$constraintA1 = new ConstraintA(array('groups' => array(
'Default',
'EntityParent',
'Entity',
)));
$constraintA2 = new ConstraintA(array('groups' => array(
'Default',
'Entity',
)));

$constraints = array(
new ConstraintA(array('groups' => array(
'Default',
'EntityParent',
'Entity',
))),
new ConstraintA(array('groups' => array(
'Default',
'Entity',
))),
$constraintA1,
$constraintA2,
);

$constraintsByGroup = array(
'Default' => array(
$constraintA1,
$constraintA2,
),
'EntityParent' => array(
$constraintA1,
),
'Entity' => array(
$constraintA1,
$constraintA2,
),
);

$members = $this->metadata->getPropertyMetadata('firstName');

$this->assertCount(1, $members);
$this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
$this->assertEquals($constraints, $members[0]->getConstraints());
$this->assertEquals($constraintsByGroup, $members[0]->constraintsByGroup);
}

public function testMemberMetadatas()
Expand Down
33 changes: 23 additions & 10 deletions Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php
Original file line number Diff line number Diff line change
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,26 +66,36 @@ public function testWriteMetadataToCache()
$cache = $this->getMock('Symfony\Component\Validator\Mapping\Cache\CacheInterface');
$factory = new LazyLoadingMetadataFactory(new TestLoader(), $cache);

$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 ($constraints) {
$this->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 Down

0 comments on commit 96cad1c

Please sign in to comment.