9
9
10
10
namespace ZendTest \InputFilter ;
11
11
12
- use PHPUnit_Framework_MockObject_MockObject as MockObject ;
13
- use Zend \Filter ;
14
12
use Zend \InputFilter \FileInput ;
15
- use Zend \Validator ;
16
13
17
14
/**
18
15
* @covers Zend\InputFilter\FileInput
@@ -35,22 +32,7 @@ public function testRetrievingValueFiltersTheValueOnlyAfterValidating()
35
32
$ this ->input ->setValue ($ value );
36
33
37
34
$ 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 ]]));
54
36
55
37
$ this ->assertEquals ($ value , $ this ->input ->getValue ());
56
38
$ this ->assertTrue (
@@ -70,30 +52,20 @@ public function testCanFilterArrayOfMultiFileData()
70
52
$ this ->input ->setValue ($ values );
71
53
72
54
$ 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
+ ]));
89
61
90
62
$ this ->assertEquals ($ values , $ this ->input ->getValue ());
91
63
$ this ->assertTrue (
92
64
$ this ->input ->isValid (),
93
65
'isValid() value not match. Detail . ' . json_encode ($ this ->input ->getMessages ())
94
66
);
95
67
$ this ->assertEquals (
96
- [ $ newValue , $ newValue , $ newValue ] ,
68
+ $ filteredValue ,
97
69
$ this ->input ->getValue ()
98
70
);
99
71
}
@@ -102,8 +74,10 @@ public function testCanRetrieveRawValue()
102
74
{
103
75
$ value = ['tmp_name ' => 'bar ' ];
104
76
$ 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
+
107
81
$ this ->assertEquals ($ value , $ this ->input ->getRawValue ());
108
82
}
109
83
@@ -123,40 +97,11 @@ public function testValidationOperatesBeforeFiltering()
123
97
$ this ->input ->setValue ($ badValue );
124
98
125
99
$ 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 ]]));
142
102
143
- $ validator = new Validator \File \Exists ();
144
- $ this ->input ->getValidatorChain ()->attach ($ validator );
145
103
$ this ->assertFalse ($ this ->input ->isValid ());
146
104
$ 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 ());
160
105
}
161
106
162
107
public function testCanValidateArrayOfMultiFileData ()
@@ -176,17 +121,15 @@ public function testCanValidateArrayOfMultiFileData()
176
121
],
177
122
];
178
123
$ 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
+ ]));
181
129
$ this ->assertTrue (
182
130
$ this ->input ->isValid (),
183
131
'isValid() value not match. Detail . ' . json_encode ($ this ->input ->getMessages ())
184
132
);
185
-
186
- // Negative test
187
- $ values [1 ]['tmp_name ' ] = 'file-not-found ' ;
188
- $ this ->input ->setValue ($ values );
189
- $ this ->assertFalse ($ this ->input ->isValid ());
190
133
}
191
134
192
135
public function testFallbackValueVsIsValidRules (
0 commit comments