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

Commit 779b955

Browse files
committed
[FileInput] Reduce test dependencies
1 parent a46714e commit 779b955

File tree

3 files changed

+67
-121
lines changed

3 files changed

+67
-121
lines changed

test/ArrayInputTest.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,28 +68,39 @@ public function mixedValueProvider()
6868
return $dataSets;
6969
}
7070

71-
protected function createFilterChainMock($valueRaw = null, $valueFiltered = null)
71+
protected function createFilterChainMock(array $valueMap = [])
7272
{
7373
// ArrayInput filters per each array value
74-
if (is_array($valueRaw)) {
75-
$valueRaw = current($valueRaw);
76-
}
77-
78-
if (is_array($valueFiltered)) {
79-
$valueFiltered = current($valueFiltered);
80-
}
74+
$valueMap = array_map(
75+
function ($values) {
76+
if (is_array($values[0])) {
77+
$values[0] = current($values[0]);
78+
}
79+
if (is_array($values[1])) {
80+
$values[1] = current($values[1]);
81+
}
82+
return $values;
83+
},
84+
$valueMap
85+
);
8186

82-
return parent::createFilterChainMock($valueRaw, $valueFiltered);
87+
return parent::createFilterChainMock($valueMap);
8388
}
8489

85-
protected function createValidatorChainMock($isValid = null, $value = null, $context = null, $messages = [])
90+
protected function createValidatorChainMock(array $valueMap = [], $messages = [])
8691
{
8792
// ArrayInput validates per each array value
88-
if (is_array($value)) {
89-
$value = current($value);
90-
}
93+
$valueMap = array_map(
94+
function ($values) {
95+
if (is_array($values[0])) {
96+
$values[0] = current($values[0]);
97+
}
98+
return $values;
99+
},
100+
$valueMap
101+
);
91102

92-
return parent::createValidatorChainMock($isValid, $value, $context, $messages);
103+
return parent::createValidatorChainMock($valueMap, $messages);
93104
}
94105

95106
protected function createNonEmptyValidatorMock($isValid, $value, $context = null)

test/FileInputTest.php

Lines changed: 27 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace ZendTest\InputFilter;
1111

1212
use PHPUnit_Framework_MockObject_MockObject as MockObject;
13-
use Zend\Filter;
1413
use Zend\InputFilter\FileInput;
1514
use Zend\Validator;
1615

@@ -40,22 +39,7 @@ public function testRetrievingValueFiltersTheValueOnlyAfterValidating()
4039
$this->input->setValue($value);
4140

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

6044
$this->assertEquals($value, $this->input->getValue());
6145
$this->assertTrue(
@@ -75,30 +59,20 @@ public function testCanFilterArrayOfMultiFileData()
7559
$this->input->setValue($values);
7660

7761
$newValue = ['tmp_name' => 'new'];
78-
/** @var Filter\File\Rename|MockObject $filterMock */
79-
$filterMock = $this->getMockBuilder(Filter\File\Rename::class)
80-
->disableOriginalConstructor()
81-
->getMock();
82-
$filterMock->expects($this->any())
83-
->method('filter')
84-
->will($this->returnValue($newValue));
85-
86-
// Why not attach mocked filter directly?
87-
// No worky without wrapping in a callback.
88-
// Missing something in mock setup?
89-
$this->input->getFilterChain()->attach(
90-
function ($value) use ($filterMock) {
91-
return $filterMock->filter($value);
92-
}
93-
);
62+
$filteredValue = [$newValue, $newValue, $newValue];
63+
$this->input->setFilterChain($this->createFilterChainMock([
64+
[$values[0], $newValue],
65+
[$values[1], $newValue],
66+
[$values[2], $newValue],
67+
]));
9468

9569
$this->assertEquals($values, $this->input->getValue());
9670
$this->assertTrue(
9771
$this->input->isValid(),
9872
'isValid() value not match. Detail . ' . json_encode($this->input->getMessages())
9973
);
10074
$this->assertEquals(
101-
[$newValue, $newValue, $newValue],
75+
$filteredValue,
10276
$this->input->getValue()
10377
);
10478
}
@@ -107,8 +81,10 @@ public function testCanRetrieveRawValue()
10781
{
10882
$value = ['tmp_name' => 'bar'];
10983
$this->input->setValue($value);
110-
$filter = new Filter\StringToUpper();
111-
$this->input->getFilterChain()->attach($filter);
84+
85+
$newValue = ['tmp_name' => 'new'];
86+
$this->input->setFilterChain($this->createFilterChainMock([[$value, $newValue]]));
87+
11288
$this->assertEquals($value, $this->input->getRawValue());
11389
}
11490

