1010namespace ZendTest \InputFilter ;
1111
1212use PHPUnit_Framework_MockObject_MockObject as MockObject ;
13- use Zend \Filter ;
1413use Zend \InputFilter \FileInput ;
1514use 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 )
0 commit comments