Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit803e1de

Browse files
committed
rework form validator tests
1 parent3e0330f commit803e1de

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+
namespaceSymfony\Component\Form\Tests\Extension\Validator\Constraints;
4+
5+
usePHPUnit\Framework\TestCase;
6+
useSymfony\Component\Form\Extension\Core\Type\FormType;
7+
useSymfony\Component\Form\Extension\Core\Type\TextType;
8+
useSymfony\Component\Form\Extension\Validator\ValidatorExtension;
9+
useSymfony\Component\Form\FormFactoryBuilder;
10+
useSymfony\Component\Form\Test\ForwardCompatTestTrait;
11+
useSymfony\Component\Validator\Constraints\Collection;
12+
useSymfony\Component\Validator\Constraints\GroupSequence;
13+
useSymfony\Component\Validator\Constraints\NotBlank;
14+
useSymfony\Component\Validator\Validation;
15+
16+
class FormValidatorFunctionalTestextends TestCase
17+
{
18+
use ForwardCompatTestTrait;
19+
20+
private$validator;
21+
private$formFactory;
22+
23+
privatefunctiondoSetUp()
24+
{
25+
$this->validator = Validation::createValidator();
26+
$this->formFactory = (newFormFactoryBuilder())
27+
->addExtension(newValidatorExtension($this->validator))
28+
->getFormFactory();
29+
}
30+
31+
publicfunctiontestNonCompositeConstraintValidatedOnce()
32+
{
33+
$form =$this->formFactory->create(TextType::class,null, [
34+
'constraints' => [newNotBlank(['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+
publicfunctiontestCompositeConstraintValidatedInEachGroup()
47+
{
48+
$form =$this->formFactory->create(FormType::class,null, [
49+
'constraints' => [
50+
newCollection([
51+
'field1' =>newNotBlank([
52+
'groups' => ['field1'],
53+
]),
54+
'field2' =>newNotBlank([
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+
publicfunctiontestCompositeConstraintValidatedInSequence()
78+
{
79+
$form =$this->formFactory->create(FormType::class,null, [
80+
'constraints' => [
81+
newCollection([
82+
'field1' =>newNotBlank([
83+
'groups' => ['field1'],
84+
]),
85+
'field2' =>newNotBlank([
86+
'groups' => ['field2'],
87+
]),
88+
]),
89+
],
90+
'validation_groups' =>newGroupSequence(['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+
publicfunctiontestCascadeValidationToChildFormsUsingPropertyPaths()
108+
{
109+
$form =$this->formFactory->create(FormType::class,null, [
110+
'validation_groups' => ['group1','group2'],
111+
])
112+
->add('field1',null, [
113+
'constraints' => [newNotBlank(['groups' =>'group1'])],
114+
'property_path' =>'[foo]',
115+
])
116+
->add('field2',null, [
117+
'constraints' => [newNotBlank(['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+
publicfunctiontestCascadeValidationToChildFormsUsingPropertyPathsValidatedInSequence()
137+
{
138+
$form =$this->formFactory->create(FormType::class,null, [
139+
'validation_groups' =>newGroupSequence(['group1','group2']),
140+
])
141+
->add('field1',null, [
142+
'constraints' => [newNotBlank(['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-
publicfunctiontestNonCompositeConstraintValidatedOnce()
708-
{
709-
$form =$this
710-
->getBuilder('form',null, [
711-
'constraints' => [newNotBlank(['groups' => ['foo','bar']])],
712-
'validation_groups' => ['foo','bar'],
713-
])
714-
->setCompound(false)
715-
->getForm();
716-
$form->submit('');
717-
718-
$context =newExecutionContext(Validation::createValidator(),$form,newIdentityTranslator());
719-
$this->validator->initialize($context);
720-
$this->validator->validate($form,newForm());
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-
publicfunctiontestCompositeConstraintValidatedInEachGroup()
728-
{
729-
$form =$this->getBuilder('form',null, [
730-
'constraints' => [
731-
newCollection([
732-
'field1' =>newNotBlank([
733-
'groups' => ['field1'],
734-
]),
735-
'field2' =>newNotBlank([
736-
'groups' => ['field2'],
737-
]),
738-
]),
739-
],
740-
'validation_groups' => ['field1','field2'],
741-
])
742-
->setData([])
743-
->setCompound(true)
744-
->setDataMapper(newPropertyPathMapper())
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 =newExecutionContext(Validation::createValidator(),$form,newIdentityTranslator());
754-
$this->validator->initialize($context);
755-
$this->validator->validate($form,newForm());
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-
publicfunctiontestCompositeConstraintValidatedInSequence()
765-
{
766-
$form =$this->getCompoundForm([], [
767-
'constraints' => [
768-
newCollection([
769-
'field1' =>newNotBlank([
770-
'groups' => ['field1'],
771-
]),
772-
'field2' =>newNotBlank([
773-
'groups' => ['field2'],
774-
]),
775-
]),
776-
],
777-
'validation_groups' =>newGroupSequence(['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 =newExecutionContext(Validation::createValidator(),$form,newIdentityTranslator());
789-
$this->validator->initialize($context);
790-
$this->validator->validate($form,newForm());
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-
publicfunctiontestCascadeValidationToChildFormsUsingPropertyPaths()
798-
{
799-
$form =$this->getCompoundForm([], [
800-
'validation_groups' => ['group1','group2'],
801-
])
802-
->add('field1',null, [
803-
'constraints' => [newNotBlank(['groups' =>'group1'])],
804-
'property_path' =>'[foo]',
805-
])
806-
->add('field2',null, [
807-
'constraints' => [newNotBlank(['groups' =>'group2'])],
808-
'property_path' =>'[bar]',
809-
])
810-
;
811-
812-
$form->submit([
813-
'field1' =>'',
814-
'field2' =>'',
815-
]);
816-
817-
$context =newExecutionContext(Validation::createValidator(),$form,newIdentityTranslator());
818-
$this->validator->initialize($context);
819-
$this->validator->validate($form,newForm());
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-
publicfunctiontestCascadeValidationToChildFormsUsingPropertyPathsValidatedInSequence()
829-
{
830-
$form =$this->getCompoundForm([], [
831-
'validation_groups' =>newGroupSequence(['group1','group2']),
832-
])
833-
->add('field1',null, [
834-
'constraints' => [newNotBlank(['groups' =>'group1'])],
835-
'property_path' =>'[foo]',
836-
])
837-
;
838-
839-
$form->submit([
840-
'field1' =>'',
841-
]);
842-
843-
$context =newExecutionContext(Validation::createValidator(),$form,newIdentityTranslator());
844-
$this->validator->initialize($context);
845-
$this->validator->validate($form,newForm());
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
protectedfunctioncreateValidator()
853708
{
854709
returnnewFormValidator();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp