|
19 | 19 | use Symfony\Component\Validator\Constraints\Email;
|
20 | 20 | use Symfony\Component\Validator\Constraints\GroupSequence;
|
21 | 21 | use Symfony\Component\Validator\Constraints\Length;
|
| 22 | +use Symfony\Component\Validator\Constraints\NotBlank; |
22 | 23 | use Symfony\Component\Validator\Constraints\Valid;
|
23 | 24 | use Symfony\Component\Validator\ConstraintViolationList;
|
24 | 25 | use Symfony\Component\Validator\Validation;
|
@@ -74,6 +75,132 @@ public function testGroupSequenceWithConstraintsOption()
|
74 | 75 | $this->assertCount(1, $form->getErrors(true));
|
75 | 76 | }
|
76 | 77 |
|
| 78 | + /** |
| 79 | + * @dataProvider provideGroupsSequenceAndResultData |
| 80 | + */ |
| 81 | + public function testGroupSequenceWithConstraintsOptionMatrix( |
| 82 | + array $groups, |
| 83 | + array $sequence, |
| 84 | + $errorCount, |
| 85 | + array $propertyPaths |
| 86 | + ) { |
| 87 | + $form = Forms::createFormFactoryBuilder() |
| 88 | + ->addExtension(new ValidatorExtension(Validation::createValidator())) |
| 89 | + ->getFormFactory() |
| 90 | + ->create(FormTypeTest::TESTED_TYPE, null, ([ |
| 91 | + 'validation_groups' => new GroupSequence($sequence), |
| 92 | + ])); |
| 93 | + |
| 94 | + $data = []; |
| 95 | + foreach ($groups as $fieldName => $fieldGroups) { |
| 96 | + $form = $form->add( |
| 97 | + $fieldName, TextTypeTest:: |
| 98 | + TESTED_TYPE, |
| 99 | + [ |
| 100 | + 'constraints' => [new NotBlank(['groups' => $fieldGroups])], |
| 101 | + ]); |
| 102 | + |
| 103 | + $data[$fieldName] = ''; |
| 104 | + } |
| 105 | + |
| 106 | + $form->submit($data); |
| 107 | + |
| 108 | + $errors = $form->getErrors(true); |
| 109 | + $this->assertCount($errorCount, $form->getErrors(true)); |
| 110 | + |
| 111 | + foreach ($errors as $i => $error) { |
| 112 | + $this->assertEquals('children['.$propertyPaths[$i].'].data', $error->getCause()->getPropertyPath()); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public function provideGroupsSequenceAndResultData() |
| 117 | + { |
| 118 | + return [ |
| 119 | + // two fields (sequence of groups and group order): |
| 120 | + [ |
| 121 | + 'groups' => [ |
| 122 | + 'field1' => ['First'], |
| 123 | + 'field2' => ['Second'], |
| 124 | + ], |
| 125 | + 'sequence' => ['First'], |
| 126 | + 'errors' => 1, |
| 127 | + 'propertyPaths' => ['field1'], |
| 128 | + ], |
| 129 | + [ |
| 130 | + 'groups' => [ |
| 131 | + 'field1' => ['First'], |
| 132 | + 'field2' => ['Second'], |
| 133 | + ], |
| 134 | + 'sequence' => ['Second'], |
| 135 | + 'errors' => 1, |
| 136 | + 'propertyPaths' => ['field2'], |
| 137 | + ], |
| 138 | + [ |
| 139 | + 'groups' => [ |
| 140 | + 'field1' => ['First'], |
| 141 | + 'field2' => ['Second'], |
| 142 | + ], |
| 143 | + 'sequence' => ['First', 'Second'], |
| 144 | + 'errors' => 1, |
| 145 | + 'propertyPaths' => ['field1'], |
| 146 | + ], |
| 147 | + [ |
| 148 | + 'groups' => [ |
| 149 | + 'field1' => ['First'], |
| 150 | + 'field2' => ['Second'], |
| 151 | + ], |
| 152 | + 'sequence' => ['Second', 'First'], |
| 153 | + 'errors' => 1, |
| 154 | + 'propertyPaths' => ['field2'], |
| 155 | + ], |
| 156 | + |
| 157 | + // two fields (field with sequence of groups) |
| 158 | + [ |
| 159 | + 'groups' => [ |
| 160 | + 'field1' => ['First'], |
| 161 | + 'field2' => ['Second', 'First'], |
| 162 | + ], |
| 163 | + 'sequence' => ['First'], |
| 164 | + 'errors' => 2, |
| 165 | + 'propertyPaths' => ['field1', 'field2'], |
| 166 | + ], |
| 167 | + |
| 168 | + // three fields (sequence with multigroup) |
| 169 | + [ |
| 170 | + 'groups' => [ |
| 171 | + 'field1' => ['First'], |
| 172 | + 'field2' => ['Second'], |
| 173 | + 'field3' => ['Third'], |
| 174 | + ], |
| 175 | + 'sequence' => [['First', 'Second'], 'Third'], |
| 176 | + 'errors' => 2, |
| 177 | + 'propertyPaths' => ['field1', 'field2'], |
| 178 | + ], |
| 179 | + [ |
| 180 | + 'groups' => [ |
| 181 | + 'field1' => ['First'], |
| 182 | + 'field2' => ['Second'], |
| 183 | + 'field3' => ['Third'], |
| 184 | + ], |
| 185 | + 'sequence' => ['First', ['Second', 'Third']], |
| 186 | + 'errors' => 1, |
| 187 | + 'propertyPaths' => ['field1'], |
| 188 | + ], |
| 189 | + |
| 190 | + // three fields (field with sequence of groups) |
| 191 | + [ |
| 192 | + 'groups' => [ |
| 193 | + 'field1' => ['First'], |
| 194 | + 'field2' => ['Second'], |
| 195 | + 'field3' => ['Third', 'Second'], |
| 196 | + ], |
| 197 | + 'sequence' => [['First', 'Second'], 'Third'], |
| 198 | + 'errors' => 3, |
| 199 | + 'propertyPaths' => ['field1', 'field2', 'field3'], |
| 200 | + ], |
| 201 | + ]; |
| 202 | + } |
| 203 | + |
77 | 204 | protected function createForm(array $options = [])
|
78 | 205 | {
|
79 | 206 | return $this->factory->create(FormTypeTest::TESTED_TYPE, null, $options);
|
|
0 commit comments