99
1010namespace ZendTest \InputFilter ;
1111
12- use PHPUnit_Framework_MockObject_MockObject as MockObject ;
13- use Zend \Filter ;
1412use 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 (
0 commit comments