Skip to content

Commit a53d179

Browse files
Using throw config of filesystem disks when faking (#53779)
* 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 <[email protected]>
1 parent da5e4e4 commit a53d179

File tree

2 files changed

+94
-9
lines changed

2 files changed

+94
-9
lines changed

src/Illuminate/Support/Facades/Storage.php

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,17 @@ class Storage extends Facade
9696
*/
9797
public static function fake($disk = null, array $config = [])
9898
{
99-
$disk = $disk ?: static::$app['config']->get('filesystems.default');
100-
101-
$root = storage_path('framework/testing/disks/'.$disk);
99+
$root = self::getRootPath($disk = $disk ?: static::$app['config']->get('filesystems.default'));
102100

103101
if ($token = ParallelTesting::token()) {
104102
$root = "{$root}_test_{$token}";
105103
}
106104

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

109-
static::set($disk, $fake = static::createLocalDriver(array_merge($config, [
110-
'root' => $root,
111-
])));
107+
static::set($disk, $fake = static::createLocalDriver(
108+
self::buildDiskConfiguration($disk, $config, root: $root)
109+
));
112110

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

129-
static::set($disk, $fake = static::createLocalDriver(array_merge($config, [
130-
'root' => storage_path('framework/testing/disks/'.$disk),
131-
])));
127+
static::set($disk, $fake = static::createLocalDriver(
128+
self::buildDiskConfiguration($disk, $config, root: self::getRootPath($disk))
129+
));
132130

133131
return $fake;
134132
}
135133

134+
/**
135+
* Get the root path of the given disk.
136+
*
137+
* @param string $disk
138+
* @return string
139+
*/
140+
protected static function getRootPath(string $disk): string
141+
{
142+
return storage_path('framework/testing/disks/'.$disk);
143+
}
144+
145+
/**
146+
* Assemble the configuration of the given disk.
147+
*
148+
* @param string $disk
149+
* @param array $config
150+
* @param string $root
151+
* @return array
152+
*/
153+
protected static function buildDiskConfiguration(string $disk, array $config, string $root): array
154+
{
155+
$originalConfig = static::$app['config']["filesystems.disks.{$disk}"] ?? [];
156+
157+
return array_merge([
158+
'throw' => $originalConfig['throw'] ?? false],
159+
$config,
160+
['root' => $root]
161+
);
162+
}
163+
136164
/**
137165
* Get the registered name of the component.
138166
*

tests/Support/StorageFacadeTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Support;
4+
5+
use Illuminate\Support\Facades\Config;
6+
use Illuminate\Support\Facades\Storage;
7+
use League\Flysystem\UnableToReadFile;
8+
use Orchestra\Testbench\TestCase;
9+
10+
class StorageFacadeTest extends TestCase
11+
{
12+
public function testFake_whenDiskNotConfigured_doesNotThrowExceptionOnError()
13+
{
14+
$result = Storage::fake('test')->get('nonExistentFile');
15+
16+
$this->assertNull($result);
17+
}
18+
19+
public function testFake_whenThrowSetToDisk_throwsExceptionOnError()
20+
{
21+
Config::set('filesystems.disks.test', ['throw' => true]);
22+
23+
$this->expectException(UnableToReadFile::class);
24+
Storage::fake('test')->get('nonExistentFile');
25+
}
26+
27+
public function testFake_whenThrowOverwritten_usesOverwrite()
28+
{
29+
Config::set('filesystems.disks.test', ['throw' => true]);
30+
31+
$result = Storage::fake('test', ['throw' => false])->get('nonExistentFile');
32+
$this->assertNull($result);
33+
}
34+
35+
public function testPersistentFake_whenDiskNotConfigured_doesNotThrowExceptionOnError()
36+
{
37+
$result = Storage::persistentFake('test')->get('nonExistentFile');
38+
39+
$this->assertNull($result);
40+
}
41+
42+
public function testPersistentFake_whenThrowSetToDisk_throwsExceptionOnError()
43+
{
44+
Config::set('filesystems.disks.test', ['throw' => true]);
45+
46+
$this->expectException(UnableToReadFile::class);
47+
Storage::persistentFake('test')->get('nonExistentFile');
48+
}
49+
50+
public function testPersistentFake_whenThrowOverwritten_usesOverwrite()
51+
{
52+
Config::set('filesystems.disks.test', ['throw' => true]);
53+
54+
$result = Storage::persistentFake('test', ['throw' => false])->get('nonExistentFile');
55+
$this->assertNull($result);
56+
}
57+
}

0 commit comments

Comments
 (0)