Return fake filesystem

This commit is contained in:
philipp lang 2024-01-30 13:30:50 +01:00
parent d09a91dfaa
commit ca5d5e157b
2 changed files with 10 additions and 8 deletions

View File

@ -14,6 +14,10 @@ class FilesystemConfig
public string $path;
public StorageType $type;
public function __construct(public string $identifier)
{
}
public function host(string $host): self
{
$this->host = $host;
@ -49,7 +53,7 @@ class FilesystemConfig
return $this;
}
public function swap(): void
public function swap()
{
if (!app()->has('original-filesystem')) {
app()->singleton('original-filesystem', fn () => app(FilesystemManager::class));
@ -62,8 +66,10 @@ class FilesystemConfig
if ($this->type === StorageType::FTP) {
$mock->shouldReceive('createFtpDriver')->withArgs(fn ($config) => $this->host === $config['host'] && $this->username === $config['username'] && $this->password === $config['password'] && $this->path === $config['root'])
->andReturnUsing(function () {
return app('original-filesystem')->createLocalDriver(['root' => __DIR__ . '/fakes']);
return app('original-filesystem')->createLocalDriver(['root' => __DIR__ . '/fakes/' . $this->identifier]);
});
return app('original-filesystem')->createLocalDriver(['root' => __DIR__ . '/fakes/' . $this->identifier]);
}
}
}

View File

@ -5,18 +5,14 @@ namespace Zoomyboy\FilesystemTests;
trait TestsFilesystems
{
public $mockedSystems = [];
public bool $mocked = false;
public function mockFilesystem(string $identifier): FilesystemConfig
{
$this->beforeApplicationDestroyed(function () {
@exec('rm -R ' . __DIR__ . '/fakes/* > /dev/null 2>&1');
});
return $this->mockedSystems[$identifier] = data_get($this->mockedSystems, $identifier, new FilesystemConfig($identifier));
}
public function clearFilesystems(): void
{
exec('rm -R ' . __DIR__ . '/fakes/*');
return new FilesystemConfig($identifier);
}
}