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