Skip to content

Commit

Permalink
Added public URLs for WebDAV
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Oct 5, 2022
1 parent d2d5fa6 commit 55a462d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 85 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ services:
AUTH_TYPE: Digest
USERNAME: alice
PASSWORD: secret1234
ANONYMOUS_METHODS: 'GET,OPTIONS'
sftp:
container_name: sftp
restart: always
Expand Down
98 changes: 60 additions & 38 deletions src/AdapterTestUtilities/FilesystemAdapterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace League\Flysystem\AdapterTestUtilities;

use const PHP_EOL;
use Generator;
use League\Flysystem\Config;
use League\Flysystem\DirectoryAttributes;
Expand All @@ -15,13 +14,17 @@
use League\Flysystem\UnableToReadFile;
use League\Flysystem\UnableToRetrieveMetadata;
use League\Flysystem\UnableToSetVisibility;
use League\Flysystem\UrlGeneration\PublicUrlGenerator;
use League\Flysystem\Visibility;
use PHPUnit\Framework\TestCase;
use Throwable;

use function file_get_contents;
use function is_resource;
use function iterator_to_array;

use const PHP_EOL;

/**
* @codeCoverageIgnore
*/
Expand Down Expand Up @@ -99,7 +102,7 @@ public function clearStorage(): void
return;
}

