1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Koded\Http; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Koded\Http\ServerRequest; |
7
|
|
|
use Koded\Http\UploadedFile; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use function Koded\Http\build_files_array; |
10
|
|
|
use function Koded\Http\normalize_files_array; |
11
|
|
|
|
12
|
|
|
class FilesTraitTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
public function test_with_empty_files() |
16
|
|
|
{ |
17
|
|
|
$request = new ServerRequest; |
18
|
|
|
$this->assertSame([], $request->getUploadedFiles()); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function test_simple_file() |
22
|
|
|
{ |
23
|
|
|
$file = '/tmp/y4k9a7fm'; |
24
|
|
|
touch($file); |
25
|
|
|
|
26
|
|
|
$_FILES = include __DIR__ . '/fixtures/simple-file-array.php'; |
27
|
|
|
|
28
|
|
|
$request = new ServerRequest; |
29
|
|
|
$this->assertInstanceOf(UploadedFile::class, $request->getUploadedFiles()['test']); |
30
|
|
|
|
31
|
|
|
unlink($file); |
32
|
|
|
$_FILES = []; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function test_nested_files() |
36
|
|
|
{ |
37
|
|
|
$file1 = '/tmp/H3b00Ul2kq'; |
38
|
|
|
$file2 = '/tmp/gt288ksoY3E'; |
39
|
|
|
touch($file1); |
40
|
|
|
touch($file2); |
41
|
|
|
|
42
|
|
|
// the uber lame multiple _FILES |
43
|
|
|
|
44
|
|
|
$_FILES = [ |
45
|
|
|
'test' => [ |
46
|
|
|
'name' => [ |
47
|
|
|
'filename1.txt', |
48
|
|
|
'filename2.txt', |
49
|
|
|
], |
50
|
|
|
'tmp_name' => [ |
51
|
|
|
$file1, |
52
|
|
|
$file2, |
53
|
|
|
], |
54
|
|
|
'size' => [ |
55
|
|
|
42, |
56
|
|
|
24, |
57
|
|
|
], |
58
|
|
|
'error' => [ |
59
|
|
|
UPLOAD_ERR_OK, |
60
|
|
|
UPLOAD_ERR_OK, |
61
|
|
|
], |
62
|
|
|
'type' => [ |
63
|
|
|
'text/plain', |
64
|
|
|
'text/plain', |
65
|
|
|
] |
66
|
|
|
] |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
$request = new ServerRequest; |
70
|
|
|
$this->assertInstanceOf(UploadedFile::class, $request->getUploadedFiles()['test'][0]); |
71
|
|
|
$this->assertInstanceOf(UploadedFile::class, $request->getUploadedFiles()['test'][1]); |
72
|
|
|
|
73
|
|
|
unlink($file1); |
74
|
|
|
unlink($file2); |
75
|
|
|
$_FILES = []; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function test_with_ridiculously_nested_file() |
79
|
|
|
{ |
80
|
|
|
$file1 = '/tmp/php3liuXo'; |
81
|
|
|
$file2 = '/tmp/phpYAvEdT'; |
82
|
|
|
touch($file1); |
83
|
|
|
touch($file2); |
84
|
|
|
$_FILES = include __DIR__ . '/fixtures/very-complicated-files-array.php'; |
85
|
|
|
|
86
|
|
|
$request = new ServerRequest; |
87
|
|
|
$this->assertInstanceOf(UploadedFile::class, $request->getUploadedFiles()['test'][0]['a']['b']['c']); |
88
|
|
|
$this->assertInstanceOf(UploadedFile::class, $request->getUploadedFiles()['test'][1]['a']['b']['c']); |
89
|
|
|
|
90
|
|
|
unlink($file1); |
91
|
|
|
unlink($file2); |
92
|
|
|
$_FILES = []; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function test_with_file_instance() |
96
|
|
|
{ |
97
|
|
|
$file = '/tmp/y4k9a7fm'; |
98
|
|
|
touch($file); |
99
|
|
|
|
100
|
|
|
$_FILES = include __DIR__ . '/fixtures/simple-file-array.php'; |
101
|
|
|
|
102
|
|
|
$request = new ServerRequest; |
103
|
|
|
$this->assertInstanceOf(UploadedFile::class, $request->getUploadedFiles()['test']); |
104
|
|
|
|
105
|
|
|
unlink($file); |
106
|
|
|
$_FILES = []; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function test_with_files_replacement() |
110
|
|
|
{ |
111
|
|
|
$file = '/tmp/y4k9a7fm'; |
112
|
|
|
touch($file); |
113
|
|
|
|
114
|
|
|
$request = new ServerRequest; |
115
|
|
|
$request = $request->withUploadedFiles(include __DIR__ . '/fixtures/simple-file-array.php'); |
116
|
|
|
$files = $request->getUploadedFiles(); |
117
|
|
|
|
118
|
|
|
$this->assertIsArray($files); |
119
|
|
|
$this->assertInstanceOf(UploadedFile::class, $files['test']); |
120
|
|
|
|
121
|
|
|
unlink($file); |
122
|
|
|
$_FILES = []; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function test_files_array_with_file_instance() |
126
|
|
|
{ |
127
|
|
|
$normalized = normalize_files_array(include __DIR__ . '/fixtures/very-complicated-files-array.php'); |
128
|
|
|
|
129
|
|
|
$normalized['test'][0]['a']['b']['c'] = new UploadedFile([ |
130
|
|
|
'tmp_name' => __DIR__ . '/fixtures/very-complicated-files-array.php', |
131
|
|
|
]); |
132
|
|
|
|
133
|
|
|
$this->assertInstanceOf(UploadedFile::class, build_files_array($normalized)['test'][0]['a']['b']['c']); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function test_files_array_exception() |
137
|
|
|
{ |
138
|
|
|
$this->expectException(InvalidArgumentException::class); |
139
|
|
|
$this->expectExceptionMessage('The uploaded file is not supported'); |
140
|
|
|
|
141
|
|
|
new UploadedFile([]); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|