Skip to content

Commit

Permalink
[11.x] Test for forgetting non-flexible keys for file driver (#53018)
Browse files Browse the repository at this point in the history
* Test for forgetting non-flexible keys for file driver

* Check file existence before forgetting

* only forget when parent key is forgotten

* lint
  • Loading branch information
timacdonald authored Oct 7, 2024
1 parent 65b6a40 commit ffee111
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Illuminate/Cache/FileStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,11 @@ public function restoreLock($name, $owner)
public function forget($key)
{
if ($this->files->exists($file = $this->path($key))) {
$this->files->delete($this->path("illuminate:cache:flexible:created:{$key}"));

return $this->files->delete($file);
return tap($this->files->delete($file), function ($forgotten) use ($key) {
if ($forgotten && $this->files->exists($file = $this->path("illuminate:cache:flexible:created:{$key}"))) {
$this->files->delete($file);
}
});
}

return false;
Expand Down
44 changes: 44 additions & 0 deletions tests/Cache/CacheFileStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Mockery as m;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -330,6 +331,49 @@ public function testFlushIgnoreNonExistingDirectory()
$this->assertFalse($result, 'Flush should not clean directory');
}

public function testItHandlesForgettingNonFlexibleKeys()
{
$store = new FileStore(new Filesystem, __DIR__);

$key = Str::random();
$path = $store->path($key);
$flexiblePath = "illuminate:cache:flexible:created:{$key}";

$store->put($key, 'value', 5);

$this->assertFileExists($path);
$this->assertFileDoesNotExist($flexiblePath);

$store->forget($key);

$this->assertFileDoesNotExist($path);
$this->assertFileDoesNotExist($flexiblePath);
}

public function itOnlyForgetsFlexibleKeysIfParentIsForgotten()
{
$store = new FileStore(new Filesystem, __DIR__);

$key = Str::random();
$path = $store->path($key);
$flexiblePath = "illuminate:cache:flexible:created:{$key}";

touch($flexiblePath);

$this->assertFileDoesNotExist($path);
$this->assertFileExists($flexiblePath);

$store->forget($key);

$this->assertFileDoesNotExist($path);
$this->assertFileExists($flexiblePath);

$store->put($key, 'value', 5);

$this->assertFileDoesNotExist($path);
$this->assertFileDoesNotExist($flexiblePath);
}

protected function mockFilesystem()
{
return $this->createMock(Filesystem::class);
Expand Down

0 comments on commit ffee111

Please sign in to comment.