diff --git a/CHANGELOG.md b/CHANGELOG.md index 1300a8b..2c286b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All Notable changes to `laravel-request` will be documented in this file +## 1.1.0 - 2016-09-10 +### CHANGE +- Change namespace of UploadedFileTestable trait to easy use in other package unit test. + ## 1.0.0 - 2016-08-28 - Initial release diff --git a/src/UploadedFileTestable.php b/src/UploadedFileTestable.php new file mode 100644 index 0000000..be724f7 --- /dev/null +++ b/src/UploadedFileTestable.php @@ -0,0 +1,70 @@ +assertFileExists($fullPath); + + $uploadedFile = new UploadedFile( + $fullPath, + pathinfo($fullPath, PATHINFO_BASENAME), + ($mimeType === null || $mimeType == '') ? mime_content_type($fullPath) : $mimeType, + filesize($fullPath), + $errorCode, + true // true for test + ); + return $uploadedFile; + } + + /** + * Take $fullPath existing file, copy it to system temp dir with random name and + * Create an instance of Illuminate\Http\UploadedFile for testing (param test=true). + * Before creating UploadedFile class check if file exists with assertFileExists($fullPath). + * @param string $fullPath + * @param string $mimeType if empty try to resolve mimeType automatically. + * @param int $errorCode default 0 (no error). + * For all possible values see Symfony\Component\HttpFoundation\File\UploadedFile::getErrorMessage() + * @return UploadedFile + * @throws \Symfony\Component\HttpFoundation\File\Exception\FileException + * @throws \Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException + */ + public function getUploadedFileForTestAndCopyInSysTempDir( + string $fullPath, + string $mimeType = '', + int $errorCode = 0 + ) : UploadedFile + { + $this->assertFileExists($fullPath); + + $origname = pathinfo($fullPath, PATHINFO_BASENAME); + $path = sys_get_temp_dir() . '/' . $origname; + if (file_exists($path)) { + unlink($path); + } + copy($fullPath, $path); + + $uploadedFile = $this->getUploadedFileForTest($path, $mimeType, $errorCode); + return $uploadedFile; + } +} diff --git a/tests/RequestHelperTest.php b/tests/RequestHelperTest.php index df0e974..e1b2195 100644 --- a/tests/RequestHelperTest.php +++ b/tests/RequestHelperTest.php @@ -10,6 +10,7 @@ use Padosoft\Laravel\Request\RequestHelper; use Padosoft\Test\traits\ExceptionTestable; use Padosoft\Test\traits\FileSystemTestable; +use Padosoft\Laravel\Request\UploadedFileTestable; class RequestHelperTest extends Orchestra { diff --git a/tests/UploadedFileHelperTest.php b/tests/UploadedFileHelperTest.php index 5f8f611..2303858 100644 --- a/tests/UploadedFileHelperTest.php +++ b/tests/UploadedFileHelperTest.php @@ -5,6 +5,7 @@ use Padosoft\Laravel\Request\UploadedFileHelper; use Padosoft\Test\traits\ExceptionTestable; use Padosoft\Test\traits\FileSystemTestable; +use Padosoft\Laravel\Request\UploadedFileTestable; class UploadedFileHelperTest extends \PHPUnit_Framework_TestCase { diff --git a/tests/UploadedFileTestable.php b/tests/UploadedFileTestable.php deleted file mode 100644 index 358078c..0000000 --- a/tests/UploadedFileTestable.php +++ /dev/null @@ -1,32 +0,0 @@ -assertFileExists($fullPath); - - $uploadedFile = new UploadedFile( - $fullPath, - pathinfo($fullPath, PATHINFO_BASENAME), - ($mimeType===null || $mimeType=='') ? mime_content_type($fullPath) : $mimeType, - filesize($fullPath), - $errorCode, - true // true for test - ); - return $uploadedFile; - } -}