@@ -128,40 +104,11 @@ public function testValidationOperatesBeforeFiltering()
128104
$this->input->setValue($badValue);
129105

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

148-
$validator = new Validator\File\Exists();
149-
$this->input->getValidatorChain()->attach($validator);
150110
$this->assertFalse($this->input->isValid());
151111
$this->assertEquals($badValue, $this->input->getValue());
152-
153-
$goodValue = [
154-
'tmp_name' => __FILE__,
155-
'name' => 'foo',
156-
'size' => 1,
157-
'error' => 0,
158-
];
159-
$this->input->setValue($goodValue);
160-
$this->assertTrue(
161-
$this->input->isValid(),
162-
'isValid() value not match. Detail . ' . json_encode($this->input->getMessages())
163-
);
164-
$this->assertEquals($filteredValue, $this->input->getValue());
165112
}
166113

167114
public function testGetMessagesReturnsValidationMessages()
@@ -195,17 +142,15 @@ public function testCanValidateArrayOfMultiFileData()
195142
],
196143
];
197144
$this->input->setValue($values);
198-
$validator = new Validator\File\Exists();
199-
$this->input->getValidatorChain()->attach($validator);
145+
$this->input->setValidatorChain($this->createValidatorChainMock([
146+
[$values[0], null, true],
147+
[$values[1], null, true],
148+
[$values[2], null, true],
149+
]));
200150
$this->assertTrue(
201151
$this->input->isValid(),
202152
'isValid() value not match. Detail . ' . json_encode($this->input->getMessages())
203153
);
204-
205-
// Negative test
206-
$values[1]['tmp_name'] = 'file-not-found';
207-
$this->input->setValue($values);
208-
$this->assertFalse($this->input->isValid());
209154
}
210155

211156
public function testAutoPrependUploadValidatorIsOnByDefault()
@@ -281,19 +226,15 @@ public function testValidationsRunWithoutFileArrayDueToAjaxPost()
281226
$this->assertTrue($this->input->isRequired());
282227
$this->input->setValue('');
283228

284-
/** @var Validator\File\UploadFile|MockObject $uploadMock */
285-
$uploadMock = $this->getMock(Validator\File\UploadFile::class, ['isValid']);
286-
$uploadMock->expects($this->exactly(1))
287-
->method('isValid')
288-
->will($this->returnValue(false));
289-
290-
$validatorChain = $this->input->getValidatorChain();
291-
$validatorChain->prependValidator($uploadMock);
229+
$expectedNormalizedValue = [
230+
'tmp_name' => '',
231+
'name' => '',
232+
'size' => 0,
233+
'type' => '',
234+
'error' => UPLOAD_ERR_NO_FILE,
235+
];
236+
$this->input->setValidatorChain($this->createValidatorChainMock([[$expectedNormalizedValue, null, false]]));
292237
$this->assertFalse($this->input->isValid());
293-
294-
$validators = $validatorChain->getValidators();
295-
$this->assertEquals(1, count($validators));
296-
$this->assertEquals($uploadMock, $validators[0]['instance']);
297238
}
298239

299240
public function testNotEmptyValidatorAddedWhenIsValidIsCalled($value = null)

