Created
November 20, 2018 00:00
-
-
Save richartkeil/761c926d807a695d540363ccba815ad4 to your computer and use it in GitHub Desktop.
Laravel Storage and Parallel Testing - CreatesFakeStorage
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
<?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