From af76d074a0a0db5509f2bb4ae55f739d72302cb7 Mon Sep 17 00:00:00 2001 From: po_taka Date: Sun, 5 Feb 2017 19:55:20 +0200 Subject: [PATCH 1/4] added test for staticClassLoader in LazyLoadingMetadatafactory --- .../Tests/Fixtures/EntityStaticCar.php | 20 +++++++++++++++++ .../Tests/Fixtures/EntityStaticCarTurbo.php | 20 +++++++++++++++++ .../Tests/Fixtures/EntityStaticVehicle.php | 22 +++++++++++++++++++ .../LazyLoadingMetadataFactoryTest.php | 15 +++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCar.php create mode 100644 src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCarTurbo.php create mode 100644 src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticVehicle.php diff --git a/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCar.php b/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCar.php new file mode 100644 index 0000000000000..713265ba0d755 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCar.php @@ -0,0 +1,20 @@ + + */ +class EntityStaticCar extends EntityStaticVehicle +{ + public static function loadValidatorMetadata(ClassMetadata $metadata) + { + $metadata->addPropertyConstraint('wheels', new Length(array ('max' => 99,))); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCarTurbo.php b/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCarTurbo.php new file mode 100644 index 0000000000000..daea271587569 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCarTurbo.php @@ -0,0 +1,20 @@ + + */ +class EntityStaticCarTurbo extends EntityStaticCar +{ + public static function loadValidatorMetadata(ClassMetadata $metadata) + { + $metadata->addPropertyConstraint('wheels', new Length(array ('max' => 99,))); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticVehicle.php b/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticVehicle.php new file mode 100644 index 0000000000000..42d6946c8840a --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticVehicle.php @@ -0,0 +1,22 @@ + + */ +class EntityStaticVehicle +{ + public $wheels; + + public static function loadValidatorMetadata(ClassMetadata $metadata) + { + $metadata->addPropertyConstraint('wheels', new Length(array ('max' => 99,))); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php index 9696744166537..d4c7d02cd2412 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php @@ -168,6 +168,21 @@ 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'); + $classMetaData = $metadata->getPropertyMetadata('wheels'); + $groups = $classMetaData[0]->getConstraints()[0]->groups; + + $this->assertContains('Default', $groups); + $this->assertContains('EntityStaticCarTurbo', $groups); + $this->assertContains('EntityStaticCar', $groups); + $this->assertContains('EntityStaticVehicle', $groups); + } + } class TestLoader implements LoaderInterface From e23e7e4f4bc6aaea03deb60ea78d92b8d47709d0 Mon Sep 17 00:00:00 2001 From: po_taka Date: Sun, 5 Feb 2017 19:58:39 +0200 Subject: [PATCH 2/4] added test for staticClassLoader in LazyLoadingMetadatafactory --- .../Validator/Tests/Fixtures/EntityStaticCar.php | 14 +++++++++----- .../Tests/Fixtures/EntityStaticCarTurbo.php | 14 +++++++++----- .../Tests/Fixtures/EntityStaticVehicle.php | 14 +++++++++----- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCar.php b/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCar.php index 713265ba0d755..e8d2667199197 100644 --- a/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCar.php +++ b/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCar.php @@ -1,16 +1,20 @@ + * + * 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; -/** - * Description of EntityStatic - * - * @author po_taka - */ class EntityStaticCar extends EntityStaticVehicle { public static function loadValidatorMetadata(ClassMetadata $metadata) diff --git a/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCarTurbo.php b/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCarTurbo.php index daea271587569..b26522d66ba07 100644 --- a/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCarTurbo.php +++ b/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticCarTurbo.php @@ -1,16 +1,20 @@ + * + * 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; -/** - * Description of EntityStaticParent - * - * @author po_taka - */ class EntityStaticCarTurbo extends EntityStaticCar { public static function loadValidatorMetadata(ClassMetadata $metadata) diff --git a/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticVehicle.php b/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticVehicle.php index 42d6946c8840a..3dc5bfd2f4d54 100644 --- a/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticVehicle.php +++ b/src/Symfony/Component/Validator/Tests/Fixtures/EntityStaticVehicle.php @@ -1,16 +1,20 @@ + * + * 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; -/** - * Description of EntityStaticVehicle - * - * @author po_taka - */ class EntityStaticVehicle { public $wheels; From f0c4b645e91b72570c55422cda65688cda6918e1 Mon Sep 17 00:00:00 2001 From: po_taka Date: Sun, 5 Feb 2017 20:07:42 +0200 Subject: [PATCH 3/4] added test for staticClassLoader in LazyLoadingMetadatafactory --- .../Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php index d4c7d02cd2412..0628c4685bafa 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php @@ -175,7 +175,8 @@ public function testGroupsFromParent() $factory = new LazyLoadingMetadataFactory($reader); $metadata = $factory->getMetadataFor('Symfony\Component\Validator\Tests\Fixtures\EntityStaticCarTurbo'); $classMetaData = $metadata->getPropertyMetadata('wheels'); - $groups = $classMetaData[0]->getConstraints()[0]->groups; + $constraints = $classMetaData[0]->getConstraints(); + $groups = $constraints[0]->groups; $this->assertContains('Default', $groups); $this->assertContains('EntityStaticCarTurbo', $groups); From 916404c28343e95f21cdccafe926450965261f62 Mon Sep 17 00:00:00 2001 From: po_taka Date: Thu, 9 Feb 2017 23:25:07 +0200 Subject: [PATCH 4/4] LazyLoadingMetadataFactory test. Iterate over constraints and groups --- .../LazyLoadingMetadataFactoryTest.php | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php index 0628c4685bafa..bc584f1fec360 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php @@ -174,14 +174,35 @@ public function testGroupsFromParent() $reader = new \Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader(); $factory = new LazyLoadingMetadataFactory($reader); $metadata = $factory->getMetadataFor('Symfony\Component\Validator\Tests\Fixtures\EntityStaticCarTurbo'); - $classMetaData = $metadata->getPropertyMetadata('wheels'); - $constraints = $classMetaData[0]->getConstraints(); - $groups = $constraints[0]->groups; - - $this->assertContains('Default', $groups); - $this->assertContains('EntityStaticCarTurbo', $groups); - $this->assertContains('EntityStaticCar', $groups); - $this->assertContains('EntityStaticVehicle', $groups); + $classMetaDataCollection = $metadata->getPropertyMetadata('wheels'); + + /* + * Array element will be removed if it exists + * in any constraint group + */ + $expectedConstraints = [ + 'Default' => 'Default', + 'EntityStaticCarTurbo' => 'EntityStaticCarTurbo', + 'EntityStaticCar' => 'EntityStaticCar', + 'EntityStaticVehicle' => 'EntityStaticVehicle', + ]; + + foreach ($classMetaDataCollection as $classMetaData) { + $constraints = $classMetaData->getConstraints(); + foreach ($constraints as $constraint) { + $groups = $constraint->groups; + foreach ($groups as $group) { + if (array_key_exists($group, $expectedConstraints)) { + unset($expectedConstraints[$group]); + } + } + } + } + + $this->assertEmpty( + $expectedConstraints, + "Following groups were not found " . implode(',', $expectedConstraints) + ); } }