18
18
use Symfony \Component \Validator \Constraints \Country ;
19
19
use Symfony \Component \Validator \Constraints \DivisibleBy ;
20
20
use Symfony \Component \Validator \Constraints \EqualTo ;
21
+ use Symfony \Component \Validator \Constraints \Expression ;
21
22
use Symfony \Component \Validator \Constraints \GreaterThanOrEqual ;
22
23
use Symfony \Component \Validator \Constraints \IdenticalTo ;
23
24
use Symfony \Component \Validator \Constraints \Language ;
24
25
use Symfony \Component \Validator \Constraints \Length ;
25
26
use Symfony \Component \Validator \Constraints \LessThan ;
26
27
use Symfony \Component \Validator \Constraints \Negative ;
28
+ use Symfony \Component \Validator \Constraints \NotNull ;
27
29
use Symfony \Component \Validator \Constraints \Range ;
28
30
use Symfony \Component \Validator \Constraints \Regex ;
29
31
use Symfony \Component \Validator \Constraints \Unique ;
32
+ use Symfony \Component \Validator \Constraints \Valid ;
33
+ use Symfony \Component \Validator \ConstraintViolation ;
34
+ use Symfony \Component \Validator \Mapping \ClassMetadata ;
35
+ use Symfony \Component \Validator \Mapping \Factory \MetadataFactoryInterface ;
36
+ use Symfony \Component \Validator \Mapping \MetadataInterface ;
30
37
use Symfony \Component \Validator \Test \ConstraintValidatorTestCase ;
31
38
use Symfony \Component \Validator \Validation ;
32
39
@@ -45,15 +52,7 @@ protected function createValidator()
45
52
*/
46
53
public function testValidCombinations ($ value , $ constraints )
47
54
{
48
- $ i = 0 ;
49
-
50
- foreach ($ constraints as $ constraint ) {
51
- $ this ->expectViolationsAt ($ i ++, $ value , $ constraint );
52
- }
53
-
54
- $ this ->validator ->validate ($ value , new AtLeastOneOf ($ constraints ));
55
-
56
- $ this ->assertNoViolation ();
55
+ $ this ->assertCount (0 , Validation::createValidator ()->validate ($ value , new AtLeastOneOf ($ constraints )));
57
56
}
58
57
59
58
public function getValidCombinations ()
@@ -96,18 +95,20 @@ public function getValidCombinations()
96
95
public function testInvalidCombinationsWithDefaultMessage ($ value , $ constraints )
97
96
{
98
97
$ atLeastOneOf = new AtLeastOneOf (['constraints ' => $ constraints ]);
98
+ $ validator = Validation::createValidator ();
99
99
100
100
$ message = [$ atLeastOneOf ->message ];
101
101
102
102
$ i = 0 ;
103
103
104
104
foreach ($ constraints as $ constraint ) {
105
- $ message [] = ' [ ' .( $ i + 1 ). ' ] ' . $ this -> expectViolationsAt ( $ i ++, $ value , $ constraint )->get (0 )->getMessage ();
105
+ $ message [] = sprintf ( ' [%d] %s ' , ++ $ i , $ validator -> validate ( $ value , $ constraint )->get (0 )->getMessage () );
106
106
}
107
107
108
- $ this -> validator ->validate ($ value , $ atLeastOneOf );
108
+ $ violations = $ validator ->validate ($ value , $ atLeastOneOf );
109
109
110
- $ this ->buildViolation (implode ('' , $ message ))->setCode (AtLeastOneOf::AT_LEAST_ONE_OF_ERROR )->assertRaised ();
110
+ $ this ->assertCount (1 , $ violations , sprintf ('1 violation expected. Got %u. ' , \count ($ violations )));
111
+ $ this ->assertEquals (new ConstraintViolation (implode ('' , $ message ), implode ('' , $ message ), [], $ value , '' , $ value , null , AtLeastOneOf::AT_LEAST_ONE_OF_ERROR , $ atLeastOneOf ), $ violations ->get (0 ));
111
112
}
112
113
113
114
/**
@@ -117,15 +118,10 @@ public function testInvalidCombinationsWithCustomMessage($value, $constraints)
117
118
{
118
119
$ atLeastOneOf = new AtLeastOneOf (['constraints ' => $ constraints , 'message ' => 'foo ' , 'includeInternalMessages ' => false ]);
119
120
120
- $ i = 0 ;
121
-
122
- foreach ($ constraints as $ constraint ) {
123
- $ this ->expectViolationsAt ($ i ++, $ value , $ constraint );
124
- }
121
+ $ violations = Validation::createValidator ()->validate ($ value , $ atLeastOneOf );
125
122
126
- $ this ->validator ->validate ($ value , $ atLeastOneOf );
127
-
128
- $ this ->buildViolation ('foo ' )->setCode (AtLeastOneOf::AT_LEAST_ONE_OF_ERROR )->assertRaised ();
123
+ $ this ->assertCount (1 , $ violations , sprintf ('1 violation expected. Got %u. ' , \count ($ violations )));
124
+ $ this ->assertEquals (new ConstraintViolation ('foo ' , 'foo ' , [], $ value , '' , $ value , null , AtLeastOneOf::AT_LEAST_ONE_OF_ERROR , $ atLeastOneOf ), $ violations ->get (0 ));
129
125
}
130
126
131
127
public function getInvalidCombinations ()
@@ -184,4 +180,40 @@ public function testGroupsArePropagatedToNestedConstraints()
184
180
185
181
$ this ->assertCount (1 , $ violations );
186
182
}
183
+
184
+ public function testContextIsPropagatedToNestedConstraints ()
185
+ {
186
+ $ validator = Validation::createValidatorBuilder ()
187
+ ->setMetadataFactory (new class () implements MetadataFactoryInterface {
188
+ public function getMetadataFor ($ classOrObject ): MetadataInterface
189
+ {
190
+ return (new ClassMetadata (ExpressionConstraintNested::class))
191
+ ->addPropertyConstraint ('foo ' , new AtLeastOneOf ([
192
+ new NotNull (),
193
+ new Expression ('this.getFoobar() in ["bar", "baz"] ' ),
194
+ ]));
195
+ }
196
+
197
+ public function hasMetadataFor ($ classOrObject ): bool
198
+ {
199
+ return ExpressionConstraintNested::class === $ classOrObject ;
200
+ }
201
+ })
202
+ ->getValidator ()
203
+ ;
204
+
205
+ $ violations = $ validator ->validate (new ExpressionConstraintNested (), new Valid ());
206
+
207
+ $ this ->assertCount (0 , $ violations );
208
+ }
209
+ }
210
+
211
+ class ExpressionConstraintNested
212
+ {
213
+ private $ foo ;
214
+
215
+ public function getFoobar (): string
216
+ {
217
+ return 'bar ' ;
218
+ }
187
219
}
0 commit comments