Skip to content

Commit

Permalink
Cleaned up UploadedFileTest by providing return types and parameter…
Browse files Browse the repository at this point in the history
… types everywhere
  • Loading branch information
Ocramius committed Jul 6, 2022
1 parent 197f195 commit 091487a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 89 deletions.
46 changes: 1 addition & 45 deletions psalm-baseline.xml
Expand Up @@ -1164,54 +1164,10 @@
</MissingReturnType>
</file>
<file src="test/UploadedFileTest.php">
<MissingParamType occurrences="6">
<code>$path</code>
<code>$status</code>
<code>$status</code>
<code>$status</code>
<code>$status</code>
<code>$streamOrFile</code>
</MissingParamType>
<MissingPropertyType occurrences="1">
<code>$tmpFile</code>
</MissingPropertyType>
<MissingReturnType occurrences="24">
<code>errorConstantsAndMessages</code>
<code>invalidErrorStatuses</code>
<code>invalidMovePaths</code>
<code>invalidStreams</code>
<code>nonOkErrorStatus</code>
<code>testCannotRetrieveStreamAfterMove</code>
<code>testConstructorDoesNotRaiseExceptionForInvalidStreamWhenErrorStatusPresent</code>
<code>testGetStreamRaisesExceptionWhenErrorStatusPresent</code>
<code>testGetStreamRaisesExceptionWithAppropriateMessageWhenUploadErrorDetected</code>
<code>testGetStreamReturnsOriginalStreamObject</code>
<code>testGetStreamReturnsStreamForFile</code>
<code>testGetStreamReturnsWrappedPhpStream</code>
<code>testMoveCannotBeCalledMoreThanOnce</code>
<code>testMoveRaisesExceptionForInvalidPath</code>
<code>testMoveToCreatesStreamIfOnlyAFilenameWasProvided</code>
<code>testMoveToRaisesExceptionWhenErrorStatusPresent</code>
<code>testMoveToRaisesExceptionWithAppropriateMessageWhenUploadErrorDetected</code>
<code>testMovesFileToDesignatedPath</code>
<code>testRaisesExceptionOnInvalidErrorStatus</code>
<code>testRaisesExceptionOnInvalidStreamOrFile</code>
<code>testValidClientFilename</code>
<code>testValidClientMediaType</code>
<code>testValidNullClientFilename</code>
<code>testValidSize</code>
</MissingReturnType>
<MixedArgument occurrences="6">
<MixedArgument occurrences="2">
<code>$path</code>
<code>$status</code>
<code>$status</code>
<code>$status</code>
<code>$status</code>
<code>$streamOrFile</code>
</MixedArgument>
<UndefinedThisPropertyAssignment occurrences="1">
<code>$this-&gt;tmpfile</code>
</UndefinedThisPropertyAssignment>
</file>
<file src="test/UriTest.php">
<InvalidScalarArgument occurrences="2">
Expand Down
88 changes: 44 additions & 44 deletions test/UploadedFileTest.php
Expand Up @@ -30,12 +30,13 @@
use const UPLOAD_ERR_OK;
use const UPLOAD_ERR_PARTIAL;

class UploadedFileTest extends TestCase
final class UploadedFileTest extends TestCase
{
/** @var false|null|string */
protected $orgFile;
private $orgFile;

protected $tmpFile;
/** @var mixed */
private $tmpFile;

protected function setUp() : void
{
Expand All @@ -54,7 +55,8 @@ protected function tearDown() : void
}
}

