23
23
use Symfony \Component \Validator \Constraints \Valid ;
24
24
use Symfony \Component \Validator \ConstraintValidatorInterface ;
25
25
use Symfony \Component \Validator \ConstraintViolation ;
26
+ use Symfony \Component \Validator \ConstraintViolationInterface ;
26
27
use Symfony \Component \Validator \Context \ExecutionContext ;
27
28
use Symfony \Component \Validator \Context \ExecutionContextInterface ;
28
29
use Symfony \Component \Validator \Mapping \ClassMetadata ;
@@ -115,7 +116,7 @@ protected function createContext()
115
116
$ validator ->expects ($ this ->any ())
116
117
->method ('inContext ' )
117
118
->with ($ context )
118
- ->willReturn ($ this ->getMockBuilder (AssertingContextualValidator::class)->setMethods (null )->getMock ());
119
+ ->willReturn ($ this ->getMockBuilder (AssertingContextualValidator::class)->setConstructorArgs ([ $ context ])-> setMethods (null )->getMock ());
119
120
120
121
return $ context ;
121
122
}
@@ -185,10 +186,25 @@ protected function expectValidateAt($i, $propertyPath, $value, $group)
185
186
protected function expectValidateValue (int $ i , $ value , array $ constraints = [], $ group = null )
186
187
{
187
188
$ contextualValidator = $ this ->context ->getValidator ()->inContext ($ this ->context );
188
- $ contextualValidator ->expects ($ this ->at ($ i ))
189
- ->method ('validate ' )
190
- ->with ($ value , $ constraints , $ group )
191
- ->willReturn ($ contextualValidator );
189
+ $ contextualValidator ->expectValidation ($ i , '' , $ value , $ group , function ($ passedConstraints ) use ($ constraints ) {
190
+ if (\is_array ($ constraints ) && !\is_array ($ passedConstraints )) {
191
+ $ passedConstraints = [$ passedConstraints ];
192
+ }
193
+
194
+ Assert::assertEquals ($ constraints , $ passedConstraints );
195
+ });
196
+ }
197
+
198
+ protected function expectFailingValueValidation (int $ i , $ value , array $ constraints , $ group , ConstraintViolationInterface $ violation )
199
+ {
200
+ $ contextualValidator = $ this ->context ->getValidator ()->inContext ($ this ->context );
201
+ $ contextualValidator ->expectValidation ($ i , '' , $ value , $ group , function ($ passedConstraints ) use ($ constraints ) {
202
+ if (\is_array ($ constraints ) && !\is_array ($ passedConstraints )) {
203
+ $ passedConstraints = [$ passedConstraints ];
204
+ }
205
+
206
+ Assert::assertEquals ($ constraints , $ passedConstraints );
207
+ }, $ violation );
192
208
}
193
209
194
210
protected function expectValidateValueAt ($ i , $ propertyPath , $ value , $ constraints , $ group = null )
@@ -371,12 +387,18 @@ private function getViolation(): ConstraintViolation
371
387
372
388
class AssertingContextualValidator implements ContextualValidatorInterface
373
389
{
390
+ private $ context ;
374
391
private $ expectNoValidate = false ;
375
392
private $ atPathCalls = -1 ;
376
393
private $ expectedAtPath = [];
377
394
private $ validateCalls = -1 ;
378
395
private $ expectedValidate = [];
379
396
397
+ public function __construct (ExecutionContextInterface $ context )
398
+ {
399
+ $ this ->context = $ context ;
400
+ }
401
+
380
402
public function atPath ($ path )
381
403
{
382
404
Assert::assertFalse ($ this ->expectNoValidate , 'No validation calls have been expected. ' );
@@ -394,12 +416,20 @@ public function validate($value, $constraints = null, $groups = null)
394
416
{
395
417
Assert::assertFalse ($ this ->expectNoValidate , 'No validation calls have been expected. ' );
396
418
397
- list ($ expectedValue , $ expectedGroup , $ expectedConstraints ) = $ this ->expectedValidate [++$ this ->validateCalls ];
419
+ if (!isset ($ this ->expectedValidate [++$ this ->validateCalls ])) {
420
+ return $ this ;
421
+ }
422
+
423
+ list ($ expectedValue , $ expectedGroup , $ expectedConstraints , $ violation ) = $ this ->expectedValidate [$ this ->validateCalls ];
398
424
399
425
Assert::assertSame ($ expectedValue , $ value );
400
426
$ expectedConstraints ($ constraints );
401
427
Assert::assertSame ($ expectedGroup , $ groups );
402
428
429
+ if (null !== $ violation ) {
430
+ $ this ->context ->addViolation ($ violation ->getMessage (), $ violation ->getParameters ());
431
+ }
432
+
403
433
return $ this ;
404
434
}
405
435
@@ -415,16 +445,17 @@ public function validatePropertyValue($objectOrClass, $propertyName, $value, $gr
415
445
416
446
public function getViolations ()
417
447
{
448
+ return $ this ->context ->getViolations ();
418
449
}
419
450
420
451
public function expectNoValidate ()
421
452
{
422
453
$ this ->expectNoValidate = true ;
423
454
}
424
455
425
- public function expectValidation ($ call , $ propertyPath , $ value , $ group , $ constraints )
456
+ public function expectValidation ($ call , $ propertyPath , $ value , $ group , $ constraints, ConstraintViolationInterface $ violation = null )
426
457
{
427
458
$ this ->expectedAtPath [$ call ] = $ propertyPath ;
428
- $ this ->expectedValidate [$ call ] = [$ value , $ group , $ constraints ];
459
+ $ this ->expectedValidate [$ call ] = [$ value , $ group , $ constraints, $ violation ];
429
460
}
430
461
}
0 commit comments