@@ -35,6 +35,9 @@ protected function setUp(): void
35
35
}
36
36
37
37
$ _FILES = [];
38
+
39
+ // Set the mock's return value to true
40
+ move_uploaded_file ('' , '' , true );
38
41
}
39
42
40
43
protected function tearDown (): void
@@ -52,8 +55,7 @@ protected function tearDown(): void
52
55
public function testMove ()
53
56
{
54
57
$ finalFilename = 'fileA ' ;
55
-
56
- $ _FILES = [
58
+ $ _FILES = [
57
59
'userfile1 ' => [
58
60
'name ' => $ finalFilename . '.txt ' ,
59
61
'type ' => 'text/plain ' ,
@@ -76,7 +78,6 @@ public function testMove()
76
78
$ this ->assertTrue ($ collection ->hasFile ('userfile2 ' ));
77
79
78
80
$ destination = $ this ->destination ;
79
-
80
81
// Create the destination if not exists
81
82
if (! is_dir ($ destination )) {
82
83
mkdir ($ destination , 0777 , true );
@@ -94,8 +95,7 @@ public function testMove()
94
95
public function testMoveOverwriting ()
95
96
{
96
97
$ finalFilename = 'file_with_delimiters_underscore ' ;
97
-
98
- $ _FILES = [
98
+ $ _FILES = [
99
99
'userfile1 ' => [
100
100
'name ' => $ finalFilename . '.txt ' ,
101
101
'type ' => 'text/plain ' ,
@@ -126,7 +126,6 @@ public function testMoveOverwriting()
126
126
$ this ->assertTrue ($ collection ->hasFile ('userfile3 ' ));
127
127
128
128
$ destination = $ this ->destination ;
129
-
130
129
// Create the destination if not exists
131
130
if (! is_dir ($ destination )) {
132
131
mkdir ($ destination , 0777 , true );
@@ -146,8 +145,7 @@ public function testMoveOverwriting()
146
145
public function testMoved ()
147
146
{
148
147
$ finalFilename = 'fileA ' ;
149
-
150
- $ _FILES = [
148
+ $ _FILES = [
151
149
'userfile1 ' => [
152
150
'name ' => $ finalFilename . '.txt ' ,
153
151
'type ' => 'text/plain ' ,
@@ -162,7 +160,6 @@ public function testMoved()
162
160
$ this ->assertTrue ($ collection ->hasFile ('userfile1 ' ));
163
161
164
162
$ destination = $ this ->destination ;
165
-
166
163
// Create the destination if not exists
167
164
if (! is_dir ($ destination )) {
168
165
mkdir ($ destination , 0777 , true );
@@ -172,15 +169,16 @@ public function testMoved()
172
169
173
170
$ this ->assertInstanceOf (UploadedFile::class, $ file );
174
171
$ this ->assertFalse ($ file ->hasMoved ());
172
+
175
173
$ file ->move ($ destination , $ file ->getName (), false );
174
+
176
175
$ this ->assertTrue ($ file ->hasMoved ());
177
176
}
178
177
179
178
public function testStore ()
180
179
{
181
180
$ finalFilename = 'fileA ' ;
182
-
183
- $ _FILES = [
181
+ $ _FILES = [
184
182
'userfile1 ' => [
185
183
'name ' => $ finalFilename . '.txt ' ,
186
184
'type ' => 'text/plain ' ,
@@ -195,7 +193,6 @@ public function testStore()
195
193
$ this ->assertTrue ($ collection ->hasFile ('userfile1 ' ));
196
194
197
195
$ destination = $ this ->destination ;
198
-
199
196
// Create the destination if not exists
200
197
if (! is_dir ($ destination )) {
201
198
mkdir ($ destination , 0777 , true );
@@ -204,15 +201,16 @@ public function testStore()
204
201
$ file = $ collection ->getFile ('userfile1 ' );
205
202
206
203
$ this ->assertInstanceOf (UploadedFile::class, $ file );
204
+
207
205
$ path = $ file ->store ($ destination , $ file ->getName ());
206
+
208
207
$ this ->assertSame ($ destination . '/fileA.txt ' , $ path );
209
208
}
210
209
211
210
public function testAlreadyMoved ()
212
211
{
213
212
$ finalFilename = 'fileA ' ;
214
-
215
- $ _FILES = [
213
+ $ _FILES = [
216
214
'userfile1 ' => [
217
215
'name ' => $ finalFilename . '.txt ' ,
218
216
'type ' => 'text/plain ' ,
@@ -227,7 +225,6 @@ public function testAlreadyMoved()
227
225
$ this ->assertTrue ($ collection ->hasFile ('userfile1 ' ));
228
226
229
227
$ destination = $ this ->destination ;
230
-
231
228
// Create the destination if not exists
232
229
if (! is_dir ($ destination )) {
233
230
mkdir ($ destination , 0777 , true );
@@ -254,15 +251,15 @@ public function testInvalidFile()
254
251
];
255
252
256
253
$ destination = $ this ->destination ;
257
-
258
- $ collection = new FileCollection ();
259
- $ file = $ collection ->getFile ('userfile ' );
254
+ $ collection = new FileCollection ();
260
255
261
256
$ this ->expectException (HTTPException::class);
257
+
258
+ $ file = $ collection ->getFile ('userfile ' );
262
259
$ file ->move ($ destination , $ file ->getName (), false );
263
260
}
264
261
265
- public function testFailedMove ()
262
+ public function testFailedMoveBecauseOfWarning ()
266
263
{
267
264
$ _FILES = [
268
265
'userfile ' => [
@@ -275,16 +272,47 @@ public function testFailedMove()
275
272
];
276
273
277
274
$ destination = $ this ->destination ;
278
-
279
275
// Create the destination and make it read only
280
276
if (! is_dir ($ destination )) {
281
277
mkdir ($ destination , 0400 , true );
282
278
}
283
279
284
280
$ collection = new FileCollection ();
285
- $ file = $ collection ->getFile ('userfile ' );
286
281
287
282
$ this ->expectException (HTTPException::class);
283
+
284
+ $ file = $ collection ->getFile ('userfile ' );
285
+ $ file ->move ($ destination , $ file ->getName (), false );
286
+ }
287
+
288
+ public function testFailedMoveBecauseOfFalseReturned ()
289
+ {
290
+ $ _FILES = [
291
+ 'userfile1 ' => [
292
+ 'name ' => 'fileA.txt ' ,
293
+ 'type ' => 'text/plain ' ,
294
+ 'size ' => 124 ,
295
+ 'tmp_name ' => '/tmp/fileA.txt ' ,
296
+ 'error ' => 0 ,
297
+ ],
298
+ ];
299
+
300
+ $ collection = new FileCollection ();
301
+
302
+ $ this ->assertTrue ($ collection ->hasFile ('userfile1 ' ));
303
+
304
+ $ destination = $ this ->destination ;
305
+ // Create the destination if not exists
306
+ if (! is_dir ($ destination )) {
307
+ mkdir ($ destination , 0777 , true );
308
+ }
309
+ // Set the mock's return value to false
310
+ move_uploaded_file ('' , '' , false );
311
+
312
+ $ this ->expectException (HTTPException::class);
313
+ $ this ->expectExceptionMessage ('move_uploaded_file() returned false ' );
314
+
315
+ $ file = $ collection ->getFile ('userfile1 ' );
288
316
$ file ->move ($ destination , $ file ->getName (), false );
289
317
}
290
318
}
@@ -311,10 +339,20 @@ function is_uploaded_file($filename)
311
339
* This overwrite is for testing the move operation.
312
340
*/
313
341
314
- function move_uploaded_file ($ filename , $ destination )
342
+ function move_uploaded_file ($ filename , $ destination, ? bool $ setReturnValue = null )
315
343
{
344
+ static $ return = true ;
345
+
346
+ if ($ setReturnValue !== null ) {
347
+ $ return = $ setReturnValue ;
348
+
349
+ return true ;
350
+ }
351
+
316
352
copy ($ filename , $ destination );
317
353
unlink ($ filename );
354
+
355
+ return $ return ;
318
356
}
319
357
320
358
function rrmdir ($ src )
0 commit comments