From 7cbb63c22705db8d2ca8eb7ed5cf042142854305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Bogusz?= Date: Fri, 20 Mar 2020 20:46:24 +0100 Subject: [PATCH 1/4] Add option to override property constraints in child class --- .../DisableOverridingPropertyConstraints.php | 44 ++++ .../EnableOverridingPropertyConstraints.php | 44 ++++ .../Validator/Mapping/ClassMetadata.php | 29 +++ .../Validator/Mapping/GenericMetadata.php | 33 +++ .../OverridingPropertyConstraintsStrategy.php | 47 +++++ ...sableOverridingPropertyConstraintsTest.php | 30 +++ ...nableOverridingPropertyConstraintsTest.php | 30 +++ .../Tests/Mapping/ClassMetadataTest.php | 194 ++++++++++++++++++ 8 files changed, 451 insertions(+) create mode 100644 src/Symfony/Component/Validator/Constraints/DisableOverridingPropertyConstraints.php create mode 100644 src/Symfony/Component/Validator/Constraints/EnableOverridingPropertyConstraints.php create mode 100644 src/Symfony/Component/Validator/Mapping/OverridingPropertyConstraintsStrategy.php create mode 100644 src/Symfony/Component/Validator/Tests/Constraints/DisableOverridingPropertyConstraintsTest.php create mode 100644 src/Symfony/Component/Validator/Tests/Constraints/EnableOverridingPropertyConstraintsTest.php diff --git a/src/Symfony/Component/Validator/Constraints/DisableOverridingPropertyConstraints.php b/src/Symfony/Component/Validator/Constraints/DisableOverridingPropertyConstraints.php new file mode 100644 index 0000000000000..c02cb95047b80 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/DisableOverridingPropertyConstraints.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\Exception\ConstraintDefinitionException; + +/** + * Disables overriding property constraints of parent class. + * + * Using the annotations on a property has higher precedence than using it on a class. + * + * @Annotation + * + * @author Przemysław Bogusz + */ +class DisableOverridingPropertyConstraints extends Constraint +{ + public function __construct($options = null) + { + if (\is_array($options) && \array_key_exists('groups', $options)) { + throw new ConstraintDefinitionException(sprintf('The option "groups" is not supported by the constraint "%s".', __CLASS__)); + } + + parent::__construct($options); + } + + /** + * {@inheritdoc} + */ + public function getTargets() + { + return [self::PROPERTY_CONSTRAINT, self::CLASS_CONSTRAINT]; + } +} diff --git a/src/Symfony/Component/Validator/Constraints/EnableOverridingPropertyConstraints.php b/src/Symfony/Component/Validator/Constraints/EnableOverridingPropertyConstraints.php new file mode 100644 index 0000000000000..2fe210d79054e --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/EnableOverridingPropertyConstraints.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\Exception\ConstraintDefinitionException; + +/** + * Enables overriding property constraints of parent class. + * + * Using the annotations on a property has higher precedence than using it on a class. + * + * @Annotation + * + * @author Przemysław Bogusz + */ +class EnableOverridingPropertyConstraints extends Constraint +{ + public function __construct($options = null) + { + if (\is_array($options) && \array_key_exists('groups', $options)) { + throw new ConstraintDefinitionException(sprintf('The option "groups" is not supported by the constraint "%s".', __CLASS__)); + } + + parent::__construct($options); + } + + /** + * {@inheritdoc} + */ + public function getTargets() + { + return [self::PROPERTY_CONSTRAINT, self::CLASS_CONSTRAINT]; + } +} diff --git a/src/Symfony/Component/Validator/Mapping/ClassMetadata.php b/src/Symfony/Component/Validator/Mapping/ClassMetadata.php index a5418e189671f..97d4b9911e73e 100644 --- a/src/Symfony/Component/Validator/Mapping/ClassMetadata.php +++ b/src/Symfony/Component/Validator/Mapping/ClassMetadata.php @@ -310,13 +310,42 @@ public function mergeConstraints(self $source) if ($source->isGroupSequenceProvider()) { $this->setGroupSequenceProvider(true); } + + if (OverridingPropertyConstraintsStrategy::NONE === $this->getOverridingPropertyConstraintsStrategy()) { + $this->overridingPropertyConstraintsStrategy = $source->getOverridingPropertyConstraintsStrategy(); + } foreach ($source->getConstraints() as $constraint) { $this->addConstraint(clone $constraint); } foreach ($source->getConstrainedProperties() as $property) { + $strategy = null; + + foreach ($this->getPropertyMetadata($property) as $childMember) { + // overriding is enabled in property + if (OverridingPropertyConstraintsStrategy::ENABLED === $strategy = $childMember->getOverridingPropertyConstraintsStrategy()) { + continue 2; + } + } + foreach ($source->getPropertyMetadata($property) as $member) { + // property is overridden, but property constraints strategy is not set explicitly in property + if (OverridingPropertyConstraintsStrategy::NONE === $strategy) { + // class' strategy takes precedence over parent class property's strategy, if both are set + if (OverridingPropertyConstraintsStrategy::NONE === $strategy = $this->getOverridingPropertyConstraintsStrategy()) { + $strategy = $member->getOverridingPropertyConstraintsStrategy(); + } + + foreach ($this->getPropertyMetadata($property) as $childMember) { + $childMember->overridingPropertyConstraintsStrategy = $strategy; + } + + if (OverridingPropertyConstraintsStrategy::ENABLED === $strategy) { + continue 2; + } + } + $member = clone $member; foreach ($member->getConstraints() as $constraint) { diff --git a/src/Symfony/Component/Validator/Mapping/GenericMetadata.php b/src/Symfony/Component/Validator/Mapping/GenericMetadata.php index f470f1d98d9eb..52a4c0499c4f3 100644 --- a/src/Symfony/Component/Validator/Mapping/GenericMetadata.php +++ b/src/Symfony/Component/Validator/Mapping/GenericMetadata.php @@ -14,6 +14,8 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraints\DisableAutoMapping; use Symfony\Component\Validator\Constraints\EnableAutoMapping; +use Symfony\Component\Validator\Constraints\DisableOverridingPropertyConstraints; +use Symfony\Component\Validator\Constraints\EnableOverridingPropertyConstraints; use Symfony\Component\Validator\Constraints\Traverse; use Symfony\Component\Validator\Constraints\Valid; use Symfony\Component\Validator\Exception\ConstraintDefinitionException; @@ -88,6 +90,21 @@ class GenericMetadata implements MetadataInterface */ public $autoMappingStrategy = AutoMappingStrategy::NONE; + /** + * The strategy for merging/overriding property constraints, when extending class that has property constraints. + * + * By default, property constraints in a child class are merged with property constraints of a parent class. + * + * @var int + * + * @see OverridingPropertyConstraintsStrategy + * + * @internal This property is public in order to reduce the size of the + * class' serialized representation. Do not access it. Use + * {@link getOverridingPropertyConstraintsStrategy()} instead. + */ + public $overridingPropertyConstraintsStrategy = OverridingPropertyConstraintsStrategy::NONE; + /** * Returns the names of the properties that should be serialized. * @@ -101,6 +118,7 @@ public function __sleep() 'cascadingStrategy', 'traversalStrategy', 'autoMappingStrategy', + 'overridingPropertyConstraintsStrategy' ]; } @@ -160,6 +178,13 @@ public function addConstraint(Constraint $constraint) return $this; } + if ($constraint instanceof EnableOverridingPropertyConstraints || $constraint instanceof DisableOverridingPropertyConstraints) { + $this->overridingPropertyConstraintsStrategy = $constraint instanceof EnableOverridingPropertyConstraints ? OverridingPropertyConstraintsStrategy::ENABLED : OverridingPropertyConstraintsStrategy::DISABLED; + + // The constraint is not added + return $this; + } + $this->constraints[] = $constraint; foreach ($constraint->groups as $group) { @@ -236,4 +261,12 @@ public function getAutoMappingStrategy(): int { return $this->autoMappingStrategy; } + + /** + * @see OverridingPropertyConstraintsStrategy + */ + public function getOverridingPropertyConstraintsStrategy(): int + { + return $this->overridingPropertyConstraintsStrategy; + } } diff --git a/src/Symfony/Component/Validator/Mapping/OverridingPropertyConstraintsStrategy.php b/src/Symfony/Component/Validator/Mapping/OverridingPropertyConstraintsStrategy.php new file mode 100644 index 0000000000000..e7d8e3d9dbe88 --- /dev/null +++ b/src/Symfony/Component/Validator/Mapping/OverridingPropertyConstraintsStrategy.php @@ -0,0 +1,47 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Mapping; + +/** + * Specifies whether class' property constraints should be merged with + * or override property constraints of a parent class. + * + * Overriding property constraints works for properties that override parent property of same name. + * + * @author Przemysław Bogusz + */ +final class OverridingPropertyConstraintsStrategy +{ + /** + * Nothing explicitly set. + * In case of property, rely on class strategy. + * In case of class, inherit strategy from parent class. + */ + public const NONE = 0; + + /** + * Specifies that class' property constraints should be merged with constraints of a parent class. + */ + public const DISABLED = 1; + + /** + * Specifies that class' property constraints should override constraints of a parent class. + */ + public const ENABLED = 2; + + /** + * Not instantiable. + */ + private function __construct() + { + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/DisableOverridingPropertyConstraintsTest.php b/src/Symfony/Component/Validator/Tests/Constraints/DisableOverridingPropertyConstraintsTest.php new file mode 100644 index 0000000000000..d720e7557fe76 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/DisableOverridingPropertyConstraintsTest.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Constraints; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Validator\Constraints\DisableOverridingPropertyConstraints; +use Symfony\Component\Validator\Exception\ConstraintDefinitionException; + +/** + * @author Przemysław Bogusz + */ +class DisableOverridingPropertyConstraintsTest extends TestCase +{ + public function testGroups() + { + $this->expectException(ConstraintDefinitionException::class); + $this->expectExceptionMessage(sprintf('The option "groups" is not supported by the constraint "%s".', DisableOverridingPropertyConstraints::class)); + + new DisableOverridingPropertyConstraints(['groups' => 'foo']); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Constraints/EnableOverridingPropertyConstraintsTest.php b/src/Symfony/Component/Validator/Tests/Constraints/EnableOverridingPropertyConstraintsTest.php new file mode 100644 index 0000000000000..f8f808cecf3f0 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/EnableOverridingPropertyConstraintsTest.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Constraints; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Validator\Constraints\EnableOverridingPropertyConstraints; +use Symfony\Component\Validator\Exception\ConstraintDefinitionException; + +/** + * @author Przemysław Bogusz + */ +class EnableOverridingPropertyConstraintsTest extends TestCase +{ + public function testGroups() + { + $this->expectException(ConstraintDefinitionException::class); + $this->expectExceptionMessage(sprintf('The option "groups" is not supported by the constraint "%s".', EnableOverridingPropertyConstraints::class)); + + new EnableOverridingPropertyConstraints(['groups' => 'foo']); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php b/src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php index bbe3475ebdb4a..c314e28de71f9 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php @@ -13,8 +13,12 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\Constraints\GreaterThan; +use Symfony\Component\Validator\Constraints\DisableOverridingPropertyConstraints; +use Symfony\Component\Validator\Constraints\EnableOverridingPropertyConstraints; use Symfony\Component\Validator\Constraints\Valid; use Symfony\Component\Validator\Mapping\ClassMetadata; +use Symfony\Component\Validator\Mapping\OverridingPropertyConstraintsStrategy; use Symfony\Component\Validator\Tests\Fixtures\ConstraintA; use Symfony\Component\Validator\Tests\Fixtures\ConstraintB; use Symfony\Component\Validator\Tests\Fixtures\PropertyConstraint; @@ -310,4 +314,194 @@ 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 testOverrideConstraintsIfOverridingEnabledInProperty() + { + $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 EnableOverridingPropertyConstraints()); + $childMetadata->addPropertyConstraint('example', new GreaterThan(1)); + $childMetadata->mergeConstraints($parentMetadata); + + $expectedMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ChildClass'); + $expectedMetadata->addPropertyConstraint('example', new EnableOverridingPropertyConstraints()); + $expectedMetadata->addPropertyConstraint('example', new GreaterThan(1)); + + $this->assertEquals($expectedMetadata, $childMetadata); + } + + public function testOverrideConstraintsIfOverridingEnabledInPropertyAndDisabledInClass() + { + $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->addConstraint(new DisableOverridingPropertyConstraints()); + $childMetadata->addPropertyConstraint('example', new EnableOverridingPropertyConstraints()); + $childMetadata->addPropertyConstraint('example', new GreaterThan(1)); + $childMetadata->mergeConstraints($parentMetadata); + + $expectedMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ChildClass'); + $expectedMetadata->addConstraint(new DisableOverridingPropertyConstraints()); + $expectedMetadata->addPropertyConstraint('example', new EnableOverridingPropertyConstraints()); + $expectedMetadata->addPropertyConstraint('example', new GreaterThan(1)); + + $this->assertEquals($expectedMetadata, $childMetadata); + } + + public function testOverrideConstraintsIfOverridingEnabledInClass() + { + $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->addConstraint(new EnableOverridingPropertyConstraints()); + $childMetadata->addPropertyConstraint('example', new GreaterThan(1)); + $childMetadata->mergeConstraints($parentMetadata); + + $expectedMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ChildClass'); + $expectedMetadata->addConstraint(new EnableOverridingPropertyConstraints()); + $expectedMetadata->addPropertyConstraint('example', new GreaterThan(1)); + $expectedMetadata->getPropertyMetadata('example')[0]->overridingPropertyConstraintsStrategy = OverridingPropertyConstraintsStrategy::ENABLED; + + $this->assertEquals($expectedMetadata, $childMetadata); + } + + public function testOverrideConstraintsIfOverridingEnabledInClassAndDisabledInParentClass() + { + $parentMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ParentClass'); + $parentMetadata->addConstraint(new DisableOverridingPropertyConstraints()); + $parentMetadata->addPropertyConstraint('example', new GreaterThan(0)); + + $childMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ChildClass'); + $childMetadata->addConstraint(new EnableOverridingPropertyConstraints()); + $childMetadata->addPropertyConstraint('example', new GreaterThan(1)); + $childMetadata->mergeConstraints($parentMetadata); + + $expectedMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ChildClass'); + $expectedMetadata->addConstraint(new EnableOverridingPropertyConstraints()); + $expectedMetadata->addPropertyConstraint('example', new GreaterThan(1)); + $expectedMetadata->getPropertyMetadata('example')[0]->overridingPropertyConstraintsStrategy = OverridingPropertyConstraintsStrategy::ENABLED; + + $this->assertEquals($expectedMetadata, $childMetadata); + } + + public function testOverrideConstraintsIfOverridingEnabledInParentClass() + { + $parentMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ParentClass'); + $parentMetadata->addConstraint(new EnableOverridingPropertyConstraints()); + $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)); + $expectedMetadata->overridingPropertyConstraintsStrategy = OverridingPropertyConstraintsStrategy::ENABLED; + $expectedMetadata->getPropertyMetadata('example')[0]->overridingPropertyConstraintsStrategy = OverridingPropertyConstraintsStrategy::ENABLED; + + $this->assertEquals($expectedMetadata, $childMetadata); + } + + public function testOverrideConstraintsIfOverridingEnabledInParentClassProperty() + { + $parentMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ParentClass'); + $parentMetadata->addPropertyConstraint('example', new EnableOverridingPropertyConstraints()); + $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)); + $expectedMetadata->getPropertyMetadata('example')[0]->overridingPropertyConstraintsStrategy = OverridingPropertyConstraintsStrategy::ENABLED; + + $this->assertEquals($expectedMetadata, $childMetadata); + } + + public function testMergeConstraintsIfOverridingEnabledInClassAndDisabledInProperty() + { + $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->addConstraint(new EnableOverridingPropertyConstraints()); + $childMetadata->addPropertyConstraint('example', new GreaterThan(1)); + $childMetadata->addPropertyConstraint('example', new DisableOverridingPropertyConstraints()); + $childMetadata->mergeConstraints($parentMetadata); + + $childConstraints = []; + foreach ($childMetadata->getPropertyMetadata('example') as $member) { + $childConstraints[] = $member->getConstraints()[0]; + } + + $expectedConstraints = [ + new GreaterThan(['value' => 1, 'groups' => ['Default', 'ChildClass']]), + new GreaterThan(['value' => 0, 'groups' => ['Default', 'ParentClass', 'ChildClass']]), + ]; + + $this->assertEquals($expectedConstraints, $childConstraints); + } + + public function testMergeConstraintsIfOverridingEnabledInParentClassAndDisabledInChildClass() + { + $parentMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ParentClass'); + $parentMetadata->addConstraint(new EnableOverridingPropertyConstraints()); + $parentMetadata->addPropertyConstraint('example', new GreaterThan(0)); + + $childMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ChildClass'); + $childMetadata->addConstraint(new DisableOverridingPropertyConstraints()); + $childMetadata->addPropertyConstraint('example', new GreaterThan(1)); + $childMetadata->mergeConstraints($parentMetadata); + + $childConstraints = []; + foreach ($childMetadata->getPropertyMetadata('example') as $member) { + $childConstraints[] = $member->getConstraints()[0]; + } + + $expectedConstraints = [ + new GreaterThan(['value' => 1, 'groups' => ['Default', 'ChildClass']]), + new GreaterThan(['value' => 0, 'groups' => ['Default', 'ParentClass', 'ChildClass']]), + ]; + + $this->assertEquals($expectedConstraints, $childConstraints); + } + + public function testMergeConstraintsIfOverridingEnabledInParentClassPropertyAndDisabledInChildClassProperty() + { + $parentMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ParentClass'); + $parentMetadata->addPropertyConstraint('example', new EnableOverridingPropertyConstraints()); + $parentMetadata->addPropertyConstraint('example', new GreaterThan(0)); + + $childMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ChildClass'); + $childMetadata->addPropertyConstraint('example', new DisableOverridingPropertyConstraints()); + $childMetadata->addPropertyConstraint('example', new GreaterThan(1)); + $childMetadata->mergeConstraints($parentMetadata); + + $childConstraints = []; + foreach ($childMetadata->getPropertyMetadata('example') as $member) { + $childConstraints[] = $member->getConstraints()[0]; + } + + $expectedConstraints = [ + new GreaterThan(['value' => 1, 'groups' => ['Default', 'ChildClass']]), + new GreaterThan(['value' => 0, 'groups' => ['Default', 'ParentClass', 'ChildClass']]), + ]; + + $this->assertEquals($expectedConstraints, $childConstraints); + } +} + +class ParentClass +{ + public $example = 0; +} + +class ChildClass extends ParentClass +{ + public $example = 1; // overrides parent property of same name } From 7049301bf7764108b729487788f4161e11e6ea11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Bogusz?= Date: Sat, 21 Mar 2020 21:16:38 +0100 Subject: [PATCH 2/4] Fix CS --- .../Component/Validator/Mapping/ClassMetadata.php | 8 ++++---- .../Component/Validator/Mapping/GenericMetadata.php | 4 ++-- .../Mapping/OverridingPropertyConstraintsStrategy.php | 2 +- .../Validator/Tests/Mapping/ClassMetadataTest.php | 10 +++++----- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Symfony/Component/Validator/Mapping/ClassMetadata.php b/src/Symfony/Component/Validator/Mapping/ClassMetadata.php index 97d4b9911e73e..a2cbc18c12cf9 100644 --- a/src/Symfony/Component/Validator/Mapping/ClassMetadata.php +++ b/src/Symfony/Component/Validator/Mapping/ClassMetadata.php @@ -310,7 +310,7 @@ public function mergeConstraints(self $source) if ($source->isGroupSequenceProvider()) { $this->setGroupSequenceProvider(true); } - + if (OverridingPropertyConstraintsStrategy::NONE === $this->getOverridingPropertyConstraintsStrategy()) { $this->overridingPropertyConstraintsStrategy = $source->getOverridingPropertyConstraintsStrategy(); } @@ -321,14 +321,14 @@ public function mergeConstraints(self $source) foreach ($source->getConstrainedProperties() as $property) { $strategy = null; - + foreach ($this->getPropertyMetadata($property) as $childMember) { // overriding is enabled in property if (OverridingPropertyConstraintsStrategy::ENABLED === $strategy = $childMember->getOverridingPropertyConstraintsStrategy()) { continue 2; } } - + foreach ($source->getPropertyMetadata($property) as $member) { // property is overridden, but property constraints strategy is not set explicitly in property if (OverridingPropertyConstraintsStrategy::NONE === $strategy) { @@ -345,7 +345,7 @@ public function mergeConstraints(self $source) continue 2; } } - + $member = clone $member; foreach ($member->getConstraints() as $constraint) { diff --git a/src/Symfony/Component/Validator/Mapping/GenericMetadata.php b/src/Symfony/Component/Validator/Mapping/GenericMetadata.php index 52a4c0499c4f3..81d50c0f70cf9 100644 --- a/src/Symfony/Component/Validator/Mapping/GenericMetadata.php +++ b/src/Symfony/Component/Validator/Mapping/GenericMetadata.php @@ -13,8 +13,8 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraints\DisableAutoMapping; -use Symfony\Component\Validator\Constraints\EnableAutoMapping; use Symfony\Component\Validator\Constraints\DisableOverridingPropertyConstraints; +use Symfony\Component\Validator\Constraints\EnableAutoMapping; use Symfony\Component\Validator\Constraints\EnableOverridingPropertyConstraints; use Symfony\Component\Validator\Constraints\Traverse; use Symfony\Component\Validator\Constraints\Valid; @@ -118,7 +118,7 @@ public function __sleep() 'cascadingStrategy', 'traversalStrategy', 'autoMappingStrategy', - 'overridingPropertyConstraintsStrategy' + 'overridingPropertyConstraintsStrategy', ]; } diff --git a/src/Symfony/Component/Validator/Mapping/OverridingPropertyConstraintsStrategy.php b/src/Symfony/Component/Validator/Mapping/OverridingPropertyConstraintsStrategy.php index e7d8e3d9dbe88..7fd5de43c5553 100644 --- a/src/Symfony/Component/Validator/Mapping/OverridingPropertyConstraintsStrategy.php +++ b/src/Symfony/Component/Validator/Mapping/OverridingPropertyConstraintsStrategy.php @@ -27,7 +27,7 @@ final class OverridingPropertyConstraintsStrategy * In case of class, inherit strategy from parent class. */ public const NONE = 0; - + /** * Specifies that class' property constraints should be merged with constraints of a parent class. */ diff --git a/src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php b/src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php index c314e28de71f9..666c702cf692c 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php @@ -13,9 +13,9 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Validator\Constraint; -use Symfony\Component\Validator\Constraints\GreaterThan; use Symfony\Component\Validator\Constraints\DisableOverridingPropertyConstraints; use Symfony\Component\Validator\Constraints\EnableOverridingPropertyConstraints; +use Symfony\Component\Validator\Constraints\GreaterThan; use Symfony\Component\Validator\Constraints\Valid; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Mapping\OverridingPropertyConstraintsStrategy; @@ -314,17 +314,17 @@ 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 testOverrideConstraintsIfOverridingEnabledInProperty() { $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 EnableOverridingPropertyConstraints()); $childMetadata->addPropertyConstraint('example', new GreaterThan(1)); $childMetadata->mergeConstraints($parentMetadata); - + $expectedMetadata = new ClassMetadata('\Symfony\Component\Validator\Tests\Mapping\ChildClass'); $expectedMetadata->addPropertyConstraint('example', new EnableOverridingPropertyConstraints()); $expectedMetadata->addPropertyConstraint('example', new GreaterThan(1)); @@ -438,7 +438,7 @@ public function testMergeConstraintsIfOverridingEnabledInClassAndDisabledInPrope foreach ($childMetadata->getPropertyMetadata('example') as $member) { $childConstraints[] = $member->getConstraints()[0]; } - + $expectedConstraints = [ new GreaterThan(['value' => 1, 'groups' => ['Default', 'ChildClass']]), new GreaterThan(['value' => 0, 'groups' => ['Default', 'ParentClass', 'ChildClass']]), From 41160a6a95272e19dab6e2205f0ae225c916c074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Bogusz?= Date: Mon, 30 Mar 2020 08:14:00 +0200 Subject: [PATCH 3/4] Add line to restart CI --- .../Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php index 2870781780117..bcdb3ebf8d3de 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php @@ -37,6 +37,7 @@ class AtLeastOneOfValidatorTest extends ConstraintValidatorTestCase protected function createValidator() { return new AtLeastOneOfValidator(); + } /** From 3a230f2d87cf31d0e01a6b69c7e6445f4a1f7b37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Bogusz?= Date: Mon, 30 Mar 2020 08:14:32 +0200 Subject: [PATCH 4/4] Revert "Add line to restart CI" This reverts commit 14e69a9813ba976226d2548dfad5e4a91f8eb208. --- .../Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php index bcdb3ebf8d3de..2870781780117 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/AtLeastOneOfValidatorTest.php @@ -37,7 +37,6 @@ class AtLeastOneOfValidatorTest extends ConstraintValidatorTestCase protected function createValidator() { return new AtLeastOneOfValidator(); - } /** pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy