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

Commit fb1e741

Browse files
committed
Refactor FileInput test
Remove dependencies with specific implementations
1 parent f1b103f commit fb1e741

File tree

3 files changed

+61
-113
lines changed

3 files changed

+61
-113
lines changed

test/ArrayInputTest.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,39 @@ public function fallbackValueVsIsValidProvider()
4848
return $dataSets;
4949
}
5050

51-
protected function createFilterChainMock($valueRaw = null, $valueFiltered = null)
51+
protected function createFilterChainMock(array $valueMap = [])
5252
{
5353
// ArrayInput filters per each array value
54-
if (is_array($valueRaw)) {
55-
$valueRaw = current($valueRaw);
56-
}
57-
58-
if (is_array($valueFiltered)) {
59-
$valueFiltered = current($valueFiltered);
60-
}
54+
$valueMap = array_map(
55+
function ($values) {
56+
if (is_array($values[0])) {
57+
$values[0] = current($values[0]);
58+
}
59+
if (is_array($values[1])) {
60+
$values[1] = current($values[1]);
61+
}
62+
return $values;
63+
},
64+
$valueMap
65+
);
6166

62-
return parent::createFilterChainMock($valueRaw, $valueFiltered);
67+
return parent::createFilterChainMock($valueMap);
6368
}
6469

65-
protected function createValidatorChainMock($isValid = null, $value = null, $context = null, $messages = [])
70+
protected function createValidatorChainMock(array $valueMap = [], $messages = [])
6671
{
6772
// ArrayInput validates per each array value
68-
if (is_array($value)) {
69-
$value = current($value);
70-
}
73+
$valueMap = array_map(
74+
function ($values) {
75+
if (is_array($values[0])) {
76+
$values[0] = current($values[0]);
77+
}
78+
return $values;
79+
},
80+
$valueMap
81+
);
7182

72-
return parent::createValidatorChainMock($isValid, $value, $context, $messages);
83+
return parent::createValidatorChainMock($valueMap, $messages);
7384
}
7485

7586
protected function getDummyValue($raw = true)

test/FileInputTest.php

Lines changed: 19 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99

1010
namespace ZendTest\InputFilter;
1111

12-
use PHPUnit_Framework_MockObject_MockObject as MockObject;
13-
use Zend\Filter;
1412
use Zend\InputFilter\FileInput;
15-
use Zend\Validator;
1613

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

3734
$newValue = ['tmp_name' => 'foo'];
38-
/** @var Filter\File\Rename|MockObject $filterMock */
39-
$filterMock = $this->getMockBuilder(Filter\File\Rename::class)
40-
->disableOriginalConstructor()
41-
->getMock();
42-
$filterMock->expects($this->any())
43-
->method('filter')
44-
->will($this->returnValue($newValue));
45-
46-
// Why not attach mocked filter directly?
47-
// No worky without wrapping in a callback.
48-
// Missing something in mock setup?
49-
$this->input->getFilterChain()->attach(
50-
function ($value) use ($filterMock) {
51-
return $filterMock->filter($value);
52-
}
53-
);
35+
$this->input->setFilterChain($this->createFilterChainMock([[$value, $newValue]]));
5436

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

7254
$newValue = ['tmp_name' => 'new'];
73-
/** @var Filter\File\Rename|MockObject $filterMock */
74-
$filterMock = $this->getMockBuilder(Filter\File\Rename::class)
75-
->disableOriginalConstructor()
76-
->getMock();
77-
$filterMock->expects($this->any())
78-
->method('filter')
79-
->will($this->returnValue($newValue));
80-
81-
// Why not attach mocked filter directly?
82-
// No worky without wrapping in a callback.
83-
// Missing something in mock setup?
84-
$this->input->getFilterChain()->attach(
85-
function ($value) use ($filterMock) {
86-
return $filterMock->filter($value);
87-
}
88-
);
55+
$filteredValue = [$newValue, $newValue, $newValue];
56+
$this->input->setFilterChain($this->createFilterChainMock([
57+
[$values[0], $newValue],
58+
[$values[1], $newValue],
59+
[$values[2], $newValue],
60+
]));
8961

9062
$this->assertEquals($values, $this->input->getValue());
9163
$this->assertTrue(
9264
$this->input->isValid(),
9365
'isValid() value not match. Detail . ' . json_encode($this->input->getMessages())
9466
);
9567
$this->assertEquals(
96-
[$newValue, $newValue, $newValue],
68+
$filteredValue,
9769
$this->input->getValue()
9870
);
9971
}
@@ -102,8 +74,10 @@ public function testCanRetrieveRawValue()
10274
{
10375
$value = ['tmp_name' => 'bar'];
10476
$this->input->setValue($value);
105-
$filter = new Filter\StringToUpper();
106-
$this->input->getFilterChain()->attach($filter);
77+
78+
$newValue = ['tmp_name' => 'new'];
79+
$this->input->setFilterChain($this->createFilterChainMock([[$value, $newValue]]));
80+
10781
$this->assertEquals($value, $this->input->getRawValue());
10882
}
10983

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

12599
$filteredValue = ['tmp_name' => 'new'];
126-
/** @var Filter\File\Rename|MockObject $filterMock */
127-
$filterMock = $this->getMockBuilder(Filter\File\Rename::class)
128-
->disableOriginalConstructor()
129-
->getMock();
130-
$filterMock->expects($this->any())
131-
->method('filter')
132-
->will($this->returnValue($filteredValue));
133-
134-
// Why not attach mocked filter directly?
135-
// No worky without wrapping in a callback.
136-
// Missing something in mock setup?
137-
$this->input->getFilterChain()->attach(
138-
function ($value) use ($filterMock) {
139-
return $filterMock->filter($value);
140-
}
141-
);
100+
$this->input->setFilterChain($this->createFilterChainMock([[$badValue, $filteredValue]]));
101+
$this->input->setValidatorChain($this->createValidatorChainMock([[$badValue, null, false]]));
142102

143-
$validator = new Validator\File\Exists();
144-
$this->input->getValidatorChain()->attach($validator);
145103
$this->assertFalse($this->input->isValid());
146104
$this->assertEquals($badValue, $this->input->getValue());
147-
148-
$goodValue = [
149-
'tmp_name' => __FILE__,
150-
'name' => 'foo',
151-
'size' => 1,
152-
'error' => 0,
153-
];
154-
$this->input->setValue($goodValue);
155-
$this->assertTrue(
156-
$this->input->isValid(),
157-
'isValid() value not match. Detail . ' . json_encode($this->input->getMessages())
158-
);
159-
$this->assertEquals($filteredValue, $this->input->getValue());
160105
}
161106

162107
public function testCanValidateArrayOfMultiFileData()
@@ -176,17 +121,15 @@ public function testCanValidateArrayOfMultiFileData()
176121
],
177122
];
178123
$this->input->setValue($values);
179-
$validator = new Validator\File\Exists();
180-
$this->input->getValidatorChain()->attach($validator);
124+
$this->input->setValidatorChain($this->createValidatorChainMock([
125+
[$values[0], null, true],
126+
[$values[1], null, true],
127+
[$values[2], null, true],
128+
]));
181129
$this->assertTrue(
182130
$this->input->isValid(),
183131
'isValid() value not match. Detail . ' . json_encode($this->input->getMessages())
184132
);
185-
186-
// Negative test
187-
$values[1]['tmp_name'] = 'file-not-found';
188-
$this->input->setValue($values);
189-
$this->assertFalse($this->input->isValid());
190133
}
191134

192135
public function testFallbackValueVsIsValidRules(

test/InputTest.php

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function testFallbackValueVsIsValidRules($required, $fallbackValue, $orig
111111
$input = $this->input;
112112

113113
$input->setRequired($required);
114-
$input->setValidatorChain($this->createValidatorChainMock($isValid, $originalValue));
114+
$input->setValidatorChain($this->createValidatorChainMock([[$originalValue, null, $isValid]]));
115115
$input->setFallbackValue($fallbackValue);
116116
$input->setValue($originalValue);
117117

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

137137
$input->setRequired($required);
138-
$input->setValidatorChain($this->createValidatorChainMock(null));
138+
$input->setValidatorChain($this->createValidatorChainMock());
139139
$input->setFallbackValue($fallbackValue);
140140

141141
$this->assertTrue(
@@ -214,7 +214,7 @@ public function testNotRequiredWithoutFallbackAndValueNotSetThenIsValid()
214214
$input->setRequired(false);
215215

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

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

246246
$this->input->setFilterChain($filterChain);
247247
$this->input->setValue($valueRaw);
@@ -266,9 +266,9 @@ public function testValidationOperatesOnFilteredValue()
266266
$valueRaw = $this->getDummyValue();
267267
$valueFiltered = $this->getDummyValue(false);
268268

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

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

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

465465
$validatorInvalid = function ($value, $context = null) use ($validatorMsg) {
466-
return $this->createValidatorChainMock(false, $value, $context, $validatorMsg);
466+
return $this->createValidatorChainMock([[$value, $context, false]], $validatorMsg);
467467
};
468468
$validatorValid = function ($value, $context = null) {
469-
return $this->createValidatorChainMock(true, $value, $context);
469+
return $this->createValidatorChainMock([[$value, $context, true]]);
470470
};
471471

472472
// @codingStandardsIgnoreStart
@@ -499,47 +499,41 @@ protected function createInputInterfaceMock()
499499
}
500500

501501
/**
502-
* @param mixed $valueRaw
503-
* @param mixed $valueFiltered
502+
* @param array $valueMap
504503
*
505504
* @return FilterChain|MockObject
506505
*/
507-
protected function createFilterChainMock($valueRaw = null, $valueFiltered = null)
506+
protected function createFilterChainMock(array $valueMap = [])
508507
{
509508
/** @var FilterChain|MockObject $filterChain */
510509
$filterChain = $this->getMock(FilterChain::class);
511510

512511
$filterChain->method('filter')
513-
->with($valueRaw)
514-
->willReturn($valueFiltered)
512+
->willReturnMap($valueMap)
515513
;
516514

517515
return $filterChain;
518516
}
519517

520518
/**
521-
* @param null|bool $isValid If set stub isValid method for return the argument value.
522-
* @param mixed $value
523-
* @param mixed $context
519+
* @param array $valueMap
524520
* @param string[] $messages
525521
*
526522
* @return ValidatorChain|MockObject
527523
*/
528-
protected function createValidatorChainMock($isValid = null, $value = null, $context = null, $messages = [])
524+
protected function createValidatorChainMock(array $valueMap = [], $messages = [])
529525
{
530526
/** @var ValidatorChain|MockObject $validatorChain */
531527
$validatorChain = $this->getMock(ValidatorChain::class);
532528

533-
if (($isValid === false) || ($isValid === true)) {
534-
$validatorChain->expects($this->once())
529+
if (empty($valueMap)) {
530+
$validatorChain->expects($this->never())
535531
->method('isValid')
536-
->with($value, $context)
537-
->willReturn($isValid)
538532
;
539533
} else {
540-
$validatorChain->expects($this->never())
534+
$validatorChain->expects($this->atLeastOnce())
541535
->method('isValid')
542-
->with($value, $context)
536+
->willReturnMap($valueMap)
543537
;
544538
}
545539

0 commit comments

Comments
 (0)