Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 2c0e7e5

Browse files
committed
Refactor tests for group 7448 as a data set matrix
1 parent 4a03bd1 commit 2c0e7e5

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

test/ArrayInputTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,44 @@ public function testCanRetrieveRawValue()
5858
$this->assertEquals(['bar'], $this->input->getRawValue());
5959
}
6060

61+
/**
62+
* @group 7448
63+
*/
64+
public function testIsRequiredVsAllowEmptyVsContinueIfEmptyVsIsValid(
65+
$required = null,
66+
$allowEmpty = null,
67+
$continueIfEmpty = null,
68+
$validatorChain = null,
69+
$value = null,
70+
$expectedIsValid = null
71+
) {
72+
$this->markTestSkipped('ArrayInput::setValue is not compatible with InputInterface::setValue');
73+
}
74+
75+
/**
76+
* Specific ArrayInput::setValue behavior
77+
*
78+
* @group 7448
79+
* @dataProvider isRequiredVsAllowEmptyVsContinueIfEmptyVsIsValidProvider
80+
*/
81+
public function testArrayInputIsRequiredVsAllowEmptyVsContinueIfEmptyVsIsValid(
82+
$required,
83+
$allowEmpty,
84+
$continueIfEmpty,
85+
$validatorChain,
86+
$value,
87+
$expectedIsValid
88+
) {
89+
parent::testIsRequiredVsAllowEmptyVsContinueIfEmptyVsIsValid(
90+
$required,
91+
$allowEmpty,
92+
$continueIfEmpty,
93+
$validatorChain,
94+
[$value],
95+
$expectedIsValid
96+
);
97+
}
98+
6199
public function testValidationOperatesOnFilteredValue()
62100
{
63101
$this->input->setValue([' 123 ', ' 123']);

test/FileInputTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ public function testRetrievingValueFiltersTheValue()
3434
$this->markTestSkipped('Test are not enabled in FileInputTest');
3535
}
3636

37+
/**
38+
* @group 7448
39+
*/
40+
public function testIsRequiredVsAllowEmptyVsContinueIfEmptyVsIsValid(
41+
$required = null,
42+
$allowEmpty = null,
43+
$continueIfEmpty = null,
44+
$validatorChain = null,
45+
$value = null,
46+
$expectedIsValid = null
47+
) {
48+
$this->markTestSkipped('ArrayInput::setValue is not compatible with InputInterface::setValue');
49+
}
50+
3751
public function testRetrievingValueFiltersTheValueOnlyAfterValidating()
3852
{
3953
$value = ['tmp_name' => 'bar'];

test/InputTest.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace ZendTest\InputFilter;
1111

12+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1213
use PHPUnit_Framework_TestCase as TestCase;
1314
use RuntimeException;
1415
use Zend\Filter;
@@ -330,6 +331,82 @@ public function testMergeRetainsAllowEmptyFlag()
330331
$this->assertTrue($input2->allowEmpty());
331332
}
332333

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+
333410
public function whenRequiredAndAllowEmptyAndNotContinueIfEmptyValidatorsAreNotRun()
334411
{
335412
$validator = new Validator\Callback(function ($value) {
@@ -629,4 +706,49 @@ public function testWhenNotRequiredAndNotAllowEmptyAndContinueIfEmptyValidatorsA
629706
$input->setValue($value);
630707
$this->{$assertion}($input->isValid());
631708
}
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+
}
632754
}

0 commit comments

Comments
 (0)