Skip to content

Commit

Permalink
Storage fake and fake files.
Browse files Browse the repository at this point in the history
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
taylorotwell committed Mar 1, 2017
1 parent b4f9e2b commit b964d4a
Show file tree
Hide file tree
Showing 7 changed files with 997 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Illuminate/Filesystem/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
use League\Flysystem\AdapterInterface;
use PHPUnit\Framework\Assert as PHPUnit;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\FileNotFoundException;
Expand Down Expand Up @@ -37,6 +38,32 @@ public function __construct(FilesystemInterface $driver)
$this->driver = $driver;
}

/**
* Assert that the given file exists.
*
* @param string $path
* @return void
*/
public function assertExists($path)
{
PHPUnit::assertTrue(
$this->exists($path), "Unable to find a file at path [{$path}]."
);
}

/**
* Assert that the given file does not exist.
*
* @param string $path
* @return void
*/
public function assertMissing($path)
{
PHPUnit::assertFalse(
$this->exists($path), "Found unexpected file at path [{$path}]."
);
}

/**
* Determine if a file exists.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Illuminate/Filesystem/FilesystemManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@ protected function adapt(FilesystemInterface $filesystem)
return new FilesystemAdapter($filesystem);
}

/**
* Set the given disk instance.
*
* @param string $name
* @param mixed $disk
* @return void
*/
public function set($name, $disk)
{
$this->disks[$name] = $disk;
}

/**
* Get the filesystem connection configuration.
*
Expand Down
90 changes: 90 additions & 0 deletions src/Illuminate/Http/Testing/File.php
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'];
}
}
47 changes: 47 additions & 0 deletions src/Illuminate/Http/Testing/FileFactory.php
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);
});
}
}
Loading

0 comments on commit b964d4a

Please sign in to comment.