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

Commit

Permalink
Refactor FileInput test
Browse files Browse the repository at this point in the history
Remove dependencies with specific implementations
  • Loading branch information
Maks3w committed Dec 17, 2015
1 parent f1b103f commit fb1e741
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 113 deletions.
39 changes: 25 additions & 14 deletions test/ArrayInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,39 @@ public function fallbackValueVsIsValidProvider()
return $dataSets;
}

protected function createFilterChainMock($valueRaw = null, $valueFiltered = null)
protected function createFilterChainMock(array $valueMap = [])
{
// ArrayInput filters per each array value
if (is_array($valueRaw)) {
$valueRaw = current($valueRaw);
}

if (is_array($valueFiltered)) {
$valueFiltered = current($valueFiltered);
}
$valueMap = array_map(
function ($values) {
if (is_array($values[0])) {
$values[0] = current($values[0]);
}
if (is_array($values[1])) {
$values[1] = current($values[1]);
}
return $values;
},
$valueMap
);

return parent::createFilterChainMock($valueRaw, $valueFiltered);
return parent::createFilterChainMock($valueMap);
}

protected function createValidatorChainMock($isValid = null, $value = null, $context = null, $messages = [])
protected function createValidatorChainMock(array $valueMap = [], $messages = [])
{
// ArrayInput validates per each array value
if (is_array($value)) {
$value = current($value);
}
$valueMap = array_map(
function ($values) {
if (is_array($values[0])) {
$values[0] = current($values[0]);
}
return $values;
},
$valueMap
);

return parent::createValidatorChainMock($isValid, $value, $context, $messages);
return parent::createValidatorChainMock($valueMap, $messages);
}

protected function getDummyValue($raw = true)
Expand Down
95 changes: 19 additions & 76 deletions test/FileInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

namespace ZendTest\InputFilter;

use PHPUnit_Framework_MockObject_MockObject as MockObject;
use Zend\Filter;
use Zend\InputFilter\FileInput;
use Zend\Validator;

/**
* @covers Zend\InputFilter\FileInput
Expand All @@ -35,22 +32,7 @@ public function testRetrievingValueFiltersTheValueOnlyAfterValidating()
$this->input->setValue($value);

$newValue = ['tmp_name' => 'foo'];
/** @var Filter\File\Rename|MockObject $filterMock */
$filterMock = $this->getMockBuilder(Filter\File\Rename::class)
->disableOriginalConstructor()
->getMock();
$filterMock->expects($this->any())
->method('filter')
->will($this->returnValue($newValue));

// Why not attach mocked filter directly?
// No worky without wrapping in a callback.
// Missing something in mock setup?
$this->input->getFilterChain()->attach(
function ($value) use ($filterMock) {
return $filterMock->filter($value);
}
);
$this->input->setFilterChain($this->createFilterChainMock([[$value, $newValue]]));

$this->assertEquals($value, $this->input->getValue());
$this->assertTrue(
Expand All @@ -70,30 +52,20 @@ public function testCanFilterArrayOfMultiFileData()
$this->input->setValue($values);

$newValue = ['tmp_name' => 'new'];
/** @var Filter\File\Rename|MockObject $filterMock */
$filterMock = $this->getMockBuilder(Filter\File\Rename::class)
->disableOriginalConstructor()
->getMock();
$filterMock->expects($this->any())
->method('filter')
->will($this->returnValue($newValue));

// Why not attach mocked filter directly?
// No worky without wrapping in a callback.
// Missing something in mock setup?
$this->input->getFilterChain()->attach(
function ($value) use ($filterMock) {
return $filterMock->filter($value);
}
);
$filteredValue = [$newValue, $newValue, $newValue];
$this->input->setFilterChain($this->createFilterChainMock([
[$values[0], $newValue],
[$values[1], $newValue],
[$values[2], $newValue],
]));

$this->assertEquals($values, $this->input->getValue());
$this->assertTrue(
$this->input->isValid(),
'isValid() value not match. Detail . ' . json_encode($this->input->getMessages())
);
$this->assertEquals(
[$newValue, $newValue, $newValue],
$filteredValue,
$this->input->getValue()
);
}
Expand All @@ -102,8 +74,10 @@ public function testCanRetrieveRawValue()
{
$value = ['tmp_name' => 'bar'];
$this->input->setValue($value);
$filter = new Filter\StringToUpper();
$this->input->getFilterChain()->attach($filter);

$newValue = ['tmp_name' => 'new'];
$this->input->setFilterChain($this->createFilterChainMock([[$value, $newValue]]));

$this->assertEquals($value, $this->input->getRawValue());
}

Expand All @@ -123,40 +97,11 @@ public function testValidationOperatesBeforeFiltering()
$this->input->setValue($badValue);

$filteredValue = ['tmp_name' => 'new'];
/** @var Filter\File\Rename|MockObject $filterMock */
$filterMock = $this->getMockBuilder(Filter\File\Rename::class)
->disableOriginalConstructor()
->getMock();
$filterMock->expects($this->any())
->method('filter')
->will($this->returnValue($filteredValue));

// Why not attach mocked filter directly?
// No worky without wrapping in a callback.
// Missing something in mock setup?
$this->input->getFilterChain()->attach(
function ($value) use ($filterMock) {
return $filterMock->filter($value);
}
);
$this->input->setFilterChain($this->createFilterChainMock([[$badValue, $filteredValue]]));
$this->input->setValidatorChain($this->createValidatorChainMock([[$badValue, null, false]]));

$validator = new Validator\File\Exists();
$this->input->getValidatorChain()->attach($validator);
$this->assertFalse($this->input->isValid());
$this->assertEquals($badValue, $this->input->getValue());

