Skip to content

Commit 3b7a053

Browse files
committed
feat: support public urls and improve coverage
1 parent e501a1f commit 3b7a053

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

src/Tempest/Storage/src/GenericStorage.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use League\Flysystem\Filesystem;
77
use League\Flysystem\FilesystemAdapter;
88
use League\Flysystem\ReadOnly\ReadOnlyFilesystemAdapter;
9+
use League\Flysystem\UrlGeneration\PublicUrlGenerator;
910
use League\Flysystem\UrlGeneration\TemporaryUrlGenerator;
1011
use Tempest\Storage\Config\StorageConfig;
1112

@@ -15,9 +16,11 @@ public function __construct(
1516
private StorageConfig $storageConfig,
1617
private ?Filesystem $filesystem = null,
1718
?TemporaryUrlGenerator $temporaryUrlGenerator = null,
19+
?PublicUrlGenerator $publicUrlGenerator = null,
1820
) {
1921
$this->filesystem ??= new Filesystem(
2022
adapter: $this->createAdapter(),
23+
publicUrlGenerator: $publicUrlGenerator,
2124
temporaryUrlGenerator: $temporaryUrlGenerator,
2225
);
2326
}

src/Tempest/Storage/src/Testing/StorageTester.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Closure;
66
use DateTimeInterface;
77
use League\Flysystem\Config;
8+
use League\Flysystem\UrlGeneration\PublicUrlGenerator;
89
use League\Flysystem\UrlGeneration\TemporaryUrlGenerator;
910
use PHPUnit\Framework\Assert;
1011
use Tempest\Container\Container;
@@ -63,13 +64,37 @@ public function temporaryUrl(string $path, DateTimeInterface $expiresAt, Config
6364
$this->storage->setTemporaryUrlGenerator($generator);
6465
}
6566

67+
public function createPublicUrlsUsing(Closure $closure): void
68+
{
69+
$generator = new class($closure) implements PublicUrlGenerator {
70+
public function __construct(
71+
private readonly Closure $closure,
72+
) {}
73+
74+
public function publicUrl(string $path, Config $config): string
75+
{
76+
return ($this->closure)($path);
77+
}
78+
};
79+
80+
$this->storage->setPublicUrlGenerator($generator);
81+
}
82+
6683
public function assertFileExists(string $path): void
6784
{
6885
$storage = $this->container->get(Storage::class);
6986

7087
Assert::assertTrue($storage->fileExists($path), sprintf('File `%s` does not exist.', $path));
7188
}
7289

