Skip to content

Commit

Permalink
Test temporary URL generation, allow injection of temporary URL gener…
Browse files Browse the repository at this point in the history
…ator.
  • Loading branch information
frankdejonge committed Oct 21, 2022
1 parent 839e794 commit 9971604
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ public function __construct(
array $config = [],
PathNormalizer $pathNormalizer = null,
PublicUrlGenerator $publicUrlGenerator = null,
TemporaryUrlGenerator $temporaryUrlGenerator = null,
) {
$this->adapter = $adapter;
$this->config = new Config($config);
$this->pathNormalizer = $pathNormalizer ?: new WhitespacePathNormalizer();
$this->publicUrlGenerator = $publicUrlGenerator;
$this->temporaryUrlGenerator = $temporaryUrlGenerator;
}

public function fileExists(string $location): bool
Expand Down
39 changes: 37 additions & 2 deletions src/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

namespace League\Flysystem;

use Aws\S3\S3Client;
use DateTimeImmutable;
use DateTimeInterface;
use Generator;
use IteratorAggregate;
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
use League\Flysystem\Local\LocalFilesystemAdapter;
use League\Flysystem\UrlGeneration\PublicUrlGenerator;
use League\Flysystem\UrlGeneration\TemporaryUrlGenerator;
use LogicException;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -556,6 +557,40 @@ public function unable_to_get_checksum_for_for_file_that_does_not_exist(): void
$filesystem->checksum('path.txt');
}

/**
* @test
*/
public function generating_temporary_urls(): void
{
$filesystem = new Filesystem(
new InMemoryFilesystemAdapter(),
temporaryUrlGenerator: new class() implements TemporaryUrlGenerator {
public function temporaryUrl(string $path, DateTimeInterface $expiresAt, Config $config): string
{
return 'https://flysystem.thephpleague.com/' . $path . '?exporesAt=' . $expiresAt->format('U');
}
}
);

$now = \time();
$temporaryUrl = $filesystem->temporaryUrl('some/file.txt', new DateTimeImmutable('@' . $now));
$expectedUrl = 'https://flysystem.thephpleague.com/some/file.txt?exporesAt=' . $now;

self::assertEquals($expectedUrl, $temporaryUrl);
}

/**
* @test
*/
public function not_being_able_to_generate_temporary_urls(): void
{
$filesystem = new Filesystem(new InMemoryFilesystemAdapter());

$this->expectException(UnableToGenerateTemporaryUrl::class);

$filesystem->temporaryUrl('some/file.txt', new DateTimeImmutable());
}

/**
* @test
*/
Expand Down

0 comments on commit 9971604

Please sign in to comment.