12
12
namespace Symfony \Component \Validator \Test ;
13
13
14
14
use PHPUnit \Framework \Assert ;
15
+ use PHPUnit \Framework \Constraint \IsIdentical ;
16
+ use PHPUnit \Framework \Constraint \IsInstanceOf ;
17
+ use PHPUnit \Framework \Constraint \IsNull ;
18
+ use PHPUnit \Framework \Constraint \LogicalOr ;
19
+ use PHPUnit \Framework \ExpectationFailedException ;
15
20
use PHPUnit \Framework \TestCase ;
16
21
use Symfony \Component \Validator \Constraint ;
17
22
use Symfony \Component \Validator \Constraints \NotNull ;
23
+ use Symfony \Component \Validator \Constraints \Valid ;
18
24
use Symfony \Component \Validator \ConstraintValidatorInterface ;
19
25
use Symfony \Component \Validator \ConstraintViolation ;
20
26
use Symfony \Component \Validator \Context \ExecutionContext ;
21
27
use Symfony \Component \Validator \Context \ExecutionContextInterface ;
22
28
use Symfony \Component \Validator \Mapping \ClassMetadata ;
23
29
use Symfony \Component \Validator \Mapping \PropertyMetadata ;
30
+ use Symfony \Component \Validator \Validator \ContextualValidatorInterface ;
24
31
use Symfony \Contracts \Translation \TranslatorInterface ;
25
32
26
33
/**
@@ -101,7 +108,6 @@ protected function createContext()
101
108
$ translator = $ this ->getMockBuilder (TranslatorInterface::class)->getMock ();
102
109
$ translator ->expects ($ this ->any ())->method ('trans ' )->willReturnArgument (0 );
103
110
$ validator = $ this ->getMockBuilder ('Symfony\Component\Validator\Validator\ValidatorInterface ' )->getMock ();
104
- $ contextualValidator = $ this ->getMockBuilder ('Symfony\Component\Validator\Validator\ContextualValidatorInterface ' )->getMock ();
105
111
106
112
$ context = new ExecutionContext ($ validator , $ this ->root , $ translator );
107
113
$ context ->setGroup ($ this ->group );
@@ -111,7 +117,7 @@ protected function createContext()
111
117
$ validator ->expects ($ this ->any ())
112
118
->method ('inContext ' )
113
119
->with ($ context )
114
- ->willReturn ($ contextualValidator );
120
+ ->willReturn (new AssertingContextualValidator () );
115
121
116
122
return $ context ;
117
123
}
@@ -164,36 +170,26 @@ protected function setPropertyPath($propertyPath)
164
170
protected function expectNoValidate ()
165
171
{
166
172
$ validator = $ this ->context ->getValidator ()->inContext ($ this ->context );
167
- $ validator ->expects ($ this ->never ())
168
- ->method ('atPath ' );
169
- $ validator ->expects ($ this ->never ())
170
- ->method ('validate ' );
173
+ $ validator ->expectNoValidate ();
171
174
}
172
175
173
176
protected function expectValidateAt ($ i , $ propertyPath , $ value , $ group )
174
177
{
175
178
$ validator = $ this ->context ->getValidator ()->inContext ($ this ->context );
176
- $ validator ->expects ($ this ->at (2 * $ i ))
177
- ->method ('atPath ' )
178
- ->with ($ propertyPath )
179
- ->willReturn ($ validator );
180
- $ validator ->expects ($ this ->at (2 * $ i + 1 ))
181
- ->method ('validate ' )
182
- ->with ($ value , $ this ->logicalOr (null , [], $ this ->isInstanceOf ('\Symfony\Component\Validator\Constraints\Valid ' )), $ group )
183
- ->willReturn ($ validator );
179
+ $ validator ->expectValidation ($ i , $ propertyPath , $ value , $ group , function ($ passedConstraints ) {
180
+ $ expectedConstraints = new LogicalOr ();
181
+ $ expectedConstraints ->setConstraints ([new IsNull (), new IsIdentical ([]), new IsInstanceOf (Valid::class)]);
182
+
183
+ Assert::assertThat ($ passedConstraints , $ expectedConstraints );
184
+ });
184
185
}
185
186
186
187
protected function expectValidateValueAt ($ i , $ propertyPath , $ value , $ constraints , $ group = null )
187
188
{
188
189
$ contextualValidator = $ this ->context ->getValidator ()->inContext ($ this ->context );
189
- $ contextualValidator ->expects ($ this ->at (2 * $ i ))
190
- ->method ('atPath ' )
191
- ->with ($ propertyPath )
192
- ->willReturn ($ contextualValidator );
193
- $ contextualValidator ->expects ($ this ->at (2 * $ i + 1 ))
194
- ->method ('validate ' )
195
- ->with ($ value , $ constraints , $ group )
196
- ->willReturn ($ contextualValidator );
190
+ $ contextualValidator ->expectValidation ($ i , $ propertyPath , $ value , $ group , function ($ passedConstraints ) use ($ constraints ) {
191
+ Assert::assertEquals ($ constraints , $ passedConstraints );
192
+ });
197
193
}
198
194
199
195
protected function assertNoViolation ()
@@ -346,3 +342,63 @@ private function getViolation(): ConstraintViolation
346
342
);
347
343
}
348
344
}
345
+
346
+ class AssertingContextualValidator implements ContextualValidatorInterface
347
+ {
348
+ private $ expectNoValidate = false ;
349
+ private $ atPathCalls = -1 ;
350
+ private $ expectedAtPath = [];
351
+ private $ validateCalls = -1 ;
352
+ private $ expectedValidate = [];
353
+
354
+ public function atPath ($ path )
355
+ {
356
+ Assert::assertFalse ($ this ->expectNoValidate , 'No validation calls have been expected. ' );
357
+
358
+ if (!isset ($ this ->expectedAtPath [++$ this ->atPathCalls ])) {
359
+ throw new ExpectationFailedException (sprintf ('Validation for property path "%s" was not expected. ' , $ path ));
360
+ }
361
+
362
+ Assert::assertSame ($ this ->expectedAtPath [$ this ->atPathCalls ], $ path );
363
+
364
+ return $ this ;
365
+ }
366
+
367
+ public function validate ($ value , $ constraints = null , $ groups = null )
368
+ {
369
+ Assert::assertFalse ($ this ->expectNoValidate , 'No validation calls have been expected. ' );
370
+
371
+ list ($ expectedValue , $ expectedGroup , $ expectedConstraints ) = $ this ->expectedValidate [++$ this ->validateCalls ];
372
+
373
+ Assert::assertSame ($ expectedValue , $ value );
374
+ $ expectedConstraints ($ constraints );
375
+ Assert::assertSame ($ expectedGroup , $ groups );
376
+
377
+ return $ this ;
378
+ }
379
+
380
+ public function validateProperty ($ object , $ propertyName , $ groups = null )
381
+ {
382
+ return $ this ;
383
+ }
384
+
385
+ public function validatePropertyValue ($ objectOrClass , $ propertyName , $ value , $ groups = null )
386
+ {
387
+ return $ this ;
388
+ }
389
+
390
+ public function getViolations ()
391
+ {
392
+ }
393
+
394
+ public function expectNoValidate ()
395
+ {
396
+ $ this ->expectNoValidate = true ;
397
+ }
398
+
399
+ public function expectValidation ($ call , $ propertyPath , $ value , $ group , $ constraints )
400
+ {
401
+ $ this ->expectedAtPath [$ call ] = $ propertyPath ;
402
+ $ this ->expectedValidate [$ call ] = [$ value , $ group , $ constraints ];
403
+ }
404
+ }
0 commit comments