From 899ebb24924daa2a4e00000bf00a0c4d7211c257 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Thu, 13 Aug 2020 06:37:51 -0400 Subject: [PATCH] Move UploadedFileState test components into test file --- src/Message/UploadedFile.php | 6 +- test/src/UploadedFileState.php | 24 ------- test/tests/unit/Message/UploadedFileTest.php | 69 +++++++++++++------- 3 files changed, 48 insertions(+), 51 deletions(-) delete mode 100644 test/src/UploadedFileState.php diff --git a/src/Message/UploadedFile.php b/src/Message/UploadedFile.php index c18c4b8..cb08460 100644 --- a/src/Message/UploadedFile.php +++ b/src/Message/UploadedFile.php @@ -28,7 +28,7 @@ class UploadedFile implements UploadedFileInterface 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: * * array( @@ -66,7 +66,7 @@ class UploadedFile implements UploadedFileInterface $this->size = $size; if (file_exists($tmpName)) { - $this->stream = new Stream(fopen($tmpName, 'r')); + $this->stream = new Stream(fopen($tmpName, 'rb')); $this->tmpName = $tmpName; } else { $this->stream = new NullStream(); @@ -126,7 +126,7 @@ class UploadedFile implements UploadedFileInterface public function moveTo($path) { 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') { rename($this->tmpName, $path); diff --git a/test/src/UploadedFileState.php b/test/src/UploadedFileState.php deleted file mode 100644 index d0fdf1d..0000000 --- a/test/src/UploadedFileState.php +++ /dev/null @@ -1,24 +0,0 @@ -tmpName, 0); $this->assertInstanceOf(StreamInterface::class, $file->getStream()); } - public function testGetStreamReturnsStreamWrappingUploadedFile() + public function testGetStreamReturnsStreamWrappingUploadedFile(): void { $content = 'Hello, World!'; file_put_contents($this->tmpName, $content); @@ -53,14 +48,14 @@ class UploadedFileTest extends TestCase $this->assertEquals($content, (string) $stream); } - public function testGetStreamThrowsRuntimeExceptionForNoFile() + public function testGetStreamThrowsRuntimeExceptionForNoFile(): void { $file = new UploadedFile('', '', 0, '', 0); $this->expectException(RuntimeException::class); $file->getStream(); } - public function testGetStreamThrowsExceptionAfterMoveTo() + public function testGetStreamThrowsExceptionAfterMoveTo(): void { $this->expectException(RuntimeException::class); $content = 'Hello, World!'; @@ -70,7 +65,7 @@ class UploadedFileTest extends TestCase $file->getStream(); } - public function testGetStreamThrowsExceptionForNonUploadedFile() + public function testGetStreamThrowsExceptionForNonUploadedFile(): void { $this->expectException(RuntimeException::class); UploadedFileState::$php_sapi_name = 'apache'; @@ -79,10 +74,10 @@ class UploadedFileTest extends TestCase $file->getStream(); } - // ------------------------------------------------------------------------ + // ------------------------------------------------------------------------- // moveTo - public function testMoveToSapiRelocatesUploadedFileToDestinationIfExists() + public function testMoveToSapiRelocatesUploadedFileToDestinationIfExists(): void { UploadedFileState::$php_sapi_name = 'fpm-fcgi'; @@ -96,7 +91,7 @@ class UploadedFileTest extends TestCase $this->assertEquals($originalMd5, md5_file($this->movePath)); } - public function testMoveToNonSapiRelocatesUploadedFileToDestinationIfExists() + public function testMoveToNonSapiRelocatesUploadedFileToDestinationIfExists(): void { $content = 'Hello, World!'; file_put_contents($this->tmpName, $content); @@ -108,7 +103,7 @@ class UploadedFileTest extends TestCase $this->assertEquals($originalMd5, md5_file($this->movePath)); } - public function testMoveToThrowsExceptionOnSubsequentCall() + public function testMoveToThrowsExceptionOnSubsequentCall(): void { $this->expectException(RuntimeException::class); @@ -120,39 +115,65 @@ class UploadedFileTest extends TestCase $file->moveTo($this->movePath); } - // ------------------------------------------------------------------------ + // ------------------------------------------------------------------------- // getSize - public function testGetSizeReturnsSize() + public function testGetSizeReturnsSize(): void { $file = new UploadedFile('', '', 1024, '', 0); $this->assertEquals(1024, $file->getSize()); } - // ------------------------------------------------------------------------ + // ------------------------------------------------------------------------- // getError - public function testGetErrorReturnsError() + public function testGetErrorReturnsError(): void { $file = new UploadedFile('', '', 1024, '', UPLOAD_ERR_INI_SIZE); $this->assertEquals(UPLOAD_ERR_INI_SIZE, $file->getError()); } - // ------------------------------------------------------------------------ + // ------------------------------------------------------------------------- // clientFilename - public function testGetClientFilenameReturnsClientFilename() + public function testGetClientFilenameReturnsClientFilename(): void { $file = new UploadedFile('clientFilename', '', 0, '', 0); $this->assertEquals('clientFilename', $file->getClientFilename()); } - // ------------------------------------------------------------------------ + // ------------------------------------------------------------------------- // clientMediaType - public function testGetClientMediaTypeReturnsClientMediaType() + public function testGetClientMediaTypeReturnsClientMediaType(): void { $file = new UploadedFile('', 'clientMediaType', 0, '', 0); $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; +}