$this->runSetup(function () use ($adapter) {
$this->runSetup(function() use ($adapter) {
/** @var StorageAttributes $item */
foreach ($adapter->listContents('', false) as $item) {
if ($item->isDir()) {
Expand All @@ -124,7 +127,7 @@ public function clearCustomAdapter(): void
*/
public function writing_and_reading_with_string(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$adapter = $this->adapter();

$adapter->write('path.txt', 'contents', new Config());
Expand All @@ -141,7 +144,7 @@ public function writing_and_reading_with_string(): void
*/
public function writing_a_file_with_a_stream(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$adapter = $this->adapter();
$writeStream = stream_with_contents('contents');

Expand All @@ -163,7 +166,7 @@ public function writing_a_file_with_a_stream(): void
*/
public function writing_and_reading_files_with_special_path(string $path): void
{
$this->runScenario(function () use ($path) {
$this->runScenario(function() use ($path) {
$adapter = $this->adapter();

$adapter->write($path, 'contents', new Config());
Expand Down Expand Up @@ -196,7 +199,7 @@ public function filenameProvider(): Generator
*/
public function writing_a_file_with_an_empty_stream(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$adapter = $this->adapter();
$writeStream = stream_with_contents('');

Expand All @@ -222,7 +225,7 @@ public function reading_a_file(): void
{
$this->givenWeHaveAnExistingFile('path.txt', 'contents');

$this->runScenario(function () {
$this->runScenario(function() {
$contents = $this->adapter()->read('path.txt');

$this->assertEquals('contents', $contents);
Expand All @@ -236,7 +239,7 @@ public function reading_a_file_with_a_stream(): void
{
$this->givenWeHaveAnExistingFile('path.txt', 'contents');

$this->runScenario(function () {
$this->runScenario(function() {
$readStream = $this->adapter()->readStream('path.txt');
$contents = stream_get_contents($readStream);

Expand All @@ -251,7 +254,7 @@ public function reading_a_file_with_a_stream(): void
*/
public function overwriting_a_file(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$this->givenWeHaveAnExistingFile('path.txt', 'contents', ['visibility' => Visibility::PUBLIC]);
$adapter = $this->adapter();

Expand All @@ -269,7 +272,7 @@ public function overwriting_a_file(): void
*/
public function deleting_a_file(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$adapter = $this->adapter();
$this->givenWeHaveAnExistingFile('path.txt', 'contents');

Expand All @@ -285,7 +288,7 @@ public function deleting_a_file(): void
*/
public function listing_contents_shallow(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$this->givenWeHaveAnExistingFile('some/0-path.txt', 'contents');
$this->givenWeHaveAnExistingFile('some/1-nested/path.txt', 'contents');

Expand Down Expand Up @@ -313,7 +316,7 @@ public function listing_contents_shallow(): void
*/
public function checking_if_a_non_existing_directory_exists(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$adapter = $this->adapter();
self::assertFalse($adapter->directoryExists('this-does-not-exist.php'));
});
Expand All @@ -324,7 +327,7 @@ public function checking_if_a_non_existing_directory_exists(): void
*/
public function checking_if_a_directory_exists_after_writing_a_file(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$adapter = $this->adapter();
$this->givenWeHaveAnExistingFile('existing-directory/file.txt');
self::assertTrue($adapter->directoryExists('existing-directory'));
Expand All @@ -336,7 +339,7 @@ public function checking_if_a_directory_exists_after_writing_a_file(): void
*/
public function checking_if_a_directory_exists_after_creating_it(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$adapter = $this->adapter();
$adapter->createDirectory('explicitly-created-directory', new Config());
self::assertTrue($adapter->directoryExists('explicitly-created-directory'));
Expand All @@ -352,7 +355,7 @@ public function checking_if_a_directory_exists_after_creating_it(): void
*/
public function listing_contents_recursive(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$adapter = $this->adapter();
$adapter->createDirectory('path', new Config());
$adapter->write('path/file.txt', 'string', new Config());
Expand All @@ -378,7 +381,7 @@ protected function formatIncorrectListingCount(array $items): string

protected function givenWeHaveAnExistingFile(string $path, string $contents = 'contents', array $config = []): void
{
$this->runSetup(function () use ($path, $contents, $config) {
$this->runSetup(function() use ($path, $contents, $config) {
$this->adapter()->write($path, $contents, new Config($config));
});
}
Expand All @@ -391,7 +394,7 @@ public function fetching_file_size(): void
$adapter = $this->adapter();
$this->givenWeHaveAnExistingFile('path.txt', 'contents');

$this->runScenario(function () use ($adapter) {
$this->runScenario(function() use ($adapter) {
$attributes = $adapter->fileSize('path.txt');
$this->assertInstanceOf(FileAttributes::class, $attributes);
$this->assertEquals(8, $attributes->fileSize());
Expand All @@ -403,7 +406,7 @@ public function fetching_file_size(): void
*/
public function setting_visibility(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$adapter = $this->adapter();
$this->givenWeHaveAnExistingFile('path.txt', 'contents', [Config::OPTION_VISIBILITY => Visibility::PUBLIC]);

Expand All @@ -428,7 +431,7 @@ public function fetching_file_size_of_a_directory(): void

$adapter = $this->adapter();

$this->runScenario(function () use ($adapter) {
$this->runScenario(function() use ($adapter) {
$adapter->createDirectory('path', new Config());
$adapter->fileSize('path/');
});
Expand All @@ -441,7 +444,7 @@ public function fetching_file_size_of_non_existing_file(): void
{
$this->expectException(UnableToRetrieveMetadata::class);

$this->runScenario(function () {
$this->runScenario(function() {
$this->adapter()->fileSize('non-existing-file.txt');
});
}
Expand All @@ -453,7 +456,7 @@ public function fetching_last_modified_of_non_existing_file(): void
{
$this->expectException(UnableToRetrieveMetadata::class);

$this->runScenario(function () {
$this->runScenario(function() {
$this->adapter()->lastModified('non-existing-file.txt');
});
}
Expand All @@ -465,7 +468,7 @@ public function fetching_visibility_of_non_existing_file(): void
{
$this->expectException(UnableToRetrieveMetadata::class);

$this->runScenario(function () {
$this->runScenario(function() {
$this->adapter()->visibility('non-existing-file.txt');
});
}
Expand All @@ -475,7 +478,7 @@ public function fetching_visibility_of_non_existing_file(): void
*/
public function fetching_the_mime_type_of_an_svg_file(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$this->givenWeHaveAnExistingFile('file.svg', file_get_contents(__DIR__ . '/test_files/flysystem.svg'));

$mimetype = $this->adapter()->mimeType('file.svg')->mimeType();
Expand All @@ -491,7 +494,7 @@ public function fetching_mime_type_of_non_existing_file(): void
{
$this->expectException(UnableToRetrieveMetadata::class);

$this->runScenario(function () {
$this->runScenario(function() {
$this->adapter()->mimeType('non-existing-file.txt');
});
}
Expand All @@ -508,7 +511,7 @@ public function fetching_unknown_mime_type_of_a_file(): void

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

$this->runScenario(function () {
$this->runScenario(function() {
$this->adapter()->mimeType('unknown-mime-type.md5');
});
}
Expand All @@ -521,7 +524,7 @@ public function listing_a_toplevel_directory(): void
$this->givenWeHaveAnExistingFile('path1.txt');
$this->givenWeHaveAnExistingFile('path2.txt');

$this->runScenario(function () {
$this->runScenario(function() {
$contents = iterator_to_array($this->adapter()->listContents('', true));

$this->assertCount(2, $contents);
Expand All @@ -533,7 +536,7 @@ public function listing_a_toplevel_directory(): void
*/
public function writing_and_reading_with_streams(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$writeStream = stream_with_contents('contents');
$adapter = $this->adapter();

Expand All @@ -557,7 +560,7 @@ public function setting_visibility_on_a_file_that_does_not_exist(): void
{
$this->expectException(UnableToSetVisibility::class);

$this->runScenario(function () {
$this->runScenario(function() {
$this->adapter()->setVisibility('this-path-does-not-exists.txt', Visibility::PRIVATE);
});
}
Expand All @@ -567,7 +570,7 @@ public function setting_visibility_on_a_file_that_does_not_exist(): void
*/
public function copying_a_file(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$adapter = $this->adapter();
$adapter->write(
'source.txt',
Expand All @@ -589,7 +592,7 @@ public function copying_a_file(): void
*/
public function copying_a_file_again(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$adapter = $this->adapter();
$adapter->write(
'source.txt',
Expand All @@ -611,7 +614,7 @@ public function copying_a_file_again(): void
*/
public function moving_a_file(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$adapter = $this->adapter();
$adapter->write(
'source.txt',
Expand Down Expand Up @@ -639,7 +642,7 @@ public function reading_a_file_that_does_not_exist(): void
{
$this->expectException(UnableToReadFile::class);

$this->runScenario(function () {
$this->runScenario(function() {
$this->adapter()->read('path.txt');
});
}
Expand All @@ -651,7 +654,7 @@ public function moving_a_file_that_does_not_exist(): void
{
$this->expectException(UnableToMoveFile::class);

$this->runScenario(function () {
$this->runScenario(function() {
$this->adapter()->move('source.txt', 'destination.txt', new Config());
});
}
Expand All @@ -674,7 +677,7 @@ public function trying_to_delete_a_non_existing_file(): void
*/
public function checking_if_files_exist(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$adapter = $this->adapter();
$fileExistsBefore = $adapter->fileExists('some/path.txt');
$adapter->write('some/path.txt', 'contents', new Config());
Expand All @@ -690,7 +693,7 @@ public function checking_if_files_exist(): void
*/
public function fetching_last_modified(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$adapter = $this->adapter();
$adapter->write('path.txt', 'contents', new Config());

Expand Down Expand Up @@ -728,7 +731,7 @@ public function failing_to_read_a_non_existing_file(): void
*/
public function creating_a_directory(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$adapter = $this->adapter();

$adapter->createDirectory('creating_a_directory/path', new Config());
Expand All @@ -751,7 +754,7 @@ public function creating_a_directory(): void
*/
public function copying_a_file_with_collision(): void
{
$this->runScenario(function () {
$this->runScenario(function() {
$adapter = $this->adapter();
$adapter->write('path.txt', 'new contents', new Config());
$adapter->write('new-path.txt', 'contents', new Config());
Expand All @@ -765,9 +768,28 @@ public function copying_a_file_with_collision(): void

protected function assertFileExistsAtPath(string $path): void
{
$this->runScenario(function () use ($path) {
$this->runScenario(function() use ($path) {
$fileExists = $this->adapter()->fileExists($path);
$this->assertTrue($fileExists);
});
}

/**
* @test
*/
public function generating_a_public_url(): void
{
$adapter = $this->adapter();

if ( ! $adapter instanceof PublicUrlGenerator) {
$this->markTestSkipped('Adapter does not supply public URls');
}

$adapter->write('some/path.txt', 'public contents', new Config(['visibility' => 'public']));

$url = $adapter->publicUrl('some/path.txt', new Config());
$contents = file_get_contents($url);

self::assertEquals('public contents', $contents);
}
}
Loading

0 comments on commit 55a462d

Please sign in to comment.