Move UploadedFileState test components into test file

This commit is contained in:
PJ Dietz 2020-08-13 06:37:51 -04:00
parent 83c2290a2f
commit 899ebb2492
3 changed files with 48 additions and 51 deletions

View File

@ -28,7 +28,7 @@ class UploadedFile implements UploadedFileInterface
private $tmpName; private $tmpName;
/** /**
* Create a new Uri. The arguments correspond with keys from arrays * Create a new UploadedFile. The arguments correspond with keys from arrays
* provided by $_FILES. For example, given this structure for $_FILES: * provided by $_FILES. For example, given this structure for $_FILES:
* *
* array( * array(
@ -66,7 +66,7 @@ class UploadedFile implements UploadedFileInterface
$this->size = $size; $this->size = $size;
if (file_exists($tmpName)) { if (file_exists($tmpName)) {
$this->stream = new Stream(fopen($tmpName, 'r')); $this->stream = new Stream(fopen($tmpName, 'rb'));
$this->tmpName = $tmpName; $this->tmpName = $tmpName;
} else { } else {
$this->stream = new NullStream(); $this->stream = new NullStream();
@ -126,7 +126,7 @@ class UploadedFile implements UploadedFileInterface
public function moveTo($path) public function moveTo($path)
{ {
if ($this->tmpName === null || !file_exists($this->tmpName)) { if ($this->tmpName === null || !file_exists($this->tmpName)) {
throw new RuntimeException('File ' . $this->tmpName . ' does not exist.'); throw new RuntimeException("File {$this->tmpName} does not exist.");
} }
if (php_sapi_name() === 'cli') { if (php_sapi_name() === 'cli') {
rename($this->tmpName, $path); rename($this->tmpName, $path);

View File

@ -1,24 +0,0 @@
<?php
namespace WellRESTed\Message;
class UploadedFileState
{
public static $php_sapi_name;
public static $is_uploaded_file;
}
function php_sapi_name()
{
return UploadedFileState::$php_sapi_name;
}
function move_uploaded_file($source, $target)
{
return rename($source, $target);
}
function is_uploaded_file($file)
{
return UploadedFileState::$is_uploaded_file;
}

View File

@ -1,16 +1,11 @@
<?php <?php
namespace WellRESTed\Test\Unit\Message; namespace WellRESTed\Message;
use Psr\Http\Message\StreamInterface; use Psr\Http\Message\StreamInterface;
use RuntimeException; use RuntimeException;
use WellRESTed\Message\UploadedFile;
use WellRESTed\Message\UploadedFileState;
use WellRESTed\Test\TestCase; use WellRESTed\Test\TestCase;
// Hides several php core functions for testing.
require_once __DIR__ . '/../../../src/UploadedFileState.php';
class UploadedFileTest extends TestCase class UploadedFileTest extends TestCase
{ {
private $tmpName; private $tmpName;
@ -35,16 +30,16 @@ class UploadedFileTest extends TestCase
} }
} }
// ------------------------------------------------------------------------ // -------------------------------------------------------------------------
// getStream // getStream
public function testGetStreamReturnsStreamInterface() public function testGetStreamReturnsStreamInterface(): void
{ {
$file = new UploadedFile('', '', 0, $this->tmpName, 0); $file = new UploadedFile('', '', 0, $this->tmpName, 0);
$this->assertInstanceOf(StreamInterface::class, $file->getStream()); $this->assertInstanceOf(StreamInterface::class, $file->getStream());
} }
public function testGetStreamReturnsStreamWrappingUploadedFile() public function testGetStreamReturnsStreamWrappingUploadedFile(): void
{ {
$content = 'Hello, World!'; $content = 'Hello, World!';
file_put_contents($this->tmpName, $content); file_put_contents($this->tmpName, $content);
@ -53,14 +48,14 @@ class UploadedFileTest extends TestCase
$this->assertEquals($content, (string) $stream); $this->assertEquals($content, (string) $stream);
} }
public function testGetStreamThrowsRuntimeExceptionForNoFile() public function testGetStreamThrowsRuntimeExceptionForNoFile(): void
{ {
$file = new UploadedFile('', '', 0, '', 0); $file = new UploadedFile('', '', 0, '', 0);
$this->expectException(RuntimeException::class); $this->expectException(RuntimeException::class);
$file->getStream(); $file->getStream();
} }
public function testGetStreamThrowsExceptionAfterMoveTo() public function testGetStreamThrowsExceptionAfterMoveTo(): void
{ {
$this->expectException(RuntimeException::class); $this->expectException(RuntimeException::class);
$content = 'Hello, World!'; $content = 'Hello, World!';
@ -70,7 +65,7 @@ class UploadedFileTest extends TestCase
$file->getStream(); $file->getStream();
} }
public function testGetStreamThrowsExceptionForNonUploadedFile() public function testGetStreamThrowsExceptionForNonUploadedFile(): void
{ {
$this->expectException(RuntimeException::class); $this->expectException(RuntimeException::class);
UploadedFileState::$php_sapi_name = 'apache'; UploadedFileState::$php_sapi_name = 'apache';
@ -79,10 +74,10 @@ class UploadedFileTest extends TestCase
$file->getStream(); $file->getStream();
} }
// ------------------------------------------------------------------------ // -------------------------------------------------------------------------
// moveTo // moveTo
public function testMoveToSapiRelocatesUploadedFileToDestinationIfExists() public function testMoveToSapiRelocatesUploadedFileToDestinationIfExists(): void
{ {
UploadedFileState::$php_sapi_name = 'fpm-fcgi'; UploadedFileState::$php_sapi_name = 'fpm-fcgi';
@ -96,7 +91,7 @@ class UploadedFileTest extends TestCase
$this->assertEquals($originalMd5, md5_file($this->movePath)); $this->assertEquals($originalMd5, md5_file($this->movePath));
} }
public function testMoveToNonSapiRelocatesUploadedFileToDestinationIfExists() public function testMoveToNonSapiRelocatesUploadedFileToDestinationIfExists(): void
{ {
$content = 'Hello, World!'; $content = 'Hello, World!';
file_put_contents($this->tmpName, $content); file_put_contents($this->tmpName, $content);
@ -108,7 +103,7 @@ class UploadedFileTest extends TestCase
$this->assertEquals($originalMd5, md5_file($this->movePath)); $this->assertEquals($originalMd5, md5_file($this->movePath));
} }
public function testMoveToThrowsExceptionOnSubsequentCall() public function testMoveToThrowsExceptionOnSubsequentCall(): void
{ {
$this->expectException(RuntimeException::class); $this->expectException(RuntimeException::class);
@ -120,39 +115,65 @@ class UploadedFileTest extends TestCase
$file->moveTo($this->movePath); $file->moveTo($this->movePath);
} }
// ------------------------------------------------------------------------ // -------------------------------------------------------------------------
// getSize // getSize
public function testGetSizeReturnsSize() public function testGetSizeReturnsSize(): void
{ {
$file = new UploadedFile('', '', 1024, '', 0); $file = new UploadedFile('', '', 1024, '', 0);
$this->assertEquals(1024, $file->getSize()); $this->assertEquals(1024, $file->getSize());
} }
// ------------------------------------------------------------------------ // -------------------------------------------------------------------------
// getError // getError
public function testGetErrorReturnsError() public function testGetErrorReturnsError(): void
{ {
$file = new UploadedFile('', '', 1024, '', UPLOAD_ERR_INI_SIZE); $file = new UploadedFile('', '', 1024, '', UPLOAD_ERR_INI_SIZE);
$this->assertEquals(UPLOAD_ERR_INI_SIZE, $file->getError()); $this->assertEquals(UPLOAD_ERR_INI_SIZE, $file->getError());
} }
// ------------------------------------------------------------------------ // -------------------------------------------------------------------------
// clientFilename // clientFilename
public function testGetClientFilenameReturnsClientFilename() public function testGetClientFilenameReturnsClientFilename(): void
{ {
$file = new UploadedFile('clientFilename', '', 0, '', 0); $file = new UploadedFile('clientFilename', '', 0, '', 0);
$this->assertEquals('clientFilename', $file->getClientFilename()); $this->assertEquals('clientFilename', $file->getClientFilename());
} }
// ------------------------------------------------------------------------ // -------------------------------------------------------------------------
// clientMediaType // clientMediaType
public function testGetClientMediaTypeReturnsClientMediaType() public function testGetClientMediaTypeReturnsClientMediaType(): void
{ {
$file = new UploadedFile('', 'clientMediaType', 0, '', 0); $file = new UploadedFile('', 'clientMediaType', 0, '', 0);
$this->assertEquals('clientMediaType', $file->getClientMediaType()); $this->assertEquals('clientMediaType', $file->getClientMediaType());
} }
} }
// -----------------------------------------------------------------------------
// Declare functions in this namespace so the class under test will use these
// instead of the internal global functions during testing.
class UploadedFileState
{
public static $php_sapi_name;
public static $is_uploaded_file;
}
function php_sapi_name()
{
return UploadedFileState::$php_sapi_name;
}
function move_uploaded_file($source, $target)
{
return rename($source, $target);
}
function is_uploaded_file($file)
{
return UploadedFileState::$is_uploaded_file;
}