Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Storage fake and fake files. #18178

Merged
merged 5 commits into from
Mar 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
{
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