-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathUploadedFileTest.php
201 lines (184 loc) · 6.77 KB
/
UploadedFileTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/*
* This file is part of Aplus Framework HTTP Library.
*
* (c) Natan Felles <natanfelles@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\HTTP;
use PHPUnit\Framework\TestCase;
final class UploadedFileTest extends TestCase
{
/**
* @var array<string,int|string>
*/
protected array $file = [
'name' => 'logo.jpg',
'full_path' => 'logo.jpg',
'type' => 'foo/bar',
'size' => 19878,
'tmp_name' => __DIR__ . '/files/logo.png',
'error' => \UPLOAD_ERR_OK,
];
/**
* @var array<string,int|string>
*/
protected array $file2 = [
'name' => 'file.txt',
'full_path' => 'foo/bar/file.txt',
'type' => 'foo/baz',
'size' => 3,
'tmp_name' => __DIR__ . '/files/file.txt',
'error' => \UPLOAD_ERR_CANT_WRITE,
];
protected UploadedFileMock $uploadedFile;
protected UploadedFileMock $uploadedFile2;
protected function setUp() : void
{
$this->uploadedFile = new UploadedFileMock($this->file);
$this->uploadedFile2 = new UploadedFileMock($this->file2);
}
public function testGetClientExtension() : void
{
self::assertSame('jpg', $this->uploadedFile->getClientExtension());
self::assertSame('txt', $this->uploadedFile2->getClientExtension());
}
public function testGetExtension() : void
{
self::assertSame('png', $this->uploadedFile->getExtension());
self::assertSame('png', $this->uploadedFile->getExtension());
self::assertSame('txt', $this->uploadedFile2->getExtension());
}
public function testGetClientType() : void
{
self::assertSame('foo/bar', $this->uploadedFile->getClientType());
self::assertSame('foo/baz', $this->uploadedFile2->getClientType());
}
public function testGetType() : void
{
self::assertSame('image/png', $this->uploadedFile->getType());
self::assertSame('image/png', $this->uploadedFile->getType());
self::assertSame('text/plain', $this->uploadedFile2->getType());
self::assertSame('text/plain', $this->uploadedFile2->getType());
}
public function testGetError() : void
{
self::assertSame(\UPLOAD_ERR_OK, $this->uploadedFile->getError());
self::assertSame(\UPLOAD_ERR_CANT_WRITE, $this->uploadedFile2->getError());
}
public function testGetErrorMessage() : void
{
self::assertSame(
'There is no error, the file uploaded with success.',
$this->uploadedFile->getErrorMessage()
);
self::assertSame(
'Failed to write file to disk.',
$this->uploadedFile2->getErrorMessage()
);
}
public function testSetErrorMessage() : void
{
$this->uploadedFile->setErrorMessage(\UPLOAD_ERR_OK);
self::assertStringStartsWith(
'There is no error',
$this->uploadedFile->getErrorMessage()
);
$this->uploadedFile->setErrorMessage(\UPLOAD_ERR_INI_SIZE);
self::assertStringStartsWith(
'The uploaded file exceeds the upload_max_filesize',
$this->uploadedFile->getErrorMessage()
);
$this->uploadedFile->setErrorMessage(\UPLOAD_ERR_FORM_SIZE);
self::assertStringStartsWith(
'The uploaded file exceeds the MAX_FILE_SIZE',
$this->uploadedFile->getErrorMessage()
);
$this->uploadedFile->setErrorMessage(\UPLOAD_ERR_PARTIAL);
self::assertStringStartsWith(
'The uploaded file was only partially',
$this->uploadedFile->getErrorMessage()
);
$this->uploadedFile->setErrorMessage(\UPLOAD_ERR_NO_FILE);
self::assertStringStartsWith(
'No file was uploaded',
$this->uploadedFile->getErrorMessage()
);
$this->uploadedFile->setErrorMessage(\UPLOAD_ERR_NO_TMP_DIR);
self::assertStringStartsWith(
'Missing a temporary',
$this->uploadedFile->getErrorMessage()
);
$this->uploadedFile->setErrorMessage(\UPLOAD_ERR_CANT_WRITE);
self::assertStringStartsWith(
'Failed to write file',
$this->uploadedFile->getErrorMessage()
);
$this->uploadedFile->setErrorMessage(\UPLOAD_ERR_EXTENSION);
self::assertStringStartsWith(
'A PHP extension stopped',
$this->uploadedFile->getErrorMessage()
);
$this->uploadedFile->setErrorMessage(42);
self::assertStringStartsWith(
'Unknown error',
$this->uploadedFile->getErrorMessage()
);
}
public function testGetName() : void
{
self::assertSame('logo.jpg', $this->uploadedFile->getName());
self::assertSame('file.txt', $this->uploadedFile2->getName());
}
public function testGetFullPath() : void
{
self::assertSame('logo.jpg', $this->uploadedFile->getFullPath());
self::assertSame('foo/bar/file.txt', $this->uploadedFile2->getFullPath());
}
public function testGetSize() : void
{
self::assertSame(19878, $this->uploadedFile->getSize());
self::assertSame(3, $this->uploadedFile2->getSize());
}
public function testGetTmpName() : void
{
self::assertSame(__DIR__ . '/files/logo.png', $this->uploadedFile->getTmpName());
self::assertSame(__DIR__ . '/files/file.txt', $this->uploadedFile2->getTmpName());
}
public function testIsMoved() : void
{
self::assertFalse($this->uploadedFile->isMoved());
self::assertFalse($this->uploadedFile2->isMoved());
}
public function testIsValid() : void
{
self::assertFalse($this->uploadedFile->isValid());
self::assertFalse($this->uploadedFile2->isValid());
}
public function testMove() : void
{
$files = [
\sys_get_temp_dir() . '/test-1',
\sys_get_temp_dir() . '/test-2',
\sys_get_temp_dir() . '/test-3',
];
foreach ($files as $file) {
if (\is_file($file)) {
\unlink($file);
}
}
self::assertFalse($this->uploadedFile->move($files[0]));
\touch($files[0]);
$this->uploadedFile->isMoved = true;
$this->uploadedFile->destination = $files[0];
self::assertTrue($this->uploadedFile->move($files[1]));
self::assertSame($files[1], $this->uploadedFile->getDestination());
\touch($files[2]);
self::assertFalse($this->uploadedFile->move($files[2]));
self::assertSame($files[1], $this->uploadedFile->getDestination());
self::assertTrue($this->uploadedFile->move($files[2], true));
self::assertSame($files[2], $this->uploadedFile->getDestination());
}
}