Skip to content

Fix getting class constraints on debug command #46545

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 1 commit into from
Jun 9, 2022
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
30 changes: 25 additions & 5 deletions src/Symfony/Component/Validator/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
125 changes: 77 additions & 48 deletions src/Symfony/Component/Validator/Tests/Command/DebugCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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')
Expand Down Expand Up @@ -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)
Expand All @@ -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')
Expand All @@ -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)
Expand Down