Skip to content

Commit 091487a

Browse files
committed
Cleaned up UploadedFileTest by providing return types and parameter types everywhere
1 parent 197f195 commit 091487a

2 files changed

Lines changed: 45 additions & 89 deletions

File tree

psalm-baseline.xml

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,54 +1164,10 @@
11641164
</MissingReturnType>
11651165
</file>
11661166
<file src="test/UploadedFileTest.php">
1167-
<MissingParamType occurrences="6">
1168-
<code>$path</code>
1169-
<code>$status</code>
1170-
<code>$status</code>
1171-
<code>$status</code>
1172-
<code>$status</code>
1173-
<code>$streamOrFile</code>
1174-
</MissingParamType>
1175-
<MissingPropertyType occurrences="1">
1176-
<code>$tmpFile</code>
1177-
</MissingPropertyType>
1178-
<MissingReturnType occurrences="24">
1179-
<code>errorConstantsAndMessages</code>
1180-
<code>invalidErrorStatuses</code>
1181-
<code>invalidMovePaths</code>
1182-
<code>invalidStreams</code>
1183-
<code>nonOkErrorStatus</code>
1184-
<code>testCannotRetrieveStreamAfterMove</code>
1185-
<code>testConstructorDoesNotRaiseExceptionForInvalidStreamWhenErrorStatusPresent</code>
1186-
<code>testGetStreamRaisesExceptionWhenErrorStatusPresent</code>
1187-
<code>testGetStreamRaisesExceptionWithAppropriateMessageWhenUploadErrorDetected</code>
1188-
<code>testGetStreamReturnsOriginalStreamObject</code>
1189-
<code>testGetStreamReturnsStreamForFile</code>
1190-
<code>testGetStreamReturnsWrappedPhpStream</code>
1191-
<code>testMoveCannotBeCalledMoreThanOnce</code>
1192-
<code>testMoveRaisesExceptionForInvalidPath</code>
1193-
<code>testMoveToCreatesStreamIfOnlyAFilenameWasProvided</code>
1194-
<code>testMoveToRaisesExceptionWhenErrorStatusPresent</code>
1195-
<code>testMoveToRaisesExceptionWithAppropriateMessageWhenUploadErrorDetected</code>
1196-
<code>testMovesFileToDesignatedPath</code>
1197-
<code>testRaisesExceptionOnInvalidErrorStatus</code>
1198-
<code>testRaisesExceptionOnInvalidStreamOrFile</code>
1199-
<code>testValidClientFilename</code>
1200-
<code>testValidClientMediaType</code>
1201-
<code>testValidNullClientFilename</code>
1202-
<code>testValidSize</code>
1203-
</MissingReturnType>
1204-
<MixedArgument occurrences="6">
1167+
<MixedArgument occurrences="2">
12051168
<code>$path</code>
1206-
<code>$status</code>
1207-
<code>$status</code>
1208-
<code>$status</code>
1209-
<code>$status</code>
12101169
<code>$streamOrFile</code>
12111170
</MixedArgument>
1212-
<UndefinedThisPropertyAssignment occurrences="1">
1213-
<code>$this-&gt;tmpfile</code>
1214-
</UndefinedThisPropertyAssignment>
12151171
</file>
12161172
<file src="test/UriTest.php">
12171173
<InvalidScalarArgument occurrences="2">

test/UploadedFileTest.php

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@
3030
use const UPLOAD_ERR_OK;
3131
use const UPLOAD_ERR_PARTIAL;
3232

33-
class UploadedFileTest extends TestCase
33+
final class UploadedFileTest extends TestCase
3434
{
3535
/** @var false|null|string */
36-
protected $orgFile;
36+
private $orgFile;
3737

38-
protected $tmpFile;
38+
/** @var mixed */
39+
private $tmpFile;
3940

4041
protected function setUp() : void
4142
{
@@ -54,7 +55,8 @@ protected function tearDown() : void
5455
}
5556
}
5657

