Skip to content

Instantly share code, notes, and snippets.

@richartkeil
Created November 20, 2018 00:00
Show Gist options
  • Save richartkeil/761c926d807a695d540363ccba815ad4 to your computer and use it in GitHub Desktop.
Save richartkeil/761c926d807a695d540363ccba815ad4 to your computer and use it in GitHub Desktop.
Laravel Storage and Parallel Testing - CreatesFakeStorage
<?php
namespace Tests;
use Storage;
use Illuminate\Filesystem\Filesystem;
trait CreatesFakeStorage
{
/**
* Replace the given disk with a local testing disk. We use a unique
* base path for every test to prevent issues with the filesystem
* when running tests in a multi process environment.
*
* @param string|null $disk
* @return void
*/
protected function fakeStorage($disk = null)
{
$disk = $disk ?: $this->$app['config']->get('filesystems.default');
$time = (int) (microtime(true) * 1000);
$base = storage_path('framework/testing/' . $time);
$root = $base . '/disks/' . $disk;
$this->beforeApplicationDestroyed(function() use ($base) {
(new Filesystem)->deleteDirectory($base);
});
Storage::set($disk, Storage::createLocalDriver(['root' => $root]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment