8000 rework form validator tests · symfony/symfony@803e1de · GitHub
[go: up one dir, main page]

Skip to content

Commit 803e1de

Browse files
committed
rework form validator tests
1 parent 3e0330f commit 803e1de

File tree

2 files changed

+157
-145
lines changed

2 files changed

+157
-145
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
3+
namespace Symfony\Component\Form\Tests\Extension\Validator\Constraints;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Form\Extension\Core\Type\FormType;
7+
use Symfony\Component\Form\Extension\Core\Type\TextType;
8+
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
9+
use Symfony\Component\Form\FormFactoryBuilder;
10+
use Symfony\Component\Form\Test\ForwardCompatTestTrait;
11+
use Symfony\Component\Validator\Constraints\Collection;
12+
use Symfony\Component\Validator\Constraints\GroupSequence;
13+
use Symfony\Component\Validator\Constraints\NotBlank;
14+
use Symfony\Component\Validator\Validation;
15+
16+
class FormValidatorFunctionalTest extends TestCase
17+
{
18+
use ForwardCompatTestTrait;
19+
20+
private $validator;
21+
private $formFactory;
22+
23+
private function doSetUp()
24+
{
25+
$this->validator = Validation::createValidator();
26+
$this->formFactory = (new FormFactoryBuilder())
27+
->addExtension(new ValidatorExtension($this->validator))
28+
->getFormFactory();
29+
}
30+
31+
public function testNonCompositeConstraintValidatedOnce()
32+
{
33+
$form = $this->formFactory->create(TextType::class, null, [
34+
'constraints' => [new NotBlank(['groups' => ['foo', 'bar']])],
35+
'validation_groups' => ['foo', 'bar'],
36+
]);
37+
$form->submit('');
38+
39+
$violations = $this->validator->validate($form);
40+
41+
$this->assertCount(1, $violations);
42+
$this->assertSame('This value should not be blank.', $violations[0]->getMessage());
43+
$this->assertSame('data', $violations[0]->getPropertyPath());
44+
}
45+
46+
public function testCompositeConstraintValidatedInEachGroup()
47+
{
48+
$form = $this->formFactory->create(FormType::class, null, [
49+
'constraints' => [
50+
new Collection([
51+
'field1' => new NotBlank([
52+
'groups' => ['field1'],
53+
]),
54+
'field2' => new NotBlank([
55+
'groups' => ['field2'],
56+
]),
57+
]),
58+
],
59+
'validation_groups' => ['field1', 'field2'],
60+
]);
61+
$form->add('field1');
62+
$form->add('field2');
63+
$form->submit([
64+
'field1' => '',
65+
'field2' => '',
66+
]);
67+
68+
$violations = $this->validator->validate($form);
69+
70+
$this->assertCount(2, $violations);
71+
$this->assertSame('This value should not be blank.', $violations[0]->getMessage());
72+
$this->assertSame('data[field1]', $violations[0]->getPropertyPath());
73+
$this->assertSame('This value should not be blank.', $violations[1]->getMessage());
74+
$this->assertSame('data[field2]', $violations[1]->getPropertyPath());
75+
}
76+
77+
public function testCompositeConstraintValidatedInSequence()
78+
{
79+
$form = $this->formFactory->create(FormType::class, null, [
80+
'constraints' => [
81+
new Collection([
82+
'field1' => new NotBlank([
83+
'groups' => ['field1'],
84+
]),
85+
'field2' => new NotBlank([
86+
'groups' => ['field2'],
87+
]),
88+
]),
89+
],
90+
'validation_groups' => new GroupSequence(['field1', 'field2']),
91+
]);
92+
$form->add('field1');
93+
$form->add('field2');
94+
95+
$form->submit([
96+
'field1' => '',
97+
'field2' => '',
98+
]);
99+
100+
$violations = $this->validator->validate($form);
101+
102+
$this->assertCount(1, $violations);
103+
$this->assertSame('This value should not be blank.', $violations[0]->getMessage());
104+
$this->assertSame('data[field1]', $violations[0]->getPropertyPath());
105+
}
106+
107+
public function testCascadeValidationToChildFormsUsingPropertyPaths()
108+
{
109+
$form = $this->formFactory->create(FormType::class, null, [
110+
'validation_groups' => ['group1', 'group2'],
111+
])
112+
->add('field1', null, [
113+
'constraints' => [new NotBlank(['groups' => 'group1'])],
114+
'property_path' => '[foo]',
115+
])
116+
->add('field2', null, [
117+
'constraints' => [new NotBlank(['groups' => 'group2'])],
118+
'property_path' => '[bar]',
119+
])
120+
;
121+
122+
$form->submit([
123+
'field1' => '',
124+
'field2' => '',
125+
]);
126+
127+
$violations = $this->validator->validate($form);
128+
129+
$this->assertCount(2, $violations);
130+
$this->assertSame('This value should not be blank.', $violations[0]->getMessage());
131+
$this->assertSame('children[field1].data', $violations[0]->getPropertyPath());
132+
$this->assertSame('This value should not be blank.', $violations[1]->getMessage());
133+
$this->assertSame('children[field2].data', $violations[1]->getPropertyPath());
134+
}
135+
136+
public function testCascadeValidationToChildFormsUsingPropertyPathsValidatedInSequence()
137+
{
138+
$form = $this->formFactory->create(FormType::class, null, [
139+
'validation_groups' => new GroupSequence(['group1', 'group2']),
140+
])
141+
->add('field1', null, [
142+
'constraints' => [new NotBlank(['groups' => 'group1'])],
143+
'property_path' => '[foo]',
144+
])
145+
;
146+
147+
$form->submit([
148+
'field1' => '',
149+
]);
150+
151+
$violations = $this->validator->validate($form);
152+
153+
$this->assertCount(1, $violations);
154+
$this->assertSame('This value should not be blank.', $violations[0]->getMessage());
155+
$this->assertSame('children[field1].data', $violations[0]->getPropertyPath());
156+
}
157+
}

