|
10 | 10 | namespace ZendTest\InputFilter;
|
11 | 11 |
|
12 | 12 | use ArrayObject;
|
| 13 | +use PHPUnit_Framework_MockObject_MockObject as MockObject; |
13 | 14 | use PHPUnit_Framework_TestCase as TestCase;
|
14 | 15 | use stdClass;
|
15 | 16 | use Zend\InputFilter\Input;
|
| 17 | +use Zend\InputFilter\InputInterface; |
16 | 18 | use Zend\InputFilter\FileInput;
|
17 | 19 | use Zend\InputFilter\BaseInputFilter as InputFilter;
|
18 | 20 | use Zend\Filter;
|
@@ -433,44 +435,108 @@ public function testCanGetValidationMessages()
|
433 | 435 | }
|
434 | 436 | }
|
435 | 437 |
|
436 |
| - /** |
437 |
| - * Idea for this one is that one input may only need to be validated if another input is present. |
438 |
| - * |
439 |
| - * Commenting out for now, as validation context may make this irrelevant, and unsure what API to expose. |
440 |
| - public function testCanConditionallyInvokeValidators() |
| 438 | + /* |
| 439 | + * Idea for this one is that validation may need to rely on context -- e.g., a "password confirmation" |
| 440 | + * field may need to know what the original password entered was in order to compare. |
| 441 | + */ |
| 442 | + |
| 443 | + public function contextProvider() |
441 | 444 | {
|
442 |
| - $this->markTestIncomplete(); |
| 445 | + $data = ['fooInput' => 'fooValue']; |
| 446 | + $arrayAccessData = new ArrayObject(['fooInput' => 'fooValue']); |
| 447 | + $expectedFromData = ['fooInput' => 'fooValue']; |
| 448 | + |
| 449 | + return [ |
| 450 | + // Description => [$data, $customContext, $expectedContext] |
| 451 | + 'by default get context from data (array)' => [$data, null, $expectedFromData], |
| 452 | + 'by default get context from data (ArrayAccess)' => [$arrayAccessData, null, $expectedFromData], |
| 453 | + 'use custom context' => [[], 'fooContext', 'fooContext'], |
| 454 | + ]; |
443 | 455 | }
|
444 |
| - */ |
445 | 456 |
|
446 | 457 | /**
|
447 |
| - * Idea for this one is that validation may need to rely on context -- e.g., a "password confirmation" |
448 |
| - * field may need to know what the original password entered was in order to compare. |
| 458 | + * @dataProvider contextProvider |
| 459 | + * |
| 460 | + * @param mixed $data |
| 461 | + * @param mixed $customContext |
| 462 | + * @param mixed $expectedContext |
449 | 463 | */
|
450 |
| - public function testValidationCanUseContext() |
| 464 | + public function testValidationContext($data, $customContext, $expectedContext) |
451 | 465 | {
|
452 | 466 | $filter = new InputFilter();
|
453 | 467 |
|
454 |
| - $store = new stdClass; |
455 |
| - $foo = new Input(); |
456 |
| - $foo->getValidatorChain()->attach(new Validator\Callback(function ($value, $context) use ($store) { |
457 |
| - $store->value = $value; |
458 |
| - $store->context = $context; |
459 |
| - return true; |
460 |
| - })); |
| 468 | + /** @var InputInterface|MockObject $input */ |
| 469 | + $input = $this->getMock(InputInterface::class); |
| 470 | + $input->expects($this->once()) |
| 471 | + ->method('isValid') |
| 472 | + ->with($expectedContext) |
| 473 | + ->willReturn(true) |
| 474 | + ; |
461 | 475 |
|
462 |
| - $bar = new Input(); |
463 |
| - $bar->getValidatorChain()->attach(new Validator\Digits()); |
| 476 | + $filter->add($input, 'fooInput'); |
464 | 477 |
|
465 |
| - $filter->add($foo, 'foo') |
466 |
| - ->add($bar, 'bar'); |
| 478 | + $filter->setData($data); |
| 479 | + |
| 480 | + $this->assertTrue($filter->isValid($customContext), json_encode($filter->getMessages())); |
| 481 | + } |
| 482 | + |
| 483 | + public function testBuildValidationContextUsingInputGetRawValue() |
| 484 | + { |
| 485 | + $data = []; |
| 486 | + $expectedContext = ['fooInput' => 'fooRawValue']; |
| 487 | + $filter = new InputFilter(); |
| 488 | + |
| 489 | + /** @var InputInterface|MockObject $input */ |
| 490 | + $input = $this->getMock(InputInterface::class); |
| 491 | + $input->method('getRawValue') |
| 492 | + ->willReturn('fooRawValue') |
| 493 | + ; |
| 494 | + $input->expects($this->once()) |
| 495 | + ->method('isValid') |
| 496 | + ->with($expectedContext) |
| 497 | + ->willReturn(true) |
| 498 | + ; |
| 499 | + |
| 500 | + $filter->add($input, 'fooInput'); |
467 | 501 |
|
468 |
| - $data = ['foo' => 'foo', 'bar' => 123]; |
469 | 502 | $filter->setData($data);
|
470 | 503 |
|
471 |
| - $this->assertTrue($filter->isValid()); |
472 |
| - $this->assertEquals('foo', $store->value); |
473 |
| - $this->assertEquals($data, $store->context); |
| 504 | + $this->assertTrue($filter->isValid(), json_encode($filter->getMessages())); |
| 505 | + } |
| 506 | + |
| 507 | + public function testContextIsTheSameWhenARequiredInputIsGivenAndOptionalInputIsMissing() |
| 508 | + { |
| 509 | + $data = [ |
| 510 | + 'inputRequired' => 'inputRequiredValue', |
| 511 | + ]; |
| 512 | + $expectedContext = [ |
| 513 | + 'inputRequired' => 'inputRequiredValue', |
| 514 | + 'inputOptional' => null, |
| 515 | + ]; |
| 516 | + $filter = new InputFilter(); |
| 517 | + |
| 518 | + /** @var InputInterface|MockObject $inputRequired */ |
| 519 | + $inputRequired = $this->getMock(InputInterface::class); |
| 520 | + $inputRequired->expects($this->once()) |
| 521 | + ->method('isValid') |
| 522 | + ->with($expectedContext) |
| 523 | + ->willReturn(true) |
| 524 | + ; |
| 525 | + |
| 526 | + /** @var InputInterface|MockObject $inputOptional */ |
| 527 | + $inputOptional = $this->getMock(InputInterface::class); |
| 528 | + $inputOptional->expects($this->once()) |
| 529 | + ->method('isValid') |
| 530 | + ->with($expectedContext) |
| 531 | + ->willReturn(true) |
| 532 | + ; |
| 533 | + |
| 534 | + $filter->add($inputRequired, 'inputRequired'); |
| 535 | + $filter->add($inputOptional, 'inputOptional'); |
| 536 | + |
| 537 | + $filter->setData($data); |
| 538 | + |
| 539 | + $this->assertTrue($filter->isValid(), json_encode($filter->getMessages())); |
474 | 540 | }
|
475 | 541 |
|
476 | 542 | /**
|
@@ -627,48 +693,6 @@ public function testValidationMarksInputInvalidWhenRequiredAndAllowEmptyFlagIsFa
|
627 | 693 | $this->assertFalse($filter->isValid());
|
628 | 694 | }
|
629 | 695 |
|
630 |
| - public static function contextDataProvider() |
631 |
| - { |
632 |
| - return [ |
633 |
| - ['', 'y', true], |
634 |
| - ['', 'n', false], |
635 |
| - ]; |
636 |
| - } |
637 |
| - |
638 |
| - /** |
639 |
| - * Idea here is that an empty field may or may not be valid based on |
640 |
| - * context. |
641 |
| - */ |
642 |
| - /** |
643 |
| - * @dataProvider contextDataProvider() |
644 |
| - */ |
645 |
| - // @codingStandardsIgnoreStart |
646 |
| - public function testValidationMarksInputValidWhenAllowEmptyFlagIsTrueAndContinueIfEmptyIsTrueAndContextValidatesEmptyField($allowEmpty, $blankIsValid, $valid) |
647 |
| - { |
648 |
| - // @codingStandardsIgnoreEnd |
649 |
| - $filter = new InputFilter(); |
650 |
| - |
651 |
| - $data = [ |
652 |
| - 'allowEmpty' => $allowEmpty, |
653 |
| - 'blankIsValid' => $blankIsValid, |
654 |
| - ]; |
655 |
| - |
656 |
| - $allowEmpty = new Input(); |
657 |
| - $allowEmpty->setAllowEmpty(true) |
658 |
| - ->setContinueIfEmpty(true); |
659 |
| - |
660 |
| - $blankIsValid = new Input(); |
661 |
| - $blankIsValid->getValidatorChain()->attach(new Validator\Callback(function ($value, $context) { |
662 |
| - return ('y' === $value && empty($context['allowEmpty'])); |
663 |
| - })); |
664 |
| - |
665 |
| - $filter->add($allowEmpty, 'allowEmpty') |
666 |
| - ->add($blankIsValid, 'blankIsValid'); |
667 |
| - $filter->setData($data); |
668 |
| - |
669 |
| - $this->assertSame($valid, $filter->isValid()); |
670 |
| - } |
671 |
| - |
672 | 696 | public function testCanRetrieveRawValuesIndividuallyWithoutValidating()
|
673 | 697 | {
|
674 | 698 | if (!extension_loaded('intl')) {
|
|
0 commit comments