-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows you to fake a storage disk which replaces it with a local disk pointing into the storage/framework/testing directory. This also includes a way to generate “Fake” uploaded files, especially images, by doing: UploadedFile::fake()->image(‘name.jpg’, width, height)… you may also spoof the size of the image by doing ->size(300) where 300 is the number of kilobytes the file will report as its size.
- Loading branch information
1 parent
b4f9e2b
commit b964d4a
Showing
7 changed files
with
997 additions
and
0 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
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,90 @@ | ||
<?php | ||
|
||
namespace Illuminate\Http\Testing; | ||
|
||
use Illuminate\Http\UploadedFile; | ||
|
||
class File extends UploadedFile | ||
{ | ||
/** | ||
* The name of the file. | ||
* | ||
* @var string | ||
*/ | ||
public $name; | ||
|
||
/** | ||
* The temporary file resource. | ||
* | ||
* @var resource | ||
*/ | ||
public $tempFile; | ||
|
||
/** | ||
* The "size" to report. | ||
* | ||
* @var int | ||
*/ | ||
public $sizeToReport; | ||
|
||
/** | ||
* Create a new file instance. | ||
* | ||
* @param string $name | ||
* @param resource $tempFile | ||
* @return void | ||
*/ | ||
public function __construct($name, $tempFile) | ||
{ | ||
$this->name = $name; | ||
$this->tempFile = $tempFile; | ||
|
||
parent::__construct( | ||
$this->tempFilePath(), $name, $this->getMimeType(), | ||
filesize($this->tempFilePath()), $error = null, $test = true | ||
); | ||
} | ||
|
||
/** | ||
* Set the "size" of the file in kilobytes. | ||
* | ||
* @param int $kilobytes | ||
* @return $this | ||
*/ | ||
public function size($kilobytes) | ||
{ | ||
$this->sizeToReport = $kilobytes * 1024; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get the size of the file. | ||
* | ||
* @return int | ||
*/ | ||
public function getSize() | ||
{ | ||
return $this->sizeToReport ?: parent::getSize(); | ||
} | ||
|
||
/** | ||
* Get the MIME type for the file. | ||
* | ||
* @return string | ||
*/ | ||
public function getMimeType() | ||
{ | ||
return MimeType::from($this->name); | ||
} | ||
|
||
/** | ||
* Get the path to the temporary file. | ||
* | ||
* @return string | ||
*/ | ||
protected function tempFilePath() | ||
{ | ||
return stream_get_meta_data($this->tempFile)['uri']; | ||
} | ||
} |
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,47 @@ | ||
<?php | ||
|
||
namespace Illuminate\Http\Testing; | ||
|
||
class FileFactory | ||
{ | ||
/** | ||
* Create a new fake file. | ||
* | ||
* @param string $name | ||
* @param int $kilobytes | ||
* @return \Illuminate\Http\Testing\File | ||
*/ | ||
public function create($name, $kilobytes = null) | ||
{ | ||
return tap(new File($name, tmpfile()), function ($file) use ($kilobytes) { | ||
$file->sizeToReport = $kilobytes * 1024; | ||
}); | ||
} | ||
|
||
/** | ||
* Create a new fake image. | ||
* | ||
* @param string $name | ||
* @param int $height | ||
* @param int $height | ||
* @return \Illuminate\Http\Testing\File | ||
*/ | ||
public function image($name, $width = 10, $height = 10) | ||
{ | ||
return new File($name, $this->generateImage($width, $height)); | ||
} | ||
|
||
/** | ||
* Generate a dummy image of the given width and height. | ||
* | ||
* @param int $width | ||
* @param int $height | ||
* @return resource | ||
*/ | ||
protected function generateImage($width, $height) | ||
{ | ||
return tap(tmpfile(), function ($temp) use ($width, $height) { | ||
imagepng(imagecreatetruecolor($width, $height), $temp); | ||
}); | ||
} | ||
} |
Oops, something went wrong.