override property constraints in child class
This commit is contained in:
parent
a4ac1a785e
commit
8b281fe401
@ -346,6 +346,10 @@ class ClassMetadata extends ElementMetadata implements ClassMetadataInterface
|
||||
}
|
||||
|
||||
foreach ($source->getConstrainedProperties() as $property) {
|
||||
if ($this->hasPropertyMetadata($property)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($source->getPropertyMetadata($property) as $member) {
|
||||
$member = clone $member;
|
||||
|
||||
|
@ -12,6 +12,7 @@
|
||||
namespace Symfony\Component\Validator\Tests\Mapping;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\Constraints\GreaterThan;
|
||||
use Symfony\Component\Validator\Constraints\Valid;
|
||||
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
||||
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
|
||||
@ -295,4 +296,29 @@ class ClassMetadataTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$this->assertCount(0, $this->metadata->getPropertyMetadata('foo'), '->getPropertyMetadata() returns an empty collection if no metadata is configured for the given property');
|
||||
}
|
||||
|
||||
public function testMergeDoesOverrideConstraintsFromParentClassIfPropertyIsOverriddenInChildClass()
|
||||
{
|
||||
$parentMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ParentClass');
|
||||
$parentMetadata->addPropertyConstraint('example', new GreaterThan(0));
|
||||
|
||||
$childMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ChildClass');
|
||||
$childMetadata->addPropertyConstraint('example', new GreaterThan(1));
|
||||
$childMetadata->mergeConstraints($parentMetadata);
|
||||
|
||||
$expectedMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ChildClass');
|
||||
$expectedMetadata->addPropertyConstraint('example', new GreaterThan(1));
|
||||
|
||||
$this->assertEquals($expectedMetadata, $childMetadata);
|
||||
}
|
||||
}
|
||||
|
||||
class ParentClass
|
||||
{
|
||||
public $example = 0;
|
||||
}
|
||||
|
||||
class ChildClass extends ParentClass
|
||||
{
|
||||
public $example = 1; // overrides parent property of same name
|
||||
}
|
||||
|
Reference in New Issue
Block a user