Skip to content

Commit

Permalink
Using throw config of filesystem disks when faking (#53779)
Browse files Browse the repository at this point in the history
* Using throw config of filesystem disks when faking

* CS fix

* Further CS fixes

* Further CS fixes

* Further CS fixes

* Added dock blocks

* Added dots

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
emulgeator and taylorotwell authored Dec 10, 2024
1 parent da5e4e4 commit a53d179
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 9 deletions.
46 changes: 37 additions & 9 deletions src/Illuminate/Support/Facades/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,17 @@ class Storage extends Facade
*/
public static function fake($disk = null, array $config = [])
{
$disk = $disk ?: static::$app['config']->get('filesystems.default');

$root = storage_path('framework/testing/disks/'.$disk);
$root = self::getRootPath($disk = $disk ?: static::$app['config']->get('filesystems.default'));

if ($token = ParallelTesting::token()) {
$root = "{$root}_test_{$token}";
}

(new Filesystem)->cleanDirectory($root);

static::set($disk, $fake = static::createLocalDriver(array_merge($config, [
'root' => $root,
])));
static::set($disk, $fake = static::createLocalDriver(
self::buildDiskConfiguration($disk, $config, root: $root)
));

return tap($fake)->buildTemporaryUrlsUsing(function ($path, $expiration) {
return URL::to($path.'?expiration='.$expiration->getTimestamp());
Expand All @@ -126,13 +124,43 @@ public static function persistentFake($disk = null, array $config = [])
{
$disk = $disk ?: static::$app['config']->get('filesystems.default');

static::set($disk, $fake = static::createLocalDriver(array_merge($config, [
'root' => storage_path('framework/testing/disks/'.$disk),
])));
static::set($disk, $fake = static::createLocalDriver(
self::buildDiskConfiguration($disk, $config, root: self::getRootPath($disk))
));

return $fake;
}

/**
* Get the root path of the given disk.
*
* @param string $disk
* @return string
*/
protected static function getRootPath(string $disk): string
{
return storage_path('framework/testing/disks/'.$disk);
}

/**
* Assemble the configuration of the given disk.
*
* @param string $disk
* @param array $config
* @param string $root
* @return array
*/
protected static function buildDiskConfiguration(string $disk, array $config, string $root): array
{
$originalConfig = static::$app['config']["filesystems.disks.{$disk}"] ?? [];

return array_merge([
'throw' => $originalConfig['throw'] ?? false],
$config,
['root' => $root]
);
}

/**
* Get the registered name of the component.
*
Expand Down
57 changes: 57 additions & 0 deletions tests/Support/StorageFacadeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Illuminate\Tests\Support;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Storage;
use League\Flysystem\UnableToReadFile;
use Orchestra\Testbench\TestCase;

class StorageFacadeTest extends TestCase
{
public function testFake_whenDiskNotConfigured_doesNotThrowExceptionOnError()
{
$result = Storage::fake('test')->get('nonExistentFile');

$this->assertNull($result);
}

public function testFake_whenThrowSetToDisk_throwsExceptionOnError()
{
Config::set('filesystems.disks.test', ['throw' => true]);

$this->expectException(UnableToReadFile::class);
Storage::fake('test')->get('nonExistentFile');
}

public function testFake_whenThrowOverwritten_usesOverwrite()
{
Config::set('filesystems.disks.test', ['throw' => true]);

$result = Storage::fake('test', ['throw' => false])->get('nonExistentFile');
$this->assertNull($result);
}

public function testPersistentFake_whenDiskNotConfigured_doesNotThrowExceptionOnError()
{
$result = Storage::persistentFake('test')->get('nonExistentFile');

$this->assertNull($result);
}

public function testPersistentFake_whenThrowSetToDisk_throwsExceptionOnError()
{
Config::set('filesystems.disks.test', ['throw' => true]);

$this->expectException(UnableToReadFile::class);
Storage::persistentFake('test')->get('nonExistentFile');
}

public function testPersistentFake_whenThrowOverwritten_usesOverwrite()
{
Config::set('filesystems.disks.test', ['throw' => true]);

$result = Storage::persistentFake('test', ['throw' => false])->get('nonExistentFile');
$this->assertNull($result);
}
}

0 comments on commit a53d179

Please sign in to comment.