1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | namespace Sunrise\Http\ServerRequest\Tests; |
||||
6 | |||||
7 | /** |
||||
8 | * Import classes |
||||
9 | */ |
||||
10 | use Psr\Http\Message\StreamInterface; |
||||
11 | use Psr\Http\Message\UploadedFileInterface; |
||||
12 | use Sunrise\Http\ServerRequest\UploadedFile; |
||||
13 | use Sunrise\Stream\StreamFactory; |
||||
14 | |||||
15 | /** |
||||
16 | * Import constants |
||||
17 | */ |
||||
18 | use const Sunrise\Http\ServerRequest\UPLOAD_ERRORS; |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
19 | |||||
20 | /** |
||||
21 | * UploadedFileTest |
||||
22 | */ |
||||
23 | class UploadedFileTest extends AbstractTestCase |
||||
24 | { |
||||
25 | |||||
26 | /** |
||||
27 | * @return void |
||||
28 | */ |
||||
29 | public function testConstructor() : void |
||||
30 | { |
||||
31 | $stream = $this->createStream(); |
||||
32 | $uploadedFile = new UploadedFile($stream); |
||||
33 | |||||
34 | $this->assertInstanceOf(UploadedFileInterface::class, $uploadedFile); |
||||
35 | $this->assertSame($stream, $uploadedFile->getStream()); |
||||
36 | $this->assertNull($uploadedFile->getSize()); |
||||
37 | $this->assertSame(\UPLOAD_ERR_OK, $uploadedFile->getError()); |
||||
38 | $this->assertNull($uploadedFile->getClientFilename()); |
||||
39 | $this->assertNull($uploadedFile->getClientMediaType()); |
||||
40 | } |
||||
41 | |||||
42 | /** |
||||
43 | * @return void |
||||
44 | */ |
||||
45 | public function testConstructorWithOptionalParameters() : void |
||||
46 | { |
||||
47 | $stream = $this->createStream(); |
||||
48 | $uploadedFile = new UploadedFile($stream, 42, \UPLOAD_ERR_OK, 'foo', 'bar'); |
||||
49 | |||||
50 | $this->assertInstanceOf(UploadedFileInterface::class, $uploadedFile); |
||||
51 | $this->assertSame($stream, $uploadedFile->getStream()); |
||||
52 | $this->assertSame(42, $uploadedFile->getSize()); |
||||
53 | $this->assertSame(\UPLOAD_ERR_OK, $uploadedFile->getError()); |
||||
54 | $this->assertSame('foo', $uploadedFile->getClientFilename()); |
||||
55 | $this->assertSame('bar', $uploadedFile->getClientMediaType()); |
||||
56 | } |
||||
57 | |||||
58 | /** |
||||
59 | * @return void |
||||
60 | */ |
||||
61 | public function testConstructorWithInvalidFile() : void |
||||
62 | { |
||||
63 | $this->expectException(\InvalidArgumentException::class); |
||||
64 | $this->expectExceptionMessage('Invalid uploaded file'); |
||||
65 | |||||
66 | new UploadedFile(null); |
||||
67 | } |
||||
68 | |||||
69 | /** |
||||
70 | * @return void |
||||
71 | */ |
||||
72 | public function testMove() : void |
||||
73 | { |
||||
74 | $stream = $this->createStream('foo'); |
||||
75 | $uploadedFile = new UploadedFile($stream); |
||||
76 | $targetPath = $this->createStream()->getMetadata('uri'); |
||||
77 | $uploadedFile->moveTo($targetPath); |
||||
78 | |||||
79 | $this->assertStringEqualsFile($targetPath, 'foo'); |
||||
0 ignored issues
–
show
It seems like
$targetPath can also be of type null ; however, parameter $expectedFile of PHPUnit\Framework\Assert::assertStringEqualsFile() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
80 | } |
||||
81 | |||||
82 | /** |
||||
83 | * @return void |
||||
84 | */ |
||||
85 | public function testReWrite() : void |
||||
86 | { |
||||
87 | $stream = $this->createStream('foo'); |
||||
88 | $uploadedFile = new UploadedFile($stream); |
||||
89 | $targetPath = $this->createStream('bar')->getMetadata('uri'); |
||||
90 | $uploadedFile->moveTo($targetPath); |
||||
91 | |||||
92 | $this->assertStringEqualsFile($targetPath, 'foo'); |
||||
0 ignored issues
–
show
It seems like
$targetPath can also be of type null ; however, parameter $expectedFile of PHPUnit\Framework\Assert::assertStringEqualsFile() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
93 | } |
||||
94 | |||||
95 | /** |
||||
96 | * @dataProvider errorCodeProvider |
||||
97 | * |
||||
98 | * @return void |
||||
99 | */ |
||||
100 | public function testMoveWithError($error) : void |
||||
101 | { |
||||
102 | $stream = $this->createStream(); |
||||
103 | $uploadedFile = new UploadedFile($stream, null, $error); |
||||
104 | |||||
105 | $this->expectException(\RuntimeException::class); |
||||
106 | $this->expectExceptionMessage(\sprintf( |
||||
107 | 'The uploaded file cannot be moved due to the error #%d (%s)', |
||||
108 | $error, |
||||
109 | UPLOAD_ERRORS[$error] ?? 'Unknown error' |
||||
110 | )); |
||||
111 | |||||
112 | $uploadedFile->moveTo('/'); |
||||
113 | } |
||||
114 | |||||
115 | /** |
||||
116 | * @return void |
||||
117 | */ |
||||
118 | public function testReMove() : void |
||||
119 | { |
||||
120 | $stream = $this->createStream(); |
||||
121 | $uploadedFile = new UploadedFile($stream); |
||||
122 | $targetPath = $this->createStream()->getMetadata('uri'); |
||||
123 | $uploadedFile->moveTo($targetPath); |
||||
124 | |||||
125 | $this->expectException(\RuntimeException::class); |
||||
126 | $this->expectExceptionMessage('The uploaded file already moved'); |
||||
127 | |||||
128 | $uploadedFile->moveTo('/'); |
||||
129 | } |
||||
130 | |||||
131 | /** |
||||
132 | * @return void |
||||
133 | */ |
||||
134 | public function testMoveToNowhere() : void |
||||
135 | { |
||||
136 | $stream = $this->createStream(); |
||||
137 | $uploadedFile = new UploadedFile($stream); |
||||
138 | |||||
139 | $this->expectException(\InvalidArgumentException::class); |
||||
140 | $this->expectExceptionMessage('The uploaded file cannot be moved because the directory "/" is not available'); |
||||
141 | |||||
142 | $uploadedFile->moveTo('/'); |
||||
143 | } |
||||
144 | |||||
145 | /** |
||||
146 | * @return void |
||||
147 | */ |
||||
148 | public function testGetMovedStream() : void |
||||
149 | { |
||||
150 | $stream = $this->createStream(); |
||||
151 | $uploadedFile = new UploadedFile($stream); |
||||
152 | |||||
153 | $targetPath = $this->createStream()->getMetadata('uri'); |
||||
154 | $uploadedFile->moveTo($targetPath); |
||||
155 | |||||
156 | $this->expectException(\RuntimeException::class); |
||||
157 | $this->expectExceptionMessage('The uploaded file already moved'); |
||||
158 | |||||
159 | $uploadedFile->getStream(); |
||||
160 | } |
||||
161 | |||||
162 | /** |
||||
163 | * @dataProvider errorCodeProvider |
||||
164 | * |
||||
165 | * @return void |
||||
166 | */ |
||||
167 | public function testGetStreamWithError($error) : void |
||||
168 | { |
||||
169 | $stream = $this->createStream(); |
||||
170 | $uploadedFile = new UploadedFile($stream, null, $error); |
||||
171 | |||||
172 | $this->expectException(\RuntimeException::class); |
||||
173 | $this->expectExceptionMessage(\sprintf( |
||||
174 | 'The uploaded file has no a stream due to the error #%d (%s)', |
||||
175 | $error, |
||||
176 | UPLOAD_ERRORS[$error] ?? 'Unknown error' |
||||
177 | )); |
||||
178 | |||||
179 | $uploadedFile->getStream(); |
||||
180 | } |
||||
181 | |||||
182 | /** |
||||
183 | * @return array |
||||
184 | */ |
||||
185 | public function errorCodeProvider() : array |
||||
186 | { |
||||
187 | return [ |
||||
188 | [\UPLOAD_ERR_INI_SIZE], |
||||
189 | [\UPLOAD_ERR_FORM_SIZE], |
||||
190 | [\UPLOAD_ERR_PARTIAL], |
||||
191 | [\UPLOAD_ERR_NO_FILE], |
||||
192 | [\UPLOAD_ERR_NO_TMP_DIR], |
||||
193 | [\UPLOAD_ERR_CANT_WRITE], |
||||
194 | [\UPLOAD_ERR_EXTENSION], |
||||
195 | ]; |
||||
196 | } |
||||
197 | } |
||||
198 |