From 76552296531a17f2a35096943d2e1731cd5c9628 Mon Sep 17 00:00:00 2001 From: Jakob Givoni Date: Fri, 9 Feb 2024 15:47:31 +0100 Subject: [PATCH] 16-purge-cache-if-missing-metadata-cannot-be-retrieved --- .vscode/settings.json | 5 +++++ src/CacheAdapter.php | 1 - src/CacheItemsTrait.php | 8 +++++++- tests/CacheTestCase.php | 1 + tests/GetChecksum_Test.php | 18 ++++++++++++++++++ tests/GetFileSize_Test.php | 17 +++++++++++++++++ tests/GetLastModified_Test.php | 17 +++++++++++++++++ tests/GetMimetype_Test.php | 17 +++++++++++++++++ tests/GetVisibility_Test.php | 17 +++++++++++++++++ 9 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..5f2d270 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "cSpell.words": [ + "Flysystem" + ] +} \ No newline at end of file diff --git a/src/CacheAdapter.php b/src/CacheAdapter.php index e7af0a7..93d0c4c 100644 --- a/src/CacheAdapter.php +++ b/src/CacheAdapter.php @@ -11,7 +11,6 @@ use League\Flysystem\FilesystemAdapter; use League\Flysystem\StorageAttributes; use League\Flysystem\UnableToCopyFile; -use League\Flysystem\UnableToDeleteFile; use League\Flysystem\UnableToMoveFile; use League\Flysystem\UnableToProvideChecksum; use League\Flysystem\UnableToReadFile; diff --git a/src/CacheItemsTrait.php b/src/CacheItemsTrait.php index d2d45f8..d262d20 100644 --- a/src/CacheItemsTrait.php +++ b/src/CacheItemsTrait.php @@ -9,6 +9,7 @@ use Psr\Cache\CacheItemInterface; use Psr\Cache\CacheItemPoolInterface; use League\Flysystem\FilesystemAdapter; +use League\Flysystem\UnableToProvideChecksum; use League\Flysystem\UnableToRetrieveMetadata; use RuntimeException; @@ -140,7 +141,12 @@ protected function getFileAttributes( } if ($attributeAccessor($fileAttributes) === null) { - $fileAttributesExtension = $loader(); + try { + $fileAttributesExtension = $loader(); + } catch (UnableToRetrieveMetadata | UnableToProvideChecksum $e) { + $this->purgeCacheItem($path); + throw $e; + } $fileAttributes = self::mergeFileAttributes( fileAttributesBase: $fileAttributes, diff --git a/tests/CacheTestCase.php b/tests/CacheTestCase.php index c28c85b..2968d77 100644 --- a/tests/CacheTestCase.php +++ b/tests/CacheTestCase.php @@ -34,6 +34,7 @@ public function setUp(): void 'fully-cached-file' => new FileAttributes('fully-cached-file', 10, Visibility::PUBLIC), 'partially-cached-file' => new FileAttributes('partially-cached-file'), 'deleted-cached-file' => new FileAttributes('deleted-cached-file', 10, Visibility::PUBLIC), + 'partially-cached-deleted-file' => new FileAttributes('partially-cached-deleted-file'), 'overwritten-file' => new FileAttributes('overwritten-file', 20, Visibility::PUBLIC), 'cached-directory' => new DirectoryAttributes('cached-directory', visibility: Visibility::PUBLIC), 'cached-directory/file' => new FileAttributes('cached-directory/file', 10), diff --git a/tests/GetChecksum_Test.php b/tests/GetChecksum_Test.php index 2e20e94..3533fb9 100644 --- a/tests/GetChecksum_Test.php +++ b/tests/GetChecksum_Test.php @@ -7,6 +7,7 @@ use League\Flysystem\Config; use League\Flysystem\FileAttributes; use League\Flysystem\UnableToProvideChecksum; +use League\Flysystem\UnableToRetrieveMetadata; use Mockery; use Mockery\MockInterface; @@ -95,4 +96,21 @@ public static function errorDataProvider(): iterable yield 'Path is directory (cached)' => ['cached-directory']; yield 'Path is directory (non-cached)' => ['non-cached-directory']; } + + /** + * @test + */ + public function cache_is_purged_after_unsuccessful_get(): void + { + $path = 'partially-cached-deleted-file'; + + try { + $this->cacheAdapter->checksum($path, new Config); + } catch (UnableToProvideChecksum $e) { + } + + $this->assertCachedItems([ + $path => \null, + ]); + } } diff --git a/tests/GetFileSize_Test.php b/tests/GetFileSize_Test.php index 3651492..e608cb9 100644 --- a/tests/GetFileSize_Test.php +++ b/tests/GetFileSize_Test.php @@ -50,4 +50,21 @@ public static function errorDataProvider(): iterable yield 'Path is directory (cached)' => ['cached-directory']; yield 'Path is directory (non-cached)' => ['non-cached-directory']; } + + /** + * @test + */ + public function cache_is_purged_after_unsuccessful_get(): void + { + $path = 'partially-cached-deleted-file'; + + try { + $this->cacheAdapter->fileSize($path); + } catch (UnableToRetrieveMetadata $e) { + } + + $this->assertCachedItems([ + $path => \null, + ]); + } } diff --git a/tests/GetLastModified_Test.php b/tests/GetLastModified_Test.php index cbdaf10..4506d85 100644 --- a/tests/GetLastModified_Test.php +++ b/tests/GetLastModified_Test.php @@ -47,4 +47,21 @@ public static function errorDataProvider(): iterable yield 'Path is directory (cached)' => ['cached-directory']; yield 'Path is directory (non-cached)' => ['non-cached-directory']; } + + /** + * @test + */ + public function cache_is_purged_after_unsuccessful_get(): void + { + $path = 'partially-cached-deleted-file'; + + try { + $this->cacheAdapter->lastModified($path); + } catch (UnableToRetrieveMetadata $e) { + } + + $this->assertCachedItems([ + $path => \null, + ]); + } } diff --git a/tests/GetMimetype_Test.php b/tests/GetMimetype_Test.php index e200380..7ac1f1e 100644 --- a/tests/GetMimetype_Test.php +++ b/tests/GetMimetype_Test.php @@ -66,4 +66,21 @@ public static function errorDataProvider(): iterable yield 'Path is directory (cached)' => ['cached-directory']; yield 'Path is directory (non-cached)' => ['non-cached-directory']; } + + /** + * @test + */ + public function cache_is_purged_after_unsuccessful_get(): void + { + $path = 'partially-cached-deleted-file'; + + try { + $this->cacheAdapter->mimeType($path); + } catch (UnableToRetrieveMetadata $e) { + } + + $this->assertCachedItems([ + $path => \null, + ]); + } } diff --git a/tests/GetVisibility_Test.php b/tests/GetVisibility_Test.php index cc09502..68e56b1 100644 --- a/tests/GetVisibility_Test.php +++ b/tests/GetVisibility_Test.php @@ -65,4 +65,21 @@ public static function errorDataProvider(): iterable yield 'Path is directory (cached)' => ['cached-directory']; yield 'Path is directory (non-cached)' => ['non-cached-directory']; } + + /** + * @test + */ + public function cache_is_purged_after_unsuccessful_get(): void + { + $path = 'partially-cached-deleted-file'; + + try { + $this->cacheAdapter->visibility($path); + } catch (UnableToRetrieveMetadata $e) { + } + + $this->assertCachedItems([ + $path => \null, + ]); + } }