57-
public function invalidStreams()
58+
/** @return non-empty-array<non-empty-string, array{mixed}> */
59+
public function invalidStreams(): array
5860
{
5961
return [
6062
'null' => [null],
@@ -73,22 +75,24 @@ public function invalidStreams()
7375

7476
/**
7577
* @dataProvider invalidStreams
78+
* @param mixed $streamOrFile
7679
*/
77-
public function testRaisesExceptionOnInvalidStreamOrFile($streamOrFile)
80+
public function testRaisesExceptionOnInvalidStreamOrFile($streamOrFile): void
7881
{
7982
$this->expectException(InvalidArgumentException::class);
8083

8184
new UploadedFile($streamOrFile, 0, UPLOAD_ERR_OK);
8285
}
8386

84-
public function testValidSize()
87+
public function testValidSize(): void
8588
{
8689
$uploaded = new UploadedFile(fopen('php://temp', 'wb+'), 123, UPLOAD_ERR_OK);
8790

8891
$this->assertSame(123, $uploaded->getSize());
8992
}
9093

91-
public function invalidErrorStatuses()
94+
/** @return non-empty-array<non-empty-string, array{int}> */
95+
public function invalidErrorStatuses(): array
9296
{
9397
return [
9498
'negative' => [-1],
@@ -99,48 +103,48 @@ public function invalidErrorStatuses()
99103
/**
100104
* @dataProvider invalidErrorStatuses
101105
*/
102-
public function testRaisesExceptionOnInvalidErrorStatus($status)
106+
public function testRaisesExceptionOnInvalidErrorStatus(int $status): void
103107
{
104108
$this->expectException(InvalidArgumentException::class);
105109
$this->expectExceptionMessage('status');
106110

107111
new UploadedFile(fopen('php://temp', 'wb+'), 0, $status);
108112
}
109113

110-
public function testValidClientFilename()
114+
public function testValidClientFilename(): void
111115
{
112116
$file = new UploadedFile(fopen('php://temp', 'wb+'), 0, UPLOAD_ERR_OK, 'boo.txt');
113117
$this->assertSame('boo.txt', $file->getClientFilename());
114118
}
115119

116-
public function testValidNullClientFilename()
120+
public function testValidNullClientFilename(): void
117121
{
118122
$file = new UploadedFile(fopen('php://temp', 'wb+'), 0, UPLOAD_ERR_OK, null);
119123
$this->assertSame(null, $file->getClientFilename());
120124
}
121125

122-
public function testValidClientMediaType()
126+
public function testValidClientMediaType(): void
123127
{
124128
$file = new UploadedFile(fopen('php://temp', 'wb+'), 0, UPLOAD_ERR_OK, 'foobar.baz', 'mediatype');
125129
$this->assertSame('mediatype', $file->getClientMediaType());
126130
}
127131

128-
public function testGetStreamReturnsOriginalStreamObject()
132+
public function testGetStreamReturnsOriginalStreamObject(): void
129133
{
130134
$stream = new Stream('php://temp');
131135
$upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
132136
$this->assertSame($stream, $upload->getStream());
133137
}
134138

135-
public function testGetStreamReturnsWrappedPhpStream()
139+
public function testGetStreamReturnsWrappedPhpStream(): void
136140
{
137141
$stream = fopen('php://temp', 'wb+');
138142
$upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
139143
$uploadStream = $upload->getStream()->detach();
140144
$this->assertSame($stream, $uploadStream);
141145
}
142146

143-
public function testGetStreamReturnsStreamForFile()
147+
public function testGetStreamReturnsStreamForFile(): void
144148
{
145149
$this->tmpFile = $stream = tempnam(sys_get_temp_dir(), 'diac');
146150
$upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
@@ -150,10 +154,7 @@ public function testGetStreamReturnsStreamForFile()
150154
$this->assertSame($stream, $r->getValue($uploadStream));
151155
}
152156

153-
/**
154-
* @return void
155-
*/
156-
public function testMovesFileToDesignatedPath()
157+
public function testMovesFileToDesignatedPath(): void
157158
{
158159
$originalContents = 'Foo bar!';
159160
$stream = new Stream('php://temp', 'wb+');
@@ -167,7 +168,8 @@ public function testMovesFileToDesignatedPath()
167168
$this->assertSame($originalContents, $contents);
168169
}
169170

170-
public function invalidMovePaths()
171+
/** @return non-empty-array<non-empty-string, array{mixed}> */
172+
public function invalidMovePaths(): array
171173
{
172174
return [
173175
'null' => [null],
@@ -183,8 +185,10 @@ public function invalidMovePaths()
183185

184186
/**
185187
* @dataProvider invalidMovePaths
188+
*
189+
* @param mixed $path
186190
*/
187-
public function testMoveRaisesExceptionForInvalidPath($path)
191+
public function testMoveRaisesExceptionForInvalidPath($path): void
188192
{
189193
$stream = new Stream('php://temp', 'wb+');
190194
$stream->write('Foo bar!');
@@ -198,7 +202,7 @@ public function testMoveRaisesExceptionForInvalidPath($path)
198202
$upload->moveTo($path);
199203
}
200204

201-
public function testMoveCannotBeCalledMoreThanOnce()
205+
public function testMoveCannotBeCalledMoreThanOnce(): void
202206
{
203207
$stream = new Stream('php://temp', 'wb+');
204208
$stream->write('Foo bar!');
@@ -214,7 +218,7 @@ public function testMoveCannotBeCalledMoreThanOnce()
214218
$upload->moveTo($to);
215219
}
216220

217-
public function testCannotRetrieveStreamAfterMove()
221+
public function testCannotRetrieveStreamAfterMove(): void
218222
{
219223
$stream = new Stream('php://temp', 'wb+');
220224
$stream->write('Foo bar!');
@@ -230,7 +234,8 @@ public function testCannotRetrieveStreamAfterMove()
230234
$upload->getStream();
231235
}
232236

233-
public function nonOkErrorStatus()
237+
/** @return non-empty-array<non-empty-string, array{positive-int}> */
238+
public function nonOkErrorStatus(): array
234239
{
235240
return [
236241
'UPLOAD_ERR_INI_SIZE' => [ UPLOAD_ERR_INI_SIZE ],
@@ -247,7 +252,7 @@ public function nonOkErrorStatus()
247252
* @dataProvider nonOkErrorStatus
248253
* @group 60
249254
*/
250-
public function testConstructorDoesNotRaiseExceptionForInvalidStreamWhenErrorStatusPresent($status)
255+
public function testConstructorDoesNotRaiseExceptionForInvalidStreamWhenErrorStatusPresent(int $status): void
251256
{
252257
$uploadedFile = new UploadedFile('not ok', 0, $status);
253258
$this->assertSame($status, $uploadedFile->getError());
@@ -257,7 +262,7 @@ public function testConstructorDoesNotRaiseExceptionForInvalidStreamWhenErrorSta
257262
* @dataProvider nonOkErrorStatus
258263
* @group 60
259264
*/
260-
public function testMoveToRaisesExceptionWhenErrorStatusPresent($status)
265+
public function testMoveToRaisesExceptionWhenErrorStatusPresent(int $status): void
261266
{
262267
$uploadedFile = new UploadedFile('not ok', 0, $status);
263268

@@ -271,7 +276,7 @@ public function testMoveToRaisesExceptionWhenErrorStatusPresent($status)
271276
* @dataProvider nonOkErrorStatus
272277
* @group 60
273278
*/
274-
public function testGetStreamRaisesExceptionWhenErrorStatusPresent($status)
279+
public function testGetStreamRaisesExceptionWhenErrorStatusPresent(int $status): void
275280
{
276281
$uploadedFile = new UploadedFile('not ok', 0, $status);
277282

@@ -283,9 +288,8 @@ public function testGetStreamRaisesExceptionWhenErrorStatusPresent($status)
283288

284289
/**
285290
* @group 82
286-
* @return void
287291
*/
288-
public function testMoveToCreatesStreamIfOnlyAFilenameWasProvided()
292+
public function testMoveToCreatesStreamIfOnlyAFilenameWasProvided(): void
289293
{
290294
$this->orgFile = tempnam(sys_get_temp_dir(), 'ORG');
291295
$this->tmpFile = tempnam(sys_get_temp_dir(), 'DIA');
@@ -301,7 +305,8 @@ public function testMoveToCreatesStreamIfOnlyAFilenameWasProvided()
301305
$this->assertSame($original, $contents);
302306
}
303307

304-
public function errorConstantsAndMessages()
308+
/** @return iterable<int, array{int, non-empty-string}> */
309+
public function errorConstantsAndMessages(): iterable
305310
{
306311
foreach (UploadedFile::ERROR_MESSAGES as $constant => $message) {
307312
if ($constant === UPLOAD_ERR_OK) {
@@ -311,13 +316,11 @@ public function errorConstantsAndMessages()
311316
}
312317
}
313318

314-
/**
315-
* @dataProvider errorConstantsAndMessages
316-
* @param int $constant Upload error constant
317-
* @param string $message Associated error message
318-
*/
319-
public function testGetStreamRaisesExceptionWithAppropriateMessageWhenUploadErrorDetected($constant, $message)
320-
{
319+
/** @dataProvider errorConstantsAndMessages */
320+
public function testGetStreamRaisesExceptionWithAppropriateMessageWhenUploadErrorDetected(
321+
int $constant,
322+
string $message
323+
): void {
321324
$uploadedFile = new UploadedFile(__FILE__, 100, $constant);
322325
$this->expectException(RuntimeException::class);
323326
$this->expectExceptionMessage($message);
@@ -326,21 +329,18 @@ public function testGetStreamRaisesExceptionWithAppropriateMessageWhenUploadErro
326329

327330
/**
328331
* @dataProvider errorConstantsAndMessages
329-
* @param int $constant Upload error constant
330-
* @param string $message Associated error message
331332
*/
332-
public function testMoveToRaisesExceptionWithAppropriateMessageWhenUploadErrorDetected($constant, $message)
333-
{
333+
public function testMoveToRaisesExceptionWithAppropriateMessageWhenUploadErrorDetected(
334+
int $constant,
335+
string $message
336+
): void {
334337
$uploadedFile = new UploadedFile(__FILE__, 100, $constant);
335338
$this->expectException(RuntimeException::class);
336339
$this->expectExceptionMessage($message);
337340
$uploadedFile->moveTo('/tmp/foo');
338341
}
339342

340-
/**
341-
* @return void
342-
*/
343-
public function testMoveToInCLIShouldRemoveOriginalFile()
343+
public function testMoveToInCLIShouldRemoveOriginalFile(): void
344344
{
345345
$this->orgFile = tempnam(sys_get_temp_dir(), 'ORG');
346346
file_put_contents($this->orgFile, 'Hello');

0 commit comments

Comments
 (0)