From 633007649be336e941dea9adb936ddbce3b21488 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Wed, 1 Jun 2022 18:17:21 +0200 Subject: [PATCH] Fix getting class constraints on debug command --- .../Validator/Command/DebugCommand.php | 30 ++++- .../Tests/Command/DebugCommandTest.php | 125 +++++++++++------- 2 files changed, 102 insertions(+), 53 deletions(-) diff --git a/src/Symfony/Component/Validator/Command/DebugCommand.php b/src/Symfony/Component/Validator/Command/DebugCommand.php index ad0ebba021396..be2c3fe96337e 100644 --- a/src/Symfony/Component/Validator/Command/DebugCommand.php +++ b/src/Symfony/Component/Validator/Command/DebugCommand.php @@ -90,7 +90,19 @@ private function dumpValidatorsForClass(InputInterface $input, OutputInterface $ $rows = []; $dump = new Dumper($output); - foreach ($this->getConstrainedPropertiesData($class) as $propertyName => $constraintsData) { + /** @var ClassMetadataInterface $classMetadata */ + $classMetadata = $this->validator->getMetadataFor($class); + + foreach ($this->getClassConstraintsData($classMetadata) as $data) { + $rows[] = [ + '-', + $data['class'], + implode(', ', $data['groups']), + $dump($data['options']), + ]; + } + + foreach ($this->getConstrainedPropertiesData($classMetadata) as $propertyName => $constraintsData) { foreach ($constraintsData as $data) { $rows[] = [ $propertyName, @@ -121,12 +133,20 @@ private function dumpValidatorsForClass(InputInterface $input, OutputInterface $ $table->render(); } - private function getConstrainedPropertiesData(string $class): array + private function getClassConstraintsData(ClassMetadataInterface $classMetadata): iterable { - $data = []; + foreach ($classMetadata->getConstraints() as $constraint) { + yield [ + 'class' => \get_class($constraint), + 'groups' => $constraint->groups, + 'options' => $this->getConstraintOptions($constraint), + ]; + } + } - /** @var ClassMetadataInterface $classMetadata */ - $classMetadata = $this->validator->getMetadataFor($class); + private function getConstrainedPropertiesData(ClassMetadataInterface $classMetadata): array + { + $data = []; foreach ($classMetadata->getConstrainedProperties() as $constrainedProperty) { $data[$constrainedProperty] = $this->getPropertyData($classMetadata, $constrainedProperty); diff --git a/src/Symfony/Component/Validator/Tests/Command/DebugCommandTest.php b/src/Symfony/Component/Validator/Tests/Command/DebugCommandTest.php index f6d1691662e3f..be1691454615d 100644 --- a/src/Symfony/Component/Validator/Tests/Command/DebugCommandTest.php +++ b/src/Symfony/Component/Validator/Tests/Command/DebugCommandTest.php @@ -15,6 +15,7 @@ use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\Validator\Command\DebugCommand; use Symfony\Component\Validator\Constraints\Email; +use Symfony\Component\Validator\Constraints\Expression; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Mapping\ClassMetadataInterface; use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface; @@ -38,6 +39,11 @@ public function testOutputWithClassArgument() ->with(DummyClassOne::class) ->willReturn($classMetadata); + $classMetadata + ->expects($this->once()) + ->method('getConstraints') + ->willReturn([new Expression('1 + 1 = 2')]); + $classMetadata ->expects($this->once()) ->method('getConstrainedProperties') @@ -68,22 +74,28 @@ public function testOutputWithClassArgument() Symfony\Component\Validator\Tests\Dummy\DummyClassOne ----------------------------------------------------- -+---------------+--------------------------------------------------+---------+------------------------------------------------------------+ -| Property | Name | Groups | Options | -+---------------+--------------------------------------------------+---------+------------------------------------------------------------+ -| firstArgument | Symfony\Component\Validator\Constraints\NotBlank | Default | [ | -| | | | "allowNull" => false, | -| | | | "message" => "This value should not be blank.", | -| | | | "normalizer" => null, | -| | | | "payload" => null | -| | | | ] | -| firstArgument | Symfony\Component\Validator\Constraints\Email | Default | [ | -| | | | "message" => "This value is not a valid email address.", | -| | | | "mode" => null, | -| | | | "normalizer" => null, | -| | | | "payload" => null | -| | | | ] | -+---------------+--------------------------------------------------+---------+------------------------------------------------------------+ ++---------------+----------------------------------------------------+---------+------------------------------------------------------------+ +| Property | Name | Groups | Options | ++---------------+----------------------------------------------------+---------+------------------------------------------------------------+ +| - | Symfony\Component\Validator\Constraints\Expression | Default | [ | +| | | | "expression" => "1 + 1 = 2", | +| | | | "message" => "This value is not valid.", | +| | | | "payload" => null, | +| | | | "values" => [] | +| | | | ] | +| firstArgument | Symfony\Component\Validator\Constraints\NotBlank | Default | [ | +| | | | "allowNull" => false, | +| | | | "message" => "This value should not be blank.", | +| | | | "normalizer" => null, | +| | | | "payload" => null | +| | | | ] | +| firstArgument | Symfony\Component\Validator\Constraints\Email | Default | [ | +| | | | "message" => "This value is not a valid email address.", | +| | | | "mode" => null, | +| | | | "normalizer" => null, | +| | | | "payload" => null | +| | | | ] | ++---------------+----------------------------------------------------+---------+------------------------------------------------------------+ TXT , $tester->getDisplay(true) @@ -108,6 +120,11 @@ public function testOutputWithPathArgument() 'firstArgument', ]); + $classMetadata + ->expects($this->exactly(2)) + ->method('getConstraints') + ->willReturn([new Expression('1 + 1 = 2')]); + $classMetadata ->method('getPropertyMetadata') ->with('firstArgument') @@ -129,42 +146,54 @@ public function testOutputWithPathArgument() Symfony\Component\Validator\Tests\Dummy\DummyClassOne ----------------------------------------------------- -+---------------+--------------------------------------------------+---------+------------------------------------------------------------+ -| Property | Name | Groups | Options | -+---------------+--------------------------------------------------+---------+------------------------------------------------------------+ -| firstArgument | Symfony\Component\Validator\Constraints\NotBlank | Default | [ | -| | | | "allowNull" => false, | -| | | | "message" => "This value should not be blank.", | -| | | | "normalizer" => null, | -| | | | "payload" => null | -| | | | ] | -| firstArgument | Symfony\Component\Validator\Constraints\Email | Default | [ | -| | | | "message" => "This value is not a valid email address.", | -| | | | "mode" => null, | -| | | | "normalizer" => null, | -| | | | "payload" => null | -| | | | ] | -+---------------+--------------------------------------------------+---------+------------------------------------------------------------+ ++---------------+----------------------------------------------------+---------+------------------------------------------------------------+ +| Property | Name | Groups | Options | ++---------------+----------------------------------------------------+---------+------------------------------------------------------------+ +| - | Symfony\Component\Validator\Constraints\Expression | Default | [ | +| | | | "expression" => "1 + 1 = 2", | +| | | | "message" => "This value is not valid.", | +| | | | "payload" => null, | +| | | | "values" => [] | +| | | | ] | +| firstArgument | Symfony\Component\Validator\Constraints\NotBlank | Default | [ | +| | | | "allowNull" => false, | +| | | | "message" => "This value should not be blank.", | +| | | | "normalizer" => null, | +| | | | "payload" => null | +| | | | ] | +| firstArgument | Symfony\Component\Validator\Constraints\Email | Default | [ | +| | | | "message" => "This value is not a valid email address.", | +| | | | "mode" => null, | +| | | | "normalizer" => null, | +| | | | "payload" => null | +| | | | ] | ++---------------+----------------------------------------------------+---------+------------------------------------------------------------+ Symfony\Component\Validator\Tests\Dummy\DummyClassTwo ----------------------------------------------------- -+---------------+--------------------------------------------------+---------+------------------------------------------------------------+ -| Property | Name | Groups | Options | -+---------------+--------------------------------------------------+---------+------------------------------------------------------------+ -| firstArgument | Symfony\Component\Validator\Constraints\NotBlank | Default | [ | -| | | | "allowNull" => false, | -| | | | "message" => "This value should not be blank.", | -| | | | "normalizer" => null, | -| | | | "payload" => null | -| | | | ] | -| firstArgument | Symfony\Component\Validator\Constraints\Email | Default | [ | -| | | | "message" => "This value is not a valid email address.", | -| | | | "mode" => null, | -| | | | "normalizer" => null, | -| | | | "payload" => null | -| | | | ] | -+---------------+--------------------------------------------------+---------+------------------------------------------------------------+ ++---------------+----------------------------------------------------+---------+------------------------------------------------------------+ +| Property | Name | Groups | Options | ++---------------+----------------------------------------------------+---------+------------------------------------------------------------+ +| - | Symfony\Component\Validator\Constraints\Expression | Default | [ | +| | | | "expression" => "1 + 1 = 2", | +| | | | "message" => "This value is not valid.", | +| | | | "payload" => null, | +| | | | "values" => [] | +| | | | ] | +| firstArgument | Symfony\Component\Validator\Constraints\NotBlank | Default | [ | +| | | | "allowNull" => false, | +| | | | "message" => "This value should not be blank.", | +| | | | "normalizer" => null, | +| | | | "payload" => null | +| | | | ] | +| firstArgument | Symfony\Component\Validator\Constraints\Email | Default | [ | +| | | | "message" => "This value is not a valid email address.", | +| | | | "mode" => null, | +| | | | "normalizer" => null, | +| | | | "payload" => null | +| | | | ] | ++---------------+----------------------------------------------------+---------+------------------------------------------------------------+ TXT , $tester->getDisplay(true) 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