src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php

Lines changed: 0 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -704,151 +704,6 @@ public function testCauseForNotAllowedExtraFieldsIsTheFormConstraint()
704704
$this->assertSame($constraint, $context->getViolations()->get(0)->getConstraint());
705705
}
706706

707-
public function testNonCompositeConstraintValidatedOnce()
708-
{
709-
$form = $this
710-
->getBuilder('form', null, [
711-
'constraints' => [new NotBlank(['groups' => ['foo', 'bar']])],
712-
'validation_groups' => ['foo', 'bar'],
713-
])
714-
->setCompound(false)
715-
->getForm();
716-
$form->submit('');
717-
718-
$context = new ExecutionContext(Validation::createValidator(), $form, new IdentityTranslator());
719-
$this->validator->initialize($context);
720-
$this->validator->validate($form, new Form());
721-
722-
$this->assertCount(1, $context->getViolations());
723-
$this->assertSame('This value should not be blank.', $context->getViolations()[0]->getMessage());
724-
$this->assertSame('data', $context->getViolations()[0]->getPropertyPath());
725-
}
726-
727-
public function testCompositeConstraintValidatedInEachGroup()
728-
{
729-
$form = $this->getBuilder('form', null, [
730-
'constraints' => [
731-
new Collection([
732-
'field1' => new NotBlank([
733-
'groups' => ['field1'],
734-
]),
735-
'field2' => new NotBlank([
736-
'groups' => ['field2'],
737-
]),
738-
]),
739-
],
740-
'validation_groups' => ['field1', 'field2'],
741-
])
742-
->setData([])
743-
->setCompound(true)
744-
->setDataMapper(new PropertyPathMapper())
745-
->getForm();
746-
$form->add($this->getForm('field1'));
747-
$form->add($this->getForm('field2'));
748-
$form->submit([
749-
'field1' => '',
750-
'field2' => '',
751-
]);
752-
753-
$context = new ExecutionContext(Validation::createValidator(), $form, new IdentityTranslator());
754-
$this->validator->initialize($context);
755-
$this->validator->validate($form, new Form());
756-
757-
$this->assertCount(2, $context->getViolations());
758-
$this->assertSame('This value should not be blank.', $context->getViolations()[0]->getMessage());
759-
$this->assertSame('data[field1]', $context->getViolations()[0]->getPropertyPath());
760-
$this->assertSame('This value should not be blank.', $context->getViolations()[1]->getMessage());
761-
$this->assertSame('data[field2]', $context->getViolations()[1]->getPropertyPath());
762-
}
763-
764-
public function testCompositeConstraintValidatedInSequence()
765-
{
766-
$form = $this->getCompoundForm([], [
767-
'constraints' => [
768-
new Collection([
769-
'field1' => new NotBlank([
770-
'groups' => ['field1'],
771-
]),
772-
'field2' => new NotBlank([
773-
'groups' => ['field2'],
774-
]),
775-
]),
776-
],
777-
'validation_groups' => new GroupSequence(['field1', 'field2']),
778-
])
779-
->add($this->getForm('field1'))
780-
->add($this->getForm('field2'))
781-
;
782-
783-
$form->submit([
784-
'field1' => '',
785-
'field2' => '',
786-
]);
787-
788-
$context = new ExecutionContext(Validation::createValidator(), $form, new IdentityTranslator());
789-
$this->validator->initialize($context);
790-
$this->validator->validate($form, new Form());
791-
792-
$this->assertCount(1, $context->getViolations());
793-
$this->assertSame('This value should not be blank.', $context->getViolations()[0]->getMessage());
794-
$this->assertSame('data[field1]', $context->getViolations()[0]->getPropertyPath());
795-
}
796-
797-
public function testCascadeValidationToChildFormsUsingPropertyPaths()
798-
{
799-
$form = $this->getCompoundForm([], [
800-
'validation_groups' => ['group1', 'group2'],
801-
])
802-
->add('field1', null, [
803-
'constraints' => [new NotBlank(['groups' => 'group1'])],
804-
'property_path' => '[foo]',
805-
])
806-
->add('field2', null, [
807-
'constraints' => [new NotBlank(['groups' => 'group2'])],
808-
'property_path' => '[bar]',
809-
])
810-
;
811-
812-
$form->submit([
813-
'field1' => '',
814-
'field2' => '',
815-
]);
816-
817-
$context = new ExecutionContext(Validation::createValidator(), $form, new IdentityTranslator());
818-
$this->validator->initialize($context);
819-
$this->validator->validate($form, new Form());
820-
821-
$this->assertCount(2, $context->getViolations());
822-
$this->assertSame('This value should not be blank.', $context->getViolations()[0]->getMessage());
823-
$this->assertSame('children[field1].data', $context->getViolations()[0]->getPropertyPath());
824-
$this->assertSame('This value should not be blank.', $context->getViolations()[1]->getMessage());
825-
$this->assertSame('children[field2].data', $context->getViolations()[1]->getPropertyPath());
826-
}
827-
828-
public function testCascadeValidationToChildFormsUsingPropertyPathsValidatedInSequence()
829-
{
830-
$form = $this->getCompoundForm([], [
831-
'validation_groups' => new GroupSequence(['group1', 'group2']),
832-
])
833-
->add('field1', null, [
834-
'constraints' => [new NotBlank(['groups' => 'group1'])],
835-
'property_path' => '[foo]',
836-
])
837-
;
838-
839-
$form->submit([
840-
'field1' => '',
841-
]);
842-
843-
$context = new ExecutionContext(Validation::createValidator(), $form, new IdentityTranslator());
844-
$this->validator->initialize($context);
845-
$this->validator->validate($form, new Form());
846-
847-
$this->assertCount(1, $context->getViolations());
848-
$this->assertSame('This value should not be blank.', $context->getViolations()[0]->getMessage());
849-
$this->assertSame('children[field1].data', $context->getViolations()[0]->getPropertyPath());
850-
}
851-
852707
protected function createValidator()
853708
{
854709
return new FormValidator();

0 commit comments

Comments
 (0)
0