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

Commit abe578c

Browse files
committed
Merge branch 'hotfix/84'
Close #84
2 parents ee871d4 + ed15de0 commit abe578c

File tree

3 files changed

+62
-158
lines changed

3 files changed

+62
-158
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: 22 additions & 123 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,84 +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());
165-
}
166-
167-
public function testGetMessagesReturnsValidationMessages()
168-
{
169-
$this->input->setAutoPrependUploadValidator(true);
170-
$this->input->setValue([
171-
'tmp_name' => __FILE__,
172-
'name' => 'foo',
173-
'size' => 1,
174-
'error' => 0,
175-
]);
176-
$this->assertFalse($this->input->isValid());
177-
$messages = $this->input->getMessages();
178-
$this->assertArrayHasKey(Validator\File\UploadFile::ATTACK, $messages);
179-
}
180-
181-
public function testCanValidateArrayOfMultiFileData()
182-
{
183-
$values = [
184-
[
185-
'tmp_name' => __FILE__,
186-
'name' => 'foo',
187-
],
188-
[
189-
'tmp_name' => __FILE__,
190-
'name' => 'bar',
191-
],
192-
[
193-
'tmp_name' => __FILE__,
194-
'name' => 'baz',
195-
],
196-
];
197-
$this->input->setValue($values);
198-
$validator = new Validator\File\Exists();
199-
$this->input->getValidatorChain()->attach($validator);
200-
$this->assertTrue(
201-
$this->input->isValid(),
202-
'isValid() value not match. Detail . ' . json_encode($this->input->getMessages())
203-
);
204-
205-
// Negative test
206-
$values[1]['tmp_name'] = 'file-not-found';
207-
$this->input->setValue($values);
208-
$this->assertFalse($this->input->isValid());
209112
}
210113

211114
public function testAutoPrependUploadValidatorIsOnByDefault()
@@ -281,19 +184,15 @@ public function testValidationsRunWithoutFileArrayDueToAjaxPost()
281184
$this->assertTrue($this->input->isRequired());
282185
$this->input->setValue('');
283186

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);
187+
$expectedNormalizedValue = [
188+
'tmp_name' => '',
189+
'name' => '',
190+
'size' => 0,
191+
'type' => '',
192+
'error' => UPLOAD_ERR_NO_FILE,
193+
];
194+
$this->input->setValidatorChain($this->createValidatorChainMock([[$expectedNormalizedValue, null, false]]));
292195
$this->assertFalse($this->input->isValid());
293-
294-
$validators = $validatorChain->getValidators();
295-
$this->assertEquals(1, count($validators));
296-
$this->assertEquals($uploadMock, $validators[0]['instance']);
297196
}
298197

299198
public function testNotEmptyValidatorAddedWhenIsValidIsCalled($value = null)

test/InputTest.php

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

149149
$input->setRequired($required);
150-
$input->setValidatorChain($this->createValidatorChainMock($isValid, $originalValue));
150+
$input->setValidatorChain($this->createValidatorChainMock([[$originalValue, null, $isValid]]));
151151
$input->setFallbackValue($fallbackValue);
152152
$input->setValue($originalValue);
153153

@@ -172,7 +172,7 @@ public function testFallbackValueVsIsValidRulesWhenValueNotSet($required, $fallb
172172
$input->setContinueIfEmpty(true);
173173

174174
$input->setRequired($required);
175-
$input->setValidatorChain($this->createValidatorChainMock(null));
175+
$input->setValidatorChain($this->createValidatorChainMock());
176176
$input->setFallbackValue($fallbackValue);
177177

178178
$this->assertTrue(
@@ -329,7 +329,7 @@ public function testRetrievingValueFiltersTheValue()
329329
$valueRaw = $this->getDummyValue();
330330
$valueFiltered = $this->getDummyValue(false);
331331

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

334334
$this->input->setFilterChain($filterChain);
335335
$this->input->setValue($valueRaw);
@@ -354,9 +354,9 @@ public function testValidationOperatesOnFilteredValue()
354354
$valueRaw = $this->getDummyValue();
355355
$valueFiltered = $this->getDummyValue(false);
356356

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

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

361361
$this->input->setAllowEmpty(true);
362362
$this->input->setFilterChain($filterChain);
@@ -424,7 +424,7 @@ public function testRequiredNotEmptyValidatorNotAddedWhenOneExists($value)
424424
*/
425425
public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain($valueRaw, $valueFiltered)
426426
{
427-
$filterChain = $this->createFilterChainMock($valueRaw, $valueFiltered);
427+
$filterChain = $this->createFilterChainMock([[$valueRaw, $valueFiltered]]);
428428
$validatorChain = $this->input->getValidatorChain();
429429

430430
$this->input->setRequired(true);
@@ -826,47 +826,41 @@ protected function createInputInterfaceMock()
826826
}
827827

828828
/**
829-
* @param mixed $valueRaw
830-
* @param mixed $valueFiltered
829+
* @param array $valueMap
831830
*
832831
* @return FilterChain|MockObject
833832
*/
834-
protected function createFilterChainMock($valueRaw = null, $valueFiltered = null)
833+
protected function createFilterChainMock(array $valueMap = [])
835834
{
836835
/** @var FilterChain|MockObject $filterChain */
837836
$filterChain = $this->getMock(FilterChain::class);
838837

839838
$filterChain->method('filter')
840-
->with($valueRaw)
841-
->willReturn($valueFiltered)
839+
->willReturnMap($valueMap)
842840
;
843841

844842
return $filterChain;
845843
}
846844

847845
/**
848-
* @param null|bool $isValid If set stub isValid method for return the argument value.
849-
* @param mixed $value
850-
* @param mixed $context
846+
* @param array $valueMap
851847
* @param string[] $messages
852848
*
853849
* @return ValidatorChain|MockObject
854850
*/
855-
protected function createValidatorChainMock($isValid = null, $value = null, $context = null, $messages = [])
851+
protected function createValidatorChainMock(array $valueMap = [], $messages = [])
856852
{
857853
/** @var ValidatorChain|MockObject $validatorChain */
858854
$validatorChain = $this->getMock(ValidatorChain::class);
859855

860-
if (($isValid === false) || ($isValid === true)) {
861-
$validatorChain->expects($this->once())
856+
if (empty($valueMap)) {
857+
$validatorChain->expects($this->never())
862858
->method('isValid')
863-
->with($value, $context)
864-
->willReturn($isValid)
865859
;
866860
} else {
867-
$validatorChain->expects($this->never())
861+
$validatorChain->expects($this->atLeastOnce())
868862
->method('isValid')
869-
->with($value, $context)
863+
->willReturnMap($valueMap)
870864
;
871865
}
872866

0 commit comments

Comments
 (0)