Skip to content

[Validator] property constraints can be added in child classes #21592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/Symfony/Component/Validator/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,6 @@ public function mergeConstraints(ClassMetadata $source)
}

foreach ($source->getConstrainedProperties() as $property) {
if ($this->hasPropertyMetadata($property)) {
continue;
}

foreach ($source->getPropertyMetadata($property) as $member) {
$member = clone $member;

Expand Down
23 changes: 23 additions & 0 deletions src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests\Fixtures;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\Length;

class EntityStaticCar extends EntityStaticVehicle
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('wheels', new Length(array('max' => 99)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests\Fixtures;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\Length;

class EntityStaticCarTurbo extends EntityStaticCar
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('wheels', new Length(array('max' => 99)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests\Fixtures;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\Length;

class EntityStaticVehicle
{
public $wheels;

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('wheels', new Length(array('max' => 99)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
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;
Expand Down Expand Up @@ -304,21 +303,6 @@ public function testGetPropertyMetadataReturnsEmptyArrayWithoutConfiguredMetadat
{
$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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,25 @@ public function testMetadataCacheWithRuntimeConstraint()

$metadata = $factory->getMetadataFor(self::CLASS_NAME);
}

public function testGroupsFromParent()
{
$reader = new \Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader();
$factory = new LazyLoadingMetadataFactory($reader);
$metadata = $factory->getMetadataFor('Symfony\Component\Validator\Tests\Fixtures\EntityStaticCarTurbo');
$groups = array();

foreach ($metadata->getPropertyMetadata('wheels') as $propertyMetadata) {
$constraints = $propertyMetadata->getConstraints();
$groups = array_replace($groups, $constraints[0]->groups);
}

$this->assertCount(4, $groups);
$this->assertContains('Default', $groups);
$this->assertContains('EntityStaticCarTurbo', $groups);
$this->assertContains('EntityStaticCar', $groups);
$this->assertContains('EntityStaticVehicle', $groups);
}
}

class TestLoader implements LoaderInterface
Expand Down