-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## CHANGE - Change namespace of UploadedFileTestable trait to easy use in other package unit test.
- Loading branch information
Showing
5 changed files
with
76 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Padosoft\Laravel\Request; | ||
|
||
use Illuminate\Http\UploadedFile; | ||
|
||
/** | ||
* Class UploadedFileTestable | ||
* Trait to easy get an istance of Illuminate\Http\UploadedFile for testing. | ||
* @package Padosoft\Laravel\Request | ||
*/ | ||
trait UploadedFileTestable | ||
{ | ||
/** | ||
* 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 getUploadedFileForTest(string $fullPath, string $mimeType = '', int $errorCode = 0) : UploadedFile | ||
{ | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.