@@ -445,10 +445,84 @@ public function testCanConditionallyInvokeValidators()
445
445
}
446
446
*/
447
447
448
- /**
448
+ /*
449
449
* Idea for this one is that validation may need to rely on context -- e.g., a "password confirmation"
450
450
* field may need to know what the original password entered was in order to compare.
451
451
*/
452
+
453
+ public function contextProvider ()
454
+ {
455
+ $ data = ['fooInput ' => 'fooValue ' ];
456
+ $ arrayAccessData = new ArrayObject (['fooInput ' => 'fooValue ' ]);
457
+ $ expectedFromData = ['fooInput ' => 'fooValue ' ];
458
+
459
+ return [
460
+ // Description => [$data, $customContext, $expectedContext]
461
+ 'by default get context from data (array) ' => [$ data , null , $ expectedFromData ],
462
+ 'by default get context from data (ArrayAccess) ' => [$ arrayAccessData , null , $ expectedFromData ],
463
+ 'use custom context ' => [[], 'fooContext ' , 'fooContext ' ],
464
+ ];
465
+ }
466
+
467
+ /**
468
+ * @dataProvider contextProvider
469
+ */
470
+ public function testValidationContext ($ data , $ customContext , $ expectedContext )
471
+ {
472
+ $ filter = new InputFilter ();
473
+
474
+ $ input = $ this ->createInputInterfaceMock (true , true , $ expectedContext );
475
+ $ filter ->add ($ input , 'fooInput ' );
476
+
477
+ $ filter ->setData ($ data );
478
+
479
+ $ this ->assertTrue (
480
+ $ filter ->isValid ($ customContext ),
481
+ 'isValid() value not match. Detail: ' . json_encode ($ filter ->getMessages ())
482
+ );
483
+ }
484
+
485
+ public function testBuildValidationContextUsingInputGetRawValue ()
486
+ {
487
+ $ data = [];
488
+ $ expectedContext = ['fooInput ' => 'fooRawValue ' ];
489
+ $ filter = new InputFilter ();
490
+
491
+ $ input = $ this ->createInputInterfaceMock (true , true , $ expectedContext , 'fooRawValue ' );
492
+ $ filter ->add ($ input , 'fooInput ' );
493
+
494
+ $ filter ->setData ($ data );
495
+
496
+ $ this ->assertTrue (
497
+ $ filter ->isValid (),
498
+ 'isValid() value not match. Detail: ' . json_encode ($ filter ->getMessages ())
499
+ );
500
+ }
501
+
502
+ public function testContextIsTheSameWhenARequiredInputIsGivenAndOptionalInputIsMissing ()
503
+ {
504
+ $ data = [
505
+ 'inputRequired ' => 'inputRequiredValue ' ,
506
+ ];
507
+ $ expectedContext = [
508
+ 'inputRequired ' => 'inputRequiredValue ' ,
509
+ 'inputOptional ' => null ,
510
+ ];
511
+ $ inputRequired = $ this ->createInputInterfaceMock (true , true , $ expectedContext );
512
+ $ inputOptional = $ this ->createInputInterfaceMock (false );
513
+
514
+ $ filter = new InputFilter ();
515
+ $ filter ->add ($ inputRequired , 'inputRequired ' );
516
+ $ filter ->add ($ inputOptional , 'inputOptional ' );
517
+
518
+ $ filter ->setData ($ data );
519
+
520
+ $ this ->assertTrue (
521
+ $ filter ->isValid (),
522
+ 'isValid() value not match. Detail: ' . json_encode ($ filter ->getMessages ())
523
+ );
524
+ }
525
+
452
526
public function testValidationCanUseContext ()
453
527
{
454
528
$ filter = new InputFilter ();
@@ -1051,4 +1125,36 @@ public function testAllowsValidatingArrayAccessData()
1051
1125
$ filter ->setData ($ data );
1052
1126
$ this ->assertTrue ($ filter ->isValid ());
1053
1127
}
1128
+
1129
+ /**
1130
+ * @param null|bool $isValid
1131
+ * @param mixed $expectedContext
1132
+ * @param mixed $getRawValue
1133
+ *
1134
+ * @return MockObject|InputInterface
1135
+ */
1136
+ protected function createInputInterfaceMock ($ isRequired , $ isValid = null , $ expectedContext = 'not-set ' , $ getRawValue = 'not-set ' )
1137
+ {
1138
+ /** @var InputInterface|MockObject $input */
1139
+ $ input = $ this ->getMock (InputInterface::class);
1140
+ $ input ->method ('isRequired ' )
1141
+ ->willReturn ($ isRequired )
1142
+ ;
1143
+ if ($ getRawValue !== 'not-set ' ) {
1144
+ $ input ->method ('getRawValue ' )
1145
+ ->willReturn ($ getRawValue )
1146
+ ;
1147
+ }
1148
+ if ($ isValid !== null ) {
1149
+ $ mockMethod = $ input ->expects ($ this ->once ())
1150
+ ->method ('isValid ' )
1151
+ ->willReturn ($ isValid )
1152
+ ;
1153
+ if ($ expectedContext !== 'not-set ' ) {
1154
+ $ mockMethod ->with ($ expectedContext );
1155
+ }
1156
+ }
1157
+
1158
+ return $ input ;
1159
+ }
1054
1160
}
0 commit comments