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

Commit 4cafeed

Browse files
committed
Consolidate tests for InputFilterInterface (isValid, getXValues, getMessages, getXInputs)
1 parent e3f4c38 commit 4cafeed

File tree

1 file changed

+232
-14
lines changed

1 file changed

+232
-14
lines changed

test/BaseInputFilterTest.php

Lines changed: 232 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
namespace ZendTest\InputFilter;
1111

12+
use ArrayIterator;
1213
use ArrayObject;
14+
use FilterIterator;
1315
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1416
use PHPUnit_Framework_TestCase as TestCase;
1517
use stdClass;
@@ -272,6 +274,104 @@ public function testReplace($input, $inputName, $expectedInput)
272274
$this->assertEquals($expectedInput, $returnInput, 'get() does not match the expected input');
273275
}
274276

277+
/**
278+
* @dataProvider setDataArgumentsProvider
279+
*/
280+
public function testSetDataAndGetRawValueGetValue(
281+
$inputs,
282+
$data,
283+
$expectedRawValues,
284+
$expectedValues,
285+
$expectedIsValid,
286+
$expectedInvalidInputs,
287+
$expectedValidInputs,
288+
$expectedMessages
289+
) {
290+
$inputFilter = $this->inputFilter;
291+
foreach ($inputs as $inputName => $input) {
292+
$inputFilter->add($input, $inputName);
293+
}
294+
$return = $inputFilter->setData($data);
295+
$this->assertSame($inputFilter, $return, 'setData() must return it self');
296+
297+
// ** Check filter state **
298+
$this->assertSame($expectedRawValues, $inputFilter->getRawValues(), 'getRawValues() value not match');
299+
foreach ($expectedRawValues as $inputName => $expectedRawValue) {
300+
$this->assertSame(
301+
$expectedRawValue,
302+
$inputFilter->getRawValue($inputName),
303+
'getRawValue() value not match for input ' . $inputName
304+
);
305+
}
306+
307+
$this->assertSame($expectedValues, $inputFilter->getValues(), 'getValues() value not match');
308+
foreach ($expectedValues as $inputName => $expectedValue) {
309+
$this->assertSame(
310+
$expectedValue,
311+
$inputFilter->getValue($inputName),
312+
'getValue() value not match for input ' . $inputName
313+
);
314+
}
315+
316+
// ** Check validation state **
317+
$this->assertEquals($expectedIsValid, $inputFilter->isValid(), 'isValid() value not match');
318+
$this->assertEquals($expectedInvalidInputs, $inputFilter->getInvalidInput(), 'getInvalidInput() value not match');
319+
$this->assertEquals($expectedValidInputs, $inputFilter->getValidInput(), 'getValidInput() value not match');
320+
$this->assertEquals($expectedMessages, $inputFilter->getMessages(), 'getMessages() value not match');
321+
}
322+
323+
/**
324+
* @dataProvider setDataArgumentsProvider
325+
*/
326+
public function testSetArrayAccessDataAndGetRawValueGetValue(
327+
$inputs,
328+
$data,
329+
$expectedRawValues,
330+
$expectedValues,
331+
$expectedIsValid,
332+
$expectedInvalidInputs,
333+
$expectedValidInputs,
334+
$expectedMessages
335+
) {
336+
$dataTypes = $this->dataTypes();
337+
$this->testSetDataAndGetRawValueGetValue(
338+
$inputs,
339+
$dataTypes['ArrayAccess']($data),
340+
$expectedRawValues,
341+
$expectedValues,
342+
$expectedIsValid,
343+
$expectedInvalidInputs,
344+
$expectedValidInputs,
345+
$expectedMessages
346+
);
347+
}
348+
349+
/**
350+
* @dataProvider setDataArgumentsProvider
351+
*/
352+
public function testSetTraversableDataAndGetRawValueGetValue(
353+
$inputs,
354+
$data,
355+
$expectedRawValues,
356+
$expectedValues,
357+
$expectedIsValid,
358+
$expectedInvalidInputs,
359+
$expectedValidInputs,
360+
$expectedMessages
361+
) {
362+
$dataTypes = $this->dataTypes();
363+
$this->testSetDataAndGetRawValueGetValue(
364+
$inputs,
365+
$dataTypes['Traversable']($data),
366+
$expectedRawValues,
367+
$expectedValues,
368+
$expectedIsValid,
369+
$expectedInvalidInputs,
370+
$expectedValidInputs,
371+
$expectedMessages
372+
);
373+
}
374+
275375
public function getInputFilter()
276376
{
277377
$filter = $this->inputFilter;
@@ -1278,6 +1378,86 @@ public function addMethodArgumentsProvider()
12781378
return $dataSets;
12791379
}
12801380