test/InputTest.php

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public function testFallbackValueVsIsValidRules($required, $fallbackValue, $orig
140140
$input->setContinueIfEmpty(true);
141141

142142
$input->setRequired($required);
143-
$input->setValidatorChain($this->createValidatorChainMock($isValid, $originalValue));
143+
$input->setValidatorChain($this->createValidatorChainMock([[$originalValue, null, $isValid]]));
144144
$input->setFallbackValue($fallbackValue);
145145
$input->setValue($originalValue);
146146

@@ -165,7 +165,7 @@ public function testFallbackValueVsIsValidRulesWhenValueNotSet($required, $fallb
165165
$input->setContinueIfEmpty(true);
166166

167167
$input->setRequired($required);
168-
$input->setValidatorChain($this->createValidatorChainMock(null));
168+
$input->setValidatorChain($this->createValidatorChainMock());
169169
$input->setFallbackValue($fallbackValue);
170170

171171
$this->assertTrue(
@@ -289,7 +289,7 @@ public function testRetrievingValueFiltersTheValue()
289289
$valueRaw = $this->getDummyValue();
290290
$valueFiltered = $this->getDummyValue(false);
291291

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

294294
$this->input->setFilterChain($filterChain);
295295
$this->input->setValue($valueRaw);
@@ -314,9 +314,9 @@ public function testValidationOperatesOnFilteredValue()
314314
$valueRaw = $this->getDummyValue();
315315
$valueFiltered = $this->getDummyValue(false);
316316

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

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

321321
$this->input->setAllowEmpty(true);
322322
$this->input->setFilterChain($filterChain);
@@ -384,7 +384,7 @@ public function testRequiredNotEmptyValidatorNotAddedWhenOneExists($value)
384384
*/
385385
public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain($valueRaw, $valueFiltered)
386386
{
387-
$filterChain = $this->createFilterChainMock($valueRaw, $valueFiltered);
387+
$filterChain = $this->createFilterChainMock([[$valueRaw, $valueFiltered]]);
388388
$validatorChain = $this->input->getValidatorChain();
389389

390390
$this->input->setRequired(true);
@@ -766,47 +766,41 @@ protected function createInputInterfaceMock()
766766
}
767767

768768
/**
769-
* @param mixed $valueRaw
770-
* @param mixed $valueFiltered
769+
* @param array $valueMap
771770
*
772771
* @return FilterChain|MockObject
773772
*/
774-
protected function createFilterChainMock($valueRaw = null, $valueFiltered = null)
773+
protected function createFilterChainMock(array $valueMap = [])
775774
{
776775
/** @var FilterChain|MockObject $filterChain */
777776
$filterChain = $this->getMock(FilterChain::class);
778777

779778
$filterChain->method('filter')
780-
->with($valueRaw)
781-
->willReturn($valueFiltered)
779+
->willReturnMap($valueMap)
782780
;
783781

784782
return $filterChain;
785783
}
786784

787785
/**
788-
* @param null|bool $isValid If set stub isValid method for return the argument value.
789-
* @param mixed $value
790-
* @param mixed $context
786+
* @param array $valueMap
791787
* @param string[] $messages
792788
*
793789
* @return ValidatorChain|MockObject
794790
*/
795-
protected function createValidatorChainMock($isValid = null, $value = null, $context = null, $messages = [])
791+
protected function createValidatorChainMock(array $valueMap = [], $messages = [])
796792
{
797793
/** @var ValidatorChain|MockObject $validatorChain */
798794
$validatorChain = $this->getMock(ValidatorChain::class);
799795

800-
if (($isValid === false) || ($isValid === true)) {
801-
$validatorChain->expects($this->once())
796+
if (empty($valueMap)) {
797+
$validatorChain->expects($this->never())
802798
->method('isValid')
803-
->with($value, $context)
804-
->willReturn($isValid)
805799
;
806800
} else {
807-
$validatorChain->expects($this->never())
801+
$validatorChain->expects($this->atLeastOnce())
808802
->method('isValid')
809-
->with($value, $context)
803+
->willReturnMap($valueMap)
810804
;
811805
}
812806

0 commit comments

Comments
 (0)