From db678957f9ca562c901c2a1faa2a0d0b5583206c Mon Sep 17 00:00:00 2001 From: Jakob Givoni Date: Tue, 30 Jan 2024 16:29:33 +0100 Subject: [PATCH] 14-purge-cache-on-unsuccessful-operations --- src/CacheAdapter.php | 13 ++++++++++++- tests/Copy_Test.php | 18 ++++++++++++++++++ tests/Read_Test.php | 28 ++++++++++++++-------------- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/CacheAdapter.php b/src/CacheAdapter.php index 26149e4..1e4948a 100644 --- a/src/CacheAdapter.php +++ b/src/CacheAdapter.php @@ -10,6 +10,7 @@ use League\Flysystem\FileAttributes; use League\Flysystem\FilesystemAdapter; use League\Flysystem\StorageAttributes; +use League\Flysystem\UnableToCopyFile; use League\Flysystem\UnableToProvideChecksum; use League\Flysystem\UnableToReadFile; use League\Flysystem\UnableToRetrieveMetadata; @@ -442,7 +443,17 @@ public function move(string $source, string $destination, Config $config): void */ public function copy(string $source, string $destination, Config $config): void { - $this->adapter->copy($source, $destination, $config); + try { + $this->adapter->copy($source, $destination, $config); + } catch (UnableToCopyFile $e) { + $item = $this->getCacheItem($source); + + if ($item->isHit()) { + $this->deleteCacheItem($item); + } + + throw $e; + } $itemSource = $this->getCacheItem($source); $itemDestination = $this->getCacheItem($destination); diff --git a/tests/Copy_Test.php b/tests/Copy_Test.php index 2732703..1c7db58 100644 --- a/tests/Copy_Test.php +++ b/tests/Copy_Test.php @@ -4,6 +4,8 @@ use League\Flysystem\Config; use League\Flysystem\FileAttributes; +use League\Flysystem\UnableToCopyFile; +use League\Flysystem\UnableToReadFile; use League\Flysystem\Visibility; class Copy_Test extends CacheTestCase @@ -33,4 +35,20 @@ public static function dataProvider(): iterable yield 'cache item is copied' => ['fully-cached-file', new FileAttributes('fully-cached-file', 10, Visibility::PUBLIC), new FileAttributes('destination', 10, Visibility::PUBLIC)]; yield 'cache item is created' => ['non-cached-file', \null, new FileAttributes('destination')]; } + + /** + * @test + */ + public function cache_is_purged_after_unsuccessful_copy(): void + { + $path = 'deleted-cached-file'; + + try { + $this->cacheAdapter->copy($path, 'destination', new Config); + } catch (UnableToCopyFile $e) { + $this->assertCachedItems([ + $path => \null, + ]); + } + } } diff --git a/tests/Read_Test.php b/tests/Read_Test.php index c81247c..11c2640 100644 --- a/tests/Read_Test.php +++ b/tests/Read_Test.php @@ -52,13 +52,13 @@ public function cache_is_purged_after_unsuccessful_read(): void { $path = 'deleted-cached-file'; - $this->expectException(UnableToReadFile::class); - - $this->cacheAdapter->read($path); - - $this->assertCachedItems([ - $path => \null, - ]); + try { + $this->cacheAdapter->read($path); + } catch (UnableToReadFile $e) { + $this->assertCachedItems([ + $path => \null, + ]); + } } /** @@ -68,12 +68,12 @@ public function cache_is_purged_after_unsuccessful_readStream(): void { $path = 'deleted-cached-file'; - $this->expectException(UnableToReadFile::class); - - $this->cacheAdapter->readStream($path); - - $this->assertCachedItems([ - $path => \null, - ]); + try { + $this->cacheAdapter->readStream($path); + } catch (UnableToReadFile $e) { + $this->assertCachedItems([ + $path => \null, + ]); + } } }