Skip to content

Commit

Permalink
14-purge-cache-on-unsuccessful-operations
Browse files Browse the repository at this point in the history
  • Loading branch information
jgivoni committed Jan 30, 2024
1 parent e29afec commit db67895
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
13 changes: 12 additions & 1 deletion src/CacheAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions tests/Copy_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
]);
}
}
}
28 changes: 14 additions & 14 deletions tests/Read_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}
}

/**
Expand All @@ -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,
]);
}
}
}

0 comments on commit db67895

Please sign in to comment.