1381+
public function setDataArgumentsProvider()
1382+
{
1383+
$iAName = 'InputA';
1384+
$iBName = 'InputB';
1385+
$vRaw = 'rawValue';
1386+
$vFiltered = 'filteredValue';
1387+
1388+
$dARaw = [$iAName => $vRaw];
1389+
$dBRaw = [$iBName => $vRaw];
1390+
$d2Raw = array_merge($dARaw, $dBRaw);
1391+
$dAFiltered = [$iAName => $vFiltered];
1392+
$dBFiltered = [$iBName => $vFiltered];
1393+
$d2Filtered = array_merge($dAFiltered, $dBFiltered);
1394+
1395+
$required = true;
1396+
$valid = true;
1397+
$bOnFail = true;
1398+
1399+
$input = function ($iName, $required, $bOnFail, $isValid, $msg = []) use ($vRaw, $vFiltered) {
1400+
// @codingStandardsIgnoreStart
1401+
return function ($context) use ($iName, $required, $bOnFail, $isValid, $vRaw, $vFiltered, $msg) {
1402+
return $this->createInputInterfaceMock($iName, $required, $isValid, $context, $vRaw, $vFiltered, $msg, $bOnFail);
1403+
};
1404+
// @codingStandardsIgnoreEnd
1405+
};
1406+
1407+
// @codingStandardsIgnoreStart
1408+
$iAri = [$iAName => $input($iAName, $required, !$bOnFail, !$valid, ['Invalid ' . $iAName])];
1409+
$iAriX = [$iAName => $input($iAName, $required, $bOnFail, !$valid, ['Invalid ' . $iAName])];
1410+
$iArvX = [$iAName => $input($iAName, $required, $bOnFail, $valid, [])];
1411+
$iBri = [$iBName => $input($iBName, $required, !$bOnFail, !$valid, ['Invalid ' . $iBName])];
1412+
$iBriX = [$iBName => $input($iBName, $required, $bOnFail, !$valid, ['Invalid ' . $iBName])];
1413+
$iBrvX = [$iBName => $input($iBName, $required, $bOnFail, $valid, [])];
1414+
$iAriBri = array_merge($iAri , $iBri);
1415+
$iArvXBrvX = array_merge($iArvX, $iBrvX);
1416+
$iAriBrvX = array_merge($iAri , $iBrvX);
1417+
$iArvXBir = array_merge($iArvX, $iBri);
1418+
$iAriXBrvX = array_merge($iAriX, $iBrvX);
1419+
$iArvXBriX = array_merge($iArvX, $iBriX);
1420+
$iAriXBriX = array_merge($iAriX, $iBriX);
1421+
1422+
$msgAInv = [$iAName => ['Invalid InputA']];
1423+
$msgBInv = [$iBName => ['Invalid InputB']];
1424+
$msg2Inv = array_merge($msgAInv, $msgBInv);
1425+
1426+
$dataSets = [
1427+
// Description => [$inputs, $data argument, $expectedRawValues, $expectedValues, $expectedIsValid,
1428+
// $expectedInvalidInputs, $expectedValidInputs, $expectedMessages]
1429+
'invalid Break invalid' => [$iAriXBriX, $d2Raw, $d2Raw, $d2Filtered, false, $iAri , [] , $msgAInv],
1430+
'invalid Break valid' => [$iAriXBrvX, $d2Raw, $d2Raw, $d2Filtered, false, $iAri , [] , $msgAInv],
1431+
'valid Break invalid' => [$iArvXBriX, $d2Raw, $d2Raw, $d2Filtered, false, $iBri , $iAri , $msgBInv],
1432+
'valid Break valid' => [$iArvXBrvX, $d2Raw, $d2Raw, $d2Filtered, true , [] , $iArvXBrvX, []],
1433+
'valid invalid' => [$iArvXBir , $d2Raw, $d2Raw, $d2Filtered, false, $iBri , $iArvX , $msgBInv],
1434+
'invalid valid' => [$iAriBrvX , $d2Raw, $d2Raw, $d2Filtered, false, $iAri , $iBrvX , $msgAInv],
1435+
'invalid invalid' => [$iAriBri , $d2Raw, $d2Raw, $d2Filtered, false, $iAriBri , [] , $msg2Inv],
1436+
'invalid valid/NotSet' => [$iAriBri , $dARaw, $d2Raw, $d2Filtered, false, $iAriBrvX, [] , $msg2Inv],
1437+
];
1438+
// @codingStandardsIgnoreEnd
1439+
1440+
array_walk(
1441+
$dataSets,
1442+
function (&$set) {
1443+
// Create unique mock input instances for each set
1444+
foreach ($set[0] as $name => $createMock) {
1445+
$input = $createMock($set[2]);
1446+
1447+
$set[0][$name] = $input;
1448+
if (in_array($name, array_keys($set[5]))) {
1449+
$set[5][$name] = $input;
1450+
}
1451+
if (in_array($name, array_keys($set[6]))) {
1452+
$set[6][$name] = $input;
1453+
}
1454+
}
1455+
}
1456+
);
1457+
1458+
return $dataSets;
1459+
}
1460+
12811461
public function inputProvider()
12821462
{
12831463
$input = $this->createInputInterfaceMock('fooInput', null);
@@ -1307,18 +1487,24 @@ protected function createInputFilterInterfaceMock()
13071487
* @param string $name
13081488
* @param bool $isRequired
13091489
* @param null|bool $isValid
1310-
* @param mixed $expectedContext
1490+
* @param mixed $context
13111491
* @param mixed $getRawValue
1492+
* @param mixed $getValue
1493+
* @param string[] $getMessages
1494+
* @param bool $breakOnFailure
13121495
*
13131496
* @return MockObject|InputInterface
13141497
*/
13151498
protected function createInputInterfaceMock(
13161499
$name,
13171500
$isRequired,
13181501
$isValid = null,
1319-
$expectedContext = 'not-set',
1320-
$getRawValue = 'not-set')
1321-
{
1502+
$context = null,
1503+
$getRawValue = null,
1504+
$getValue = null,
1505+
$getMessages = [],
1506+
$breakOnFailure = false
1507+
) {
13221508
/** @var InputInterface|MockObject $input */
13231509
$input = $this->getMock(InputInterface::class);
13241510
$input->method('getName')
@@ -1327,21 +1513,53 @@ protected function createInputInterfaceMock(
13271513
$input->method('isRequired')
13281514
->willReturn($isRequired)
13291515
;
1330-
if ($getRawValue !== 'not-set') {
1331-
$input->method('getRawValue')
1332-
->willReturn($getRawValue)
1333-
;
1334-
}
1335-
if ($isValid !== null) {
1336-
$mockMethod = $input->expects($this->once())
1516+
$input->method('getRawValue')
1517+
->willReturn($getRawValue)
1518+
;
1519+
$input->method('getValue')
1520+
->willReturn($getValue)
1521+
;
1522+
$input->method('breakOnFailure')
1523+
->willReturn($breakOnFailure)
1524+
;
1525+
if (($isValid === false) || ($isValid === true)) {
1526+
$input->expects($this->once())
13371527
->method('isValid')
1528+
->with($context)
13381529
->willReturn($isValid)
13391530
;
1340-
if ($expectedContext !== 'not-set') {
1341-
$mockMethod->with($expectedContext);
1342-
}
1531+
} else {
1532+
$input->expects($this->never())
1533+
->method('isValid')
1534+
->with($context)
1535+
;
13431536
}
1537+
$input->method('getMessages')
1538+
->willReturn($getMessages)
1539+
;
13441540

13451541
return $input;
13461542
}
1543+
1544+
/**
1545+
* @return callable[]
1546+
*/
1547+
protected function dataTypes()
1548+
{
1549+
return [
1550+
// Description => callable
1551+
'array' => function ($data) {
1552+
return $data;
1553+
},
1554+
'ArrayAccess' => function ($data) {
1555+
return new ArrayIterator($data);
1556+
},
1557+
'Traversable' => function ($data) {
1558+
return $this->getMockBuilder(FilterIterator::class)
1559+
->setConstructorArgs([new ArrayIterator($data)])
1560+
->getMock()
1561+
;
1562+
},
1563+
];
1564+
}
13471565
}

0 commit comments

Comments
 (0)