|
9 | 9 |
|
10 | 10 | namespace ZendTest\InputFilter;
|
11 | 11 |
|
| 12 | +use PHPUnit_Framework_MockObject_MockObject as MockObject; |
12 | 13 | use PHPUnit_Framework_TestCase as TestCase;
|
13 | 14 | use RuntimeException;
|
14 | 15 | use Zend\Filter;
|
@@ -330,6 +331,82 @@ public function testMergeRetainsAllowEmptyFlag()
|
330 | 331 | $this->assertTrue($input2->allowEmpty());
|
331 | 332 | }
|
332 | 333 |
|
| 334 | + public function isRequiredVsAllowEmptyVsContinueIfEmptyVsIsValidProvider() |
| 335 | + { |
| 336 | + $emptyValues = $this->emptyValueProvider(); |
| 337 | + array_walk( |
| 338 | + $emptyValues, |
| 339 | + function (&$dataValue) { |
| 340 | + $dataValue = $dataValue[0]; |
| 341 | + } |
| 342 | + ); |
| 343 | + |
| 344 | + $isRequired = true; |
| 345 | + $aEmpty = true; |
| 346 | + $cIEmpty = true; |
| 347 | + |
| 348 | + $vChainNotCall = function ($context) { |
| 349 | + return $this->createValidatorChainMock(null, $context); |
| 350 | + }; |
| 351 | + $vChainInvalid = function ($context) { |
| 352 | + return $this->createValidatorChainMock(false, $context); |
| 353 | + }; |
| 354 | + $vChainValid = function ($context) { |
| 355 | + return $this->createValidatorChainMock(true, $context); |
| 356 | + }; |
| 357 | + $isValid = true; |
| 358 | + |
| 359 | + // @codingStandardsIgnoreStart |
| 360 | + $dataTemplates=[ |
| 361 | + /* Description => [$isRequired, $allowEmpty, $continueIfEmpty, $validatorChain, [$values], $expectedIsValid */ |
| 362 | + 'Required: T; AEmpty: T; CIEmpty: F; VChain: X' => [ $isRequired, $aEmpty, !$cIEmpty, $vChainNotCall, $emptyValues, $isValid], |
| 363 | + 'Required: T; AEmpty: T; CIEmpty: T; VChain: T' => [ $isRequired, $aEmpty, $cIEmpty, $vChainValid , $emptyValues, $isValid], |
| 364 | + 'Required: T; AEmpty: T; CIEmpty: T; VChain: F' => [ $isRequired, $aEmpty, $cIEmpty, $vChainInvalid, $emptyValues, !$isValid], |
| 365 | + 'Required: T; AEmpty: F; CIEmpty: F; VChain: X' => [ $isRequired, !$aEmpty, !$cIEmpty, $vChainNotCall, $emptyValues, !$isValid], |
| 366 | + 'Required: T; AEmpty: F; CIEmpty: T; VChain: T' => [ $isRequired, !$aEmpty, $cIEmpty, $vChainValid , $emptyValues, $isValid], |
| 367 | + 'Required: T; AEmpty: F; CIEmpty: T; VChain: F' => [ $isRequired, !$aEmpty, $cIEmpty, $vChainInvalid, $emptyValues, !$isValid], |
| 368 | + 'Required: F; AEmpty: T; CIEmpty: F; VChain: X' => [!$isRequired, $aEmpty, !$cIEmpty, $vChainNotCall, $emptyValues, $isValid], |
| 369 | + 'Required: F; AEmpty: F; CIEmpty: F; VChain: X' => [!$isRequired, !$aEmpty, !$cIEmpty, $vChainNotCall, $emptyValues, $isValid], |
| 370 | + 'Required: F; AEmpty: T; CIEmpty: T; VChain: T' => [!$isRequired, $aEmpty, $cIEmpty, $vChainValid , $emptyValues, $isValid], |
| 371 | + 'Required: F; AEmpty: T; CIEmpty: T; VChain: F' => [!$isRequired, $aEmpty, $cIEmpty, $vChainInvalid, $emptyValues, !$isValid], |
| 372 | + 'Required: F; AEmpty: F; CIEmpty: T; VChain: T' => [!$isRequired, !$aEmpty, $cIEmpty, $vChainValid , $emptyValues, $isValid], |
| 373 | + 'Required: F; AEmpty: F; CIEmpty: T; VChain: F' => [!$isRequired, !$aEmpty, $cIEmpty, $vChainInvalid, $emptyValues, !$isValid], |
| 374 | + ]; |
| 375 | + // @codingStandardsIgnoreEnd |
| 376 | + |
| 377 | + // Expand data template matrix for each possible input value. |
| 378 | + // Description => [$isRequired, $allowEmpty, $continueIfEmpty, $validatorChain, $value, $expectedIsValid] |
| 379 | + foreach ($dataTemplates as $dataTemplateDescription => $dataTemplate) { |
| 380 | + $tmpTemplate = $dataTemplate; |
| 381 | + foreach ($dataTemplate[4] as $valueDescription => $value) { |
| 382 | + $tmpTemplate[3] = $dataTemplate[3]($value); |
| 383 | + $tmpTemplate[4] = $value; |
| 384 | + yield $dataTemplateDescription . ' / ' . $valueDescription => $tmpTemplate; |
| 385 | + } |
| 386 | + } |
| 387 | + } |
| 388 | + |
| 389 | + /** |
| 390 | + * @group 7448 |
| 391 | + * @dataProvider isRequiredVsAllowEmptyVsContinueIfEmptyVsIsValidProvider |
| 392 | + */ |
| 393 | + public function testIsRequiredVsAllowEmptyVsContinueIfEmptyVsIsValid( |
| 394 | + $required, |
| 395 | + $allowEmpty, |
| 396 | + $continueIfEmpty, |
| 397 | + $validatorChain, |
| 398 | + $value, |
| 399 | + $expectedIsValid |
| 400 | + ) { |
| 401 | + $this->input->setRequired($required); |
| 402 | + $this->input->setAllowEmpty($allowEmpty); |
| 403 | + $this->input->setContinueIfEmpty($continueIfEmpty); |
| 404 | + $this->input->setValidatorChain($validatorChain); |
| 405 | + $this->input->setValue($value); |
| 406 | + |
| 407 | + $this->assertEquals($expectedIsValid, $this->input->isValid(), json_encode($this->input->getMessages())); |
| 408 | + } |
| 409 | + |
333 | 410 | public function whenRequiredAndAllowEmptyAndNotContinueIfEmptyValidatorsAreNotRun()
|
334 | 411 | {
|
335 | 412 | $validator = new Validator\Callback(function ($value) {
|
@@ -629,4 +706,49 @@ public function testWhenNotRequiredAndNotAllowEmptyAndContinueIfEmptyValidatorsA
|
629 | 706 | $input->setValue($value);
|
630 | 707 | $this->{$assertion}($input->isValid());
|
631 | 708 | }
|
| 709 | + |
| 710 | + public function emptyValueProvider() |
| 711 | + { |
| 712 | + return [ |
| 713 | + // Description => [$value] |
| 714 | + 'null' => [null], |
| 715 | + '""' => [''], |
| 716 | + '[]' => [[]], |
| 717 | + ]; |
| 718 | + } |
| 719 | + |
| 720 | + /** |
| 721 | + * @param null|bool $isValid |
| 722 | + * @param mixed $context |
| 723 | + * |
| 724 | + * @return MockObject|Validator\ValidatorChain |
| 725 | + */ |
| 726 | + protected function createValidatorChainMock($isValid = null, $context = 'not-set') |
| 727 | + { |
| 728 | + /** @var Validator\ValidatorChain|MockObject $validatorChain */ |
| 729 | + $validatorChain = $this->getMockBuilder(Validator\ValidatorChain::class) |
| 730 | + ->setMethods(['isValid']) |
| 731 | + ->getMock() |
| 732 | + ; |
| 733 | + |
| 734 | + switch ($isValid) { |
| 735 | + case true: |
| 736 | + case false: |
| 737 | + $isValidMethod = $validatorChain->expects($this->any()) |
| 738 | + ->method('isValid') |
| 739 | + ->willReturn($isValid) |
| 740 | + ; |
| 741 | + break; |
| 742 | + default: |
| 743 | + $isValidMethod = $validatorChain->expects($this->never()) |
| 744 | + ->method('isValid') |
| 745 | + ; |
| 746 | + break; |
| 747 | + } |
| 748 | + if ($context !== 'not-set') { |
| 749 | + $isValidMethod->with($context); |
| 750 | + } |
| 751 | + |
| 752 | + return $validatorChain; |
| 753 | + } |
632 | 754 | }
|
0 commit comments