Skip to content

Commit e501a1f

Browse files
committed
feat: improve missing adapter exception
1 parent a053364 commit e501a1f

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/Tempest/Storage/src/MissingAdapterException.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,40 @@
33
namespace Tempest\Storage;
44

55
use Exception;
6+
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
7+
use League\Flysystem\AzureBlobStorage\AzureBlobStorageAdapter;
8+
use League\Flysystem\Ftp\FtpAdapter;
9+
use League\Flysystem\GoogleCloudStorage\GoogleCloudStorageAdapter;
10+
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
11+
use League\Flysystem\PhpseclibV3\SftpAdapter;
12+
use League\Flysystem\ReadOnly\ReadOnlyFilesystemAdapter;
13+
use League\Flysystem\ZipArchive\ZipArchiveAdapter;
614

715
final class MissingAdapterException extends Exception implements StorageException
816
{
9-
public function __construct(string $missing)
17+
public function __construct(
18+
private readonly string $missing,
19+
) {
20+
$packageName = $this->getPackageName();
21+
$message = $packageName
22+
? sprintf('The `%s` adapter is missing. Install it using `composer require %s`.', $missing, $packageName)
23+
: sprintf('The `%s` adapter is missing.', $missing);
24+
25+
parent::__construct($message);
26+
}
27+
28+
private function getPackageName(): ?string
1029
{
11-
parent::__construct(
12-
message: sprintf('The `%s` adapter is missing.', $missing),
13-
);
30+
return match ($this->missing) {
31+
AwsS3V3Adapter::class => 'league/flysystem-aws-s3-v3',
32+
InMemoryFilesystemAdapter::class => 'league/flysystem-memory',
33+
ReadOnlyFilesystemAdapter::class => 'league/flysystem-read-only',
34+
SftpAdapter::class => 'league/flysystem-sftp',
35+
ZipArchiveAdapter::class => 'league/flysystem-ziparchive',
36+
AzureBlobStorageAdapter::class => 'league/flysystem-azure-blob-storage',
37+
FtpAdapter::class => 'league/flysystem-ftp',
38+
GoogleCloudStorageAdapter::class => 'league/flysystem-google-cloud-storage',
39+
default => null,
40+
};
1441
}
1542
}

tests/Integration/Storage/StorageTesterTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
use DateTime;
66
use DateTimeInterface;
7+
use League\Flysystem\FilesystemAdapter;
8+
use League\Flysystem\Local\LocalFilesystemAdapter;
9+
use Tempest\Storage\Config\StorageConfig;
710
use Tempest\Storage\ForbiddenStorageUsageException;
11+
use Tempest\Storage\MissingAdapterException;
812
use Tempest\Storage\Storage;
913
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
1014

@@ -101,4 +105,23 @@ public function test_prevent_usage_without_fake_with_fake(): void
101105

102106
$this->storage->assertFileExists('bar.txt');
103107
}
108+
109+
public function test_no_adapter(): void
110+
{
111+
$this->expectException(MissingAdapterException::class);
112+
$this->expectExceptionMessage('The `UnknownClass` adapter is missing');
113+
114+
$this->container->config(new class implements StorageConfig {
115+
public string $adapter = 'UnknownClass';
116+
public bool $readonly = false;
117+
118+
public function createAdapter(): FilesystemAdapter
119+
{
120+
return new LocalFilesystemAdapter(__DIR__);
121+
}
122+
});
123+
124+
$storage = $this->container->get(Storage::class);
125+
$storage->write('bar.txt', 'baz');
126+
}
104127
}

0 commit comments

Comments
 (0)