$goodValue = [
'tmp_name' => __FILE__,
'name' => 'foo',
'size' => 1,
'error' => 0,
];
$this->input->setValue($goodValue);
$this->assertTrue(
$this->input->isValid(),
'isValid() value not match. Detail . ' . json_encode($this->input->getMessages())
);
$this->assertEquals($filteredValue, $this->input->getValue());
}

public function testCanValidateArrayOfMultiFileData()
Expand All @@ -176,17 +121,15 @@ public function testCanValidateArrayOfMultiFileData()
],
];
$this->input->setValue($values);
$validator = new Validator\File\Exists();
$this->input->getValidatorChain()->attach($validator);
$this->input->setValidatorChain($this->createValidatorChainMock([
[$values[0], null, true],
[$values[1], null, true],
[$values[2], null, true],
]));
$this->assertTrue(
$this->input->isValid(),
'isValid() value not match. Detail . ' . json_encode($this->input->getMessages())
);

// Negative test
$values[1]['tmp_name'] = 'file-not-found';
$this->input->setValue($values);
$this->assertFalse($this->input->isValid());
}

public function testFallbackValueVsIsValidRules(
Expand Down
40 changes: 17 additions & 23 deletions test/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function testFallbackValueVsIsValidRules($required, $fallbackValue, $orig
$input = $this->input;

$input->setRequired($required);
$input->setValidatorChain($this->createValidatorChainMock($isValid, $originalValue));
$input->setValidatorChain($this->createValidatorChainMock([[$originalValue, null, $isValid]]));
$input->setFallbackValue($fallbackValue);
$input->setValue($originalValue);

Expand All @@ -135,7 +135,7 @@ public function testFallbackValueVsIsValidRulesWhenValueNotSet($required, $fallb
$input = $this->input;

$input->setRequired($required);
$input->setValidatorChain($this->createValidatorChainMock(null));
$input->setValidatorChain($this->createValidatorChainMock());
$input->setFallbackValue($fallbackValue);

$this->assertTrue(
Expand Down Expand Up @@ -214,7 +214,7 @@ public function testNotRequiredWithoutFallbackAndValueNotSetThenIsValid()
$input->setRequired(false);

// Validator should not to be called
$input->setValidatorChain($this->createValidatorChainMock(null, null));
$input->setValidatorChain($this->createValidatorChainMock());
$this->assertTrue(
$input->isValid(),
'isValid() should be return always true when is not required, and no data is set. Detail: ' .
Expand All @@ -241,7 +241,7 @@ public function testRetrievingValueFiltersTheValue()
$valueRaw = $this->getDummyValue();
$valueFiltered = $this->getDummyValue(false);

$filterChain = $this->createFilterChainMock($valueRaw, $valueFiltered);
$filterChain = $this->createFilterChainMock([[$valueRaw, $valueFiltered]]);

$this->input->setFilterChain($filterChain);
$this->input->setValue($valueRaw);
Expand All @@ -266,9 +266,9 @@ public function testValidationOperatesOnFilteredValue()
$valueRaw = $this->getDummyValue();
$valueFiltered = $this->getDummyValue(false);

$filterChain = $this->createFilterChainMock($valueRaw, $valueFiltered);
$filterChain = $this->createFilterChainMock([[$valueRaw, $valueFiltered]]);

$validatorChain = $this->createValidatorChainMock(true, $valueFiltered);
$validatorChain = $this->createValidatorChainMock([[$valueFiltered, null, true]]);

$this->input->setFilterChain($filterChain);
$this->input->setValidatorChain($validatorChain);
Expand Down Expand Up @@ -463,10 +463,10 @@ public function isRequiredVsIsValidProvider()
$validatorMsg = ['FooValidator' => 'Invalid Value'];

$validatorInvalid = function ($value, $context = null) use ($validatorMsg) {
return $this->createValidatorChainMock(false, $value, $context, $validatorMsg);
return $this->createValidatorChainMock([[$value, $context, false]], $validatorMsg);
};
$validatorValid = function ($value, $context = null) {
return $this->createValidatorChainMock(true, $value, $context);
return $this->createValidatorChainMock([[$value, $context, true]]);
};

// @codingStandardsIgnoreStart
Expand Down Expand Up @@ -499,47 +499,41 @@ protected function createInputInterfaceMock()
}

/**
* @param mixed $valueRaw
* @param mixed $valueFiltered
* @param array $valueMap
*
* @return FilterChain|MockObject
*/
protected function createFilterChainMock($valueRaw = null, $valueFiltered = null)
protected function createFilterChainMock(array $valueMap = [])
{
/** @var FilterChain|MockObject $filterChain */
$filterChain = $this->getMock(FilterChain::class);

$filterChain->method('filter')
->with($valueRaw)
->willReturn($valueFiltered)
->willReturnMap($valueMap)
;

return $filterChain;
}

/**
* @param null|bool $isValid If set stub isValid method for return the argument value.
* @param mixed $value
* @param mixed $context
* @param array $valueMap
* @param string[] $messages
*
* @return ValidatorChain|MockObject
*/
protected function createValidatorChainMock($isValid = null, $value = null, $context = null, $messages = [])
protected function createValidatorChainMock(array $valueMap = [], $messages = [])
{
/** @var ValidatorChain|MockObject $validatorChain */
$validatorChain = $this->getMock(ValidatorChain::class);

if (($isValid === false) || ($isValid === true)) {
$validatorChain->expects($this->once())
if (empty($valueMap)) {
$validatorChain->expects($this->never())
->method('isValid')
->with($value, $context)
->willReturn($isValid)
;
} else {
$validatorChain->expects($this->never())
$validatorChain->expects($this->atLeastOnce())
->method('isValid')
->with($value, $context)
->willReturnMap($valueMap)
;
}

Expand Down

0 comments on commit fb1e741

Please sign in to comment.