public function invalidStreams()
/** @return non-empty-array<non-empty-string, array{mixed}> */
public function invalidStreams(): array
{
return [
'null' => [null],
Expand All @@ -73,22 +75,24 @@ public function invalidStreams()

/**
* @dataProvider invalidStreams
* @param mixed $streamOrFile
*/
public function testRaisesExceptionOnInvalidStreamOrFile($streamOrFile)
public function testRaisesExceptionOnInvalidStreamOrFile($streamOrFile): void
{
$this->expectException(InvalidArgumentException::class);

new UploadedFile($streamOrFile, 0, UPLOAD_ERR_OK);
}

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

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

public function invalidErrorStatuses()
/** @return non-empty-array<non-empty-string, array{int}> */
public function invalidErrorStatuses(): array
{
return [
'negative' => [-1],
Expand All @@ -99,48 +103,48 @@ public function invalidErrorStatuses()
/**
* @dataProvider invalidErrorStatuses
*/
public function testRaisesExceptionOnInvalidErrorStatus($status)
public function testRaisesExceptionOnInvalidErrorStatus(int $status): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('status');

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

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

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

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

public function testGetStreamReturnsOriginalStreamObject()
public function testGetStreamReturnsOriginalStreamObject(): void
{
$stream = new Stream('php://temp');
$upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
$this->assertSame($stream, $upload->getStream());
}

public function testGetStreamReturnsWrappedPhpStream()
public function testGetStreamReturnsWrappedPhpStream(): void
{
$stream = fopen('php://temp', 'wb+');
$upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
$uploadStream = $upload->getStream()->detach();
$this->assertSame($stream, $uploadStream);
}

public function testGetStreamReturnsStreamForFile()
public function testGetStreamReturnsStreamForFile(): void
{
$this->tmpFile = $stream = tempnam(sys_get_temp_dir(), 'diac');
$upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
Expand All @@ -150,10 +154,7 @@ public function testGetStreamReturnsStreamForFile()
$this->assertSame($stream, $r->getValue($uploadStream));
}

/**
* @return void
*/
public function testMovesFileToDesignatedPath()
public function testMovesFileToDesignatedPath(): void
{
$originalContents = 'Foo bar!';
$stream = new Stream('php://temp', 'wb+');
Expand All @@ -167,7 +168,8 @@ public function testMovesFileToDesignatedPath()
$this->assertSame($originalContents, $contents);
}

public function invalidMovePaths()
/** @return non-empty-array<non-empty-string, array{mixed}> */
public function invalidMovePaths(): array
{
return [
'null' => [null],
Expand All @@ -183,8 +185,10 @@ public function invalidMovePaths()

/**
* @dataProvider invalidMovePaths
*
* @param mixed $path
*/
public function testMoveRaisesExceptionForInvalidPath($path)
public function testMoveRaisesExceptionForInvalidPath($path): void
{
$stream = new Stream('php://temp', 'wb+');
$stream->write('Foo bar!');
Expand All @@ -198,7 +202,7 @@ public function testMoveRaisesExceptionForInvalidPath($path)
$upload->moveTo($path);
}

public function testMoveCannotBeCalledMoreThanOnce()
public function testMoveCannotBeCalledMoreThanOnce(): void
{
$stream = new Stream('php://temp', 'wb+');
$stream->write('Foo bar!');
Expand All @@ -214,7 +218,7 @@ public function testMoveCannotBeCalledMoreThanOnce()
$upload->moveTo($to);
}

public function testCannotRetrieveStreamAfterMove()
public function testCannotRetrieveStreamAfterMove(): void
{
$stream = new Stream('php://temp', 'wb+');
$stream->write('Foo bar!');
Expand All @@ -230,7 +234,8 @@ public function testCannotRetrieveStreamAfterMove()
$upload->getStream();
}

public function nonOkErrorStatus()
/** @return non-empty-array<non-empty-string, array{positive-int}> */
public function nonOkErrorStatus(): array
{
return [
'UPLOAD_ERR_INI_SIZE' => [ UPLOAD_ERR_INI_SIZE ],
Expand All @@ -247,7 +252,7 @@ public function nonOkErrorStatus()
* @dataProvider nonOkErrorStatus
* @group 60
*/
public function testConstructorDoesNotRaiseExceptionForInvalidStreamWhenErrorStatusPresent($status)
public function testConstructorDoesNotRaiseExceptionForInvalidStreamWhenErrorStatusPresent(int $status): void
{
$uploadedFile = new UploadedFile('not ok', 0, $status);
$this->assertSame($status, $uploadedFile->getError());
Expand All @@ -257,7 +262,7 @@ public function testConstructorDoesNotRaiseExceptionForInvalidStreamWhenErrorSta
* @dataProvider nonOkErrorStatus
* @group 60
*/
public function testMoveToRaisesExceptionWhenErrorStatusPresent($status)
public function testMoveToRaisesExceptionWhenErrorStatusPresent(int $status): void
{
$uploadedFile = new UploadedFile('not ok', 0, $status);

Expand All @@ -271,7 +276,7 @@ public function testMoveToRaisesExceptionWhenErrorStatusPresent($status)
* @dataProvider nonOkErrorStatus
* @group 60
*/
public function testGetStreamRaisesExceptionWhenErrorStatusPresent($status)
public function testGetStreamRaisesExceptionWhenErrorStatusPresent(int $status): void
{
$uploadedFile = new UploadedFile('not ok', 0, $status);

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

/**
* @group 82
* @return void
*/
public function testMoveToCreatesStreamIfOnlyAFilenameWasProvided()
public function testMoveToCreatesStreamIfOnlyAFilenameWasProvided(): void
{
$this->orgFile = tempnam(sys_get_temp_dir(), 'ORG');
$this->tmpFile = tempnam(sys_get_temp_dir(), 'DIA');
Expand All @@ -301,7 +305,8 @@ public function testMoveToCreatesStreamIfOnlyAFilenameWasProvided()
$this->assertSame($original, $contents);
}

public function errorConstantsAndMessages()
/** @return iterable<int, array{int, non-empty-string}> */
public function errorConstantsAndMessages(): iterable
{
foreach (UploadedFile::ERROR_MESSAGES as $constant => $message) {
if ($constant === UPLOAD_ERR_OK) {
Expand All @@ -311,13 +316,11 @@ public function errorConstantsAndMessages()
}
}

/**
* @dataProvider errorConstantsAndMessages
* @param int $constant Upload error constant
* @param string $message Associated error message
*/
public function testGetStreamRaisesExceptionWithAppropriateMessageWhenUploadErrorDetected($constant, $message)
{
/** @dataProvider errorConstantsAndMessages */
public function testGetStreamRaisesExceptionWithAppropriateMessageWhenUploadErrorDetected(
int $constant,
string $message
): void {
$uploadedFile = new UploadedFile(__FILE__, 100, $constant);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage($message);
Expand All @@ -326,21 +329,18 @@ public function testGetStreamRaisesExceptionWithAppropriateMessageWhenUploadErro

/**
* @dataProvider errorConstantsAndMessages
* @param int $constant Upload error constant
* @param string $message Associated error message
*/
public function testMoveToRaisesExceptionWithAppropriateMessageWhenUploadErrorDetected($constant, $message)
{
public function testMoveToRaisesExceptionWithAppropriateMessageWhenUploadErrorDetected(
int $constant,
string $message
): void {
$uploadedFile = new UploadedFile(__FILE__, 100, $constant);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage($message);
$uploadedFile->moveTo('/tmp/foo');
}

/**
* @return void
*/
public function testMoveToInCLIShouldRemoveOriginalFile()
public function testMoveToInCLIShouldRemoveOriginalFile(): void
{
$this->orgFile = tempnam(sys_get_temp_dir(), 'ORG');
file_put_contents($this->orgFile, 'Hello');
Expand Down

0 comments on commit 091487a

Please sign in to comment.