/
githubmirror
/
framework
Обзор
Документация
Войти
/
githubmirror
/
framework
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
13.x
tests/Filesystem/FilesystemManagerTest.php
301 строка
10 KB
Lucas Michot
[13.x] Simplify most of the exceptions expectations (#61049)
05 авг 2026, 18:34
Не верифицирован
05 авг 2026, 18:34
910cd94
Код
Авторство
О чём код?
<?php namespace Illuminate\Tests\Filesystem; use Illuminate\Contracts\Filesystem\Filesystem; use Illuminate\Filesystem\FilesystemManager; use Illuminate\Foundation\Application; use InvalidArgumentException; use League\Flysystem\PathPrefixing\PathPrefixedAdapter; use League\Flysystem\UnableToReadFile; use PHPUnit\Framework\Attributes\RequiresOperatingSystem; use PHPUnit\Framework\TestCase; use stdClass; class FilesystemManagerTest extends TestCase { public function testExceptionThrownOnUnsupportedDriver() { $this->expectExceptionObject(new InvalidArgumentException('Disk [local] does not have a configured driver.')); $filesystem = new FilesystemManager(tap(new Application, function ($app) { $app['config'] = ['filesystems.disks.local' => null]; })); $filesystem->disk('local'); } public function testCanBuildOnDemandDisk() { $filesystem = new FilesystemManager(new Application); $this->assertInstanceOf(Filesystem::class, $filesystem->build('my-custom-path')); $this->assertInstanceOf(Filesystem::class, $filesystem->build([ 'driver' => 'local', 'root' => 'my-custom-path', 'url' => 'my-custom-url', 'visibility' => 'public', ])); rmdir(__DIR__.'/../../my-custom-path'); } public function testCanBuildReadOnlyDisks() { $filesystem = new FilesystemManager(new Application); $disk = $filesystem->build([ 'driver' => 'local', 'read-only' => true, 'root' => 'my-custom-path', 'url' => 'my-custom-url', 'visibility' => 'public', ]); file_put_contents(__DIR__.'/../../my-custom-path/path.txt', 'contents'); // read operations work $this->assertSame('contents', $disk->get('path.txt')); $this->assertEquals(['path.txt'], $disk->files()); // write operations fail $this->assertFalse($disk->put('path.txt', 'contents')); $this->assertFalse($disk->delete('path.txt')); $this->assertFalse($disk->deleteDirectory('directory')); $this->assertFalse($disk->prepend('path.txt', 'data')); $this->assertFalse($disk->append('path.txt', 'data')); $handle = fopen('php://memory', 'rw'); fwrite($handle, 'content'); $this->assertFalse($disk->writeStream('path.txt', $handle)); fclose($handle); unlink(__DIR__.'/../../my-custom-path/path.txt'); rmdir(__DIR__.'/../../my-custom-path'); } public function testCanBuildScopedDisks() { try { $filesystem = new FilesystemManager(tap(new Application, function ($app) { $app['config'] = [ 'filesystems.disks.local' => [ 'driver' => 'local', 'root' => 'to-be-scoped', ], ]; })); $local = $filesystem->disk('local'); $scoped = $filesystem->build([ 'driver' => 'scoped', 'disk' => 'local', 'prefix' => 'path-prefix', ]); $scoped->put('dirname/filename.txt', 'file content'); $this->assertSame('file content', $local->get('path-prefix/dirname/filename.txt')); $local->deleteDirectory('path-prefix'); } finally { rmdir(__DIR__.'/../../to-be-scoped'); } } public function testCanBuildScopedDiskFromScopedDisk() { try { $filesystem = new FilesystemManager(tap(new Application, function ($app) { $app['config'] = [ 'filesystems.disks.local' => [ 'driver' => 'local', 'root' => 'root-to-be-scoped', ], 'filesystems.disks.scoped-from-root' => [ 'driver' => 'scoped', 'disk' => 'local', 'prefix' => 'scoped-from-root-prefix', ], ]; })); $root = $filesystem->disk('local'); $nestedScoped = $filesystem->build([ 'driver' => 'scoped', 'disk' => 'scoped-from-root', 'prefix' => 'nested-scoped-prefix', ]); $nestedScoped->put('dirname/filename.txt', 'file content'); $this->assertSame('file content', $root->get('scoped-from-root-prefix/nested-scoped-prefix/dirname/filename.txt')); $root->deleteDirectory('scoped-from-root-prefix'); } finally { rmdir(__DIR__.'/../../root-to-be-scoped'); } } #[RequiresOperatingSystem('Linux|Darwin')] public function testCanBuildScopedDisksWithVisibility() { try { $filesystem = new FilesystemManager(tap(new Application, function ($app) { $app['config'] = [ 'filesystems.disks.local' => [ 'driver' => 'local', 'root' => 'to-be-scoped', 'visibility' => 'public', ], ]; })); $scoped = $filesystem->build([ 'driver' => 'scoped', 'disk' => 'local', 'prefix' => 'path-prefix', 'visibility' => 'private', ]); $scoped->put('dirname/filename.txt', 'file content'); $this->assertSame('private', $scoped->getVisibility('dirname/filename.txt')); } finally { unlink(__DIR__.'/../../to-be-scoped/path-prefix/dirname/filename.txt'); rmdir(__DIR__.'/../../to-be-scoped/path-prefix/dirname'); rmdir(__DIR__.'/../../to-be-scoped/path-prefix'); rmdir(__DIR__.'/../../to-be-scoped'); } } public function testCanBuildScopedDisksWithThrow() { try { $filesystem = new FilesystemManager(tap(new Application, function ($app) { $app['config'] = [ 'filesystems.disks.local' => [ 'driver' => 'local', 'root' => 'to-be-scoped', 'throw' => false, ], ]; })); $scoped = $filesystem->build([ 'driver' => 'scoped', 'disk' => 'local', 'prefix' => 'path-prefix', 'throw' => true, ]); $this->expectException(UnableToReadFile::class); $scoped->get('dirname/filename.txt'); } finally { rmdir(__DIR__.'/../../to-be-scoped'); } } public function testCanBuildInlineScopedDisks() { try { $filesystem = new FilesystemManager(new Application); $scoped = $filesystem->build([ 'driver' => 'scoped', 'disk' => [ 'driver' => 'local', 'root' => 'to-be-scoped', ], 'prefix' => 'path-prefix', ]); $scoped->put('dirname/filename.txt', 'file content'); $this->assertDirectoryExists(__DIR__.'/../../to-be-scoped/path-prefix'); $this->assertSame('file content', file_get_contents(__DIR__.'/../../to-be-scoped/path-prefix/dirname/filename.txt')); } finally { unlink(__DIR__.'/../../to-be-scoped/path-prefix/dirname/filename.txt'); rmdir(__DIR__.'/../../to-be-scoped/path-prefix/dirname'); rmdir(__DIR__.'/../../to-be-scoped/path-prefix'); rmdir(__DIR__.'/../../to-be-scoped'); } } public function testCustomDriverClosureBoundObjectIsFilesystemManager() { $manager = new FilesystemManager(tap(new Application, function ($app) { $app['config'] = [ 'filesystems.disks.'.__CLASS__ => [ 'driver' => __CLASS__, ], ]; })); $manager->extend(__CLASS__, fn () => $this); $this->assertSame($manager, $manager->disk(__CLASS__)); } public function testCustomDriverStaticClosure() { $manager = new FilesystemManager(tap(new Application, static function ($app) { $app['config'] = [ 'filesystems.disks.'.__CLASS__ => [ 'driver' => __CLASS__, ], ]; })); $driver = new stdClass; $manager->extend(__CLASS__, static fn () => $driver); $this->assertSame($driver, $manager->disk(__CLASS__)); } public function testInvokableObjectDriverClosure() { $manager = new FilesystemManager(tap(new Application, static function ($app) { $app['config'] = [ 'filesystems.disks.'.__CLASS__ => [ 'driver' => __CLASS__, ], ]; })); $driver = new stdClass; $creator = new CustomFilesystemDriver($driver); $manager->extend(__CLASS__, $creator(...)); $this->assertSame($driver, $manager->disk(__CLASS__)); } // public function testKeepTrackOfAdapterDecoration() // { // try { // $filesystem = new FilesystemManager(tap(new Application, function ($app) { // $app['config'] = [ // 'filesystems.disks.local' => [ // 'driver' => 'local', // 'root' => 'to-be-scoped', // ], // ]; // })); // $scoped = $filesystem->build([ // 'driver' => 'scoped', // 'disk' => 'local', // 'prefix' => 'path-prefix', // ]); // $this->assertInstanceOf(PathPrefixedAdapter::class, $scoped->getAdapter()); // } finally { // rmdir(__DIR__.'/../../to-be-scoped'); // } // } } class CustomFilesystemDriver { public function __construct(private object $driver) { } public function __invoke() { return $this->driver; } }