90+
public function assertChecksumEquals(string $path, string $checksum): void
91+
{
92+
$storage = $this->container->get(Storage::class);
93+
94+
$this->assertFileExists($path);
95+
Assert::assertEquals($checksum, $storage->checksum($path), sprintf('File `%s` checksum does not match `%s`.', $path, $checksum));
96+
}
97+
7398
public function assertSee(string $path, string $contents): void
7499
{
75100
$storage = $this->container->get(Storage::class);

src/Tempest/Storage/src/Testing/TestingStorage.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tempest\Storage\Testing;
44

55
use DateTimeInterface;
6+
use League\Flysystem\UrlGeneration\PublicUrlGenerator;
67
use League\Flysystem\UrlGeneration\TemporaryUrlGenerator;
78
use Tempest\Storage\Config\LocalStorageConfig;
89
use Tempest\Storage\DirectoryListing;
@@ -15,16 +16,28 @@ final class TestingStorage implements Storage
1516
{
1617
private Storage $storage;
1718

19+
private ?TemporaryUrlGenerator $temporaryUrlGenerator = null;
20+
21+
private ?PublicUrlGenerator $publicUrlGenerator = null;
22+
1823
public function __construct(
1924
private readonly ?string $path = null,
2025
?TemporaryUrlGenerator $temporaryUrlGenerator = null,
26+
?PublicUrlGenerator $publicUrlGenerator = null,
2127
) {
22-
$this->storage = $this->createStorage($path, $temporaryUrlGenerator);
28+
$this->storage = $this->createStorage($path, $temporaryUrlGenerator, $publicUrlGenerator);
2329
}
2430

2531
public function setTemporaryUrlGenerator(TemporaryUrlGenerator $generator): static
2632
{
27-
$this->storage = $this->createStorage($this->path, $generator);
33+
$this->storage = $this->createStorage($this->path, temporaryUrlGenerator: $generator);
34+
35+
return $this;
36+
}
37+
38+
public function setPublicUrlGenerator(PublicUrlGenerator $generator): static
39+
{
40+
$this->storage = $this->createStorage($this->path, publicUrlGenerator: $generator);
2841

2942
return $this;
3043
}
@@ -157,14 +170,15 @@ public function list(string $location = '', bool $deep = false): DirectoryListin
157170
return $this->storage->list($location, $deep);
158171
}
159172

160-
private function createStorage(?string $path = null, ?TemporaryUrlGenerator $temporaryUrlGenerator = null): Storage
173+
private function createStorage(?string $path = null, ?TemporaryUrlGenerator $temporaryUrlGenerator = null, ?PublicUrlGenerator $publicUrlGenerator = null): Storage
161174
{
162175
return new GenericStorage(
163176
storageConfig: new LocalStorageConfig(
164177
path: internal_storage_path('tests/storage/' . ($path ?? 'storage')),
165178
readonly: false,
166179
),
167-
temporaryUrlGenerator: $temporaryUrlGenerator,
180+
temporaryUrlGenerator: $temporaryUrlGenerator ?? $this->temporaryUrlGenerator,
181+
publicUrlGenerator: $publicUrlGenerator ?? $this->publicUrlGenerator,
168182
);
169183
}
170184
}

tests/Integration/Storage/StorageTesterTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public function test_directory_assertions(): void
4848
$storage->write('foo/bar.txt', 'baz');
4949
$storage->createDirectory('empty');
5050

51+
$this->storage->assertChecksumEquals('foo/bar.txt', '73feffa4b7f6bb68e44cf984c85f6e88');
52+
5153
$this->storage->assertFileExists('foo/bar.txt');
5254
$this->storage->assertFileOrDirectoryExists('foo/bar.txt');
5355
$this->storage->assertDirectoryExists('foo');
@@ -65,12 +67,29 @@ public function test_directory_assertions(): void
6567

6668
$storage->cleanDirectory();
6769
$this->storage->assertDirectoryEmpty();
70+
71+
$storage->write('foo/bar.txt', 'baz');
72+
$storage->delete('foo/bar.txt');
73+
$this->storage->assertDirectoryEmpty('foo');
74+
75+
$storage->deleteDirectory('foo');
76+
$this->storage->assertDirectoryEmpty();
6877
}
6978

70-
public function test_temporary_urls(): void
79+
public function test_public_url(): void
7180
{
7281
$this->storage->fake();
82+
$this->storage->createPublicUrlsUsing(fn (string $path) => sprintf('https://localhost/%s', $path));
83+
84+
$storage = $this->container->get(Storage::class);
85+
$storage->write('foo.txt', 'bar');
86+
87+
$this->assertSame('https://localhost/foo.txt', $storage->publicUrl('foo.txt'));
88+
}
7389

90+
public function test_temporary_urls(): void
91+
{
92+
$this->storage->fake();
7493
$this->storage->createTemporaryUrlsUsing(fn (string $path, DateTimeInterface $expiresAt) => sprintf(
7594
'https://localhost/%s?expires=%s',
7695
$path,
@@ -113,6 +132,7 @@ public function test_no_adapter(): void
113132

114133
$this->container->config(new class implements StorageConfig {
115134
public string $adapter = 'UnknownClass';
135+
116136
public bool $readonly = false;
117137

118138
public function createAdapter(): FilesystemAdapter

0 commit comments

Comments
 (0)