|
12 | 12 | namespace Symfony\Component\Form\Tests\Extension\Validator\Type;
|
13 | 13 |
|
14 | 14 | use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
|
| 15 | +use Symfony\Component\Form\Form; |
15 | 16 | use Symfony\Component\Form\Forms;
|
16 | 17 | use Symfony\Component\Form\Test\Traits\ValidatorExtensionTrait;
|
17 | 18 | use Symfony\Component\Form\Tests\Extension\Core\Type\FormTypeTest;
|
18 | 19 | use Symfony\Component\Form\Tests\Extension\Core\Type\TextTypeTest;
|
| 20 | +use Symfony\Component\Form\Tests\Fixtures\Author; |
19 | 21 | use Symfony\Component\Validator\Constraints\GroupSequence;
|
20 | 22 | use Symfony\Component\Validator\Constraints\Length;
|
21 | 23 | use Symfony\Component\Validator\Constraints\NotBlank;
|
| 24 | +use Symfony\Component\Validator\Constraints\NotNull; |
22 | 25 | use Symfony\Component\Validator\Constraints\Valid;
|
23 | 26 | use Symfony\Component\Validator\ConstraintViolationList;
|
| 27 | +use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 28 | +use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface; |
24 | 29 | use Symfony\Component\Validator\Validation;
|
25 | 30 |
|
26 | 31 | class FormTypeValidatorExtensionTest extends BaseValidatorExtensionTest
|
@@ -79,23 +84,48 @@ public function testGroupSequenceWithConstraintsOption()
|
79 | 84 |
|
80 | 85 | public function testManyFieldsGroupSequenceWithConstraintsOption()
|
81 | 86 | {
|
| 87 | + $formMetadata = new ClassMetadata(Form::class); |
| 88 | + $authorMetadata = (new ClassMetadata(Author::class)) |
| 89 | + ->addPropertyConstraint('firstName', new NotNull(['groups' => 'Second'])) |
| 90 | + ; |
| 91 | + $metadataFactory = $this->createMock(MetadataFactoryInterface::class); |
| 92 | + $metadataFactory->expects($this->any()) |
| 93 | + ->method('getMetadataFor') |
| 94 | + ->willReturnCallback(static function ($classOrObject) use ($formMetadata, $authorMetadata) { |
| 95 | + if (Author::class === $classOrObject || $classOrObject instanceof Author) { |
| 96 | + return $authorMetadata; |
| 97 | + } |
| 98 | + |
| 99 | + if (Form::class === $classOrObject || $classOrObject instanceof Form) { |
| 100 | + return $formMetadata; |
| 101 | + } |
| 102 | + |
| 103 | + return new ClassMetadata(\is_string($classOrObject) ? $classOrObject : \get_class($classOrObject)); |
| 104 | + }) |
| 105 | + ; |
| 106 | + |
| 107 | + $validator = Validation::createValidatorBuilder() |
| 108 | + ->setMetadataFactory($metadataFactory) |
| 109 | + ->getValidator() |
| 110 | + ; |
82 | 111 | $form = Forms::createFormFactoryBuilder()
|
83 |
| - ->addExtension(new ValidatorExtension(Validation::createValidator())) |
| 112 | + ->addExtension(new ValidatorExtension($validator)) |
84 | 113 | ->getFormFactory()
|
85 |
| - ->create(FormTypeTest::TESTED_TYPE, null, (['validation_groups' => new GroupSequence(['First', 'Second'])])) |
86 |
| - ->add('field1', TextTypeTest::TESTED_TYPE, [ |
| 114 | + ->create(FormTypeTest::TESTED_TYPE, new Author(), (['validation_groups' => new GroupSequence(['First', 'Second'])])) |
| 115 | + ->add('firstName', TextTypeTest::TESTED_TYPE) |
| 116 | + ->add('lastName', TextTypeTest::TESTED_TYPE, [ |
87 | 117 | 'constraints' => [
|
88 | 118 | new Length(['min' => 10, 'groups' => ['First']]),
|
89 | 119 | ],
|
90 | 120 | ])
|
91 |
| - ->add('field2', TextTypeTest::TESTED_TYPE, [ |
| 121 | + ->add('australian', TextTypeTest::TESTED_TYPE, [ |
92 | 122 | 'constraints' => [
|
93 | 123 | new NotBlank(['groups' => ['Second']]),
|
94 | 124 | ],
|
95 | 125 | ])
|
96 | 126 | ;
|
97 | 127 |
|
98 |
| - $form->submit(['field1' => 'wrong_1', 'field2' => '']); |
| 128 | + $form->submit(['firstName' => '', 'lastName' => 'wrong_1', 'australian' => '']); |
99 | 129 |
|
100 | 130 | $errors = $form->getErrors(true);
|
101 | 131 |
|
|
0 commit comments