From 3a1e0bc36717c42d11471d1686c7bc1688f1db12 Mon Sep 17 00:00:00 2001 From: Tech Wolf TW Date: Mon, 24 Mar 2025 10:12:11 +0530 Subject: [PATCH 1/2] [11.x] Add Cache Flush Event --- src/Illuminate/Cache/Events/CacheFlushed.php | 24 +++++++++++ src/Illuminate/Cache/Events/CacheFlushing.php | 24 +++++++++++ src/Illuminate/Cache/Repository.php | 12 +++++- tests/Cache/CacheEventsTest.php | 40 +++++++++++++++++++ tests/Support/SupportFacadesEventTest.php | 15 ++++++- 5 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 src/Illuminate/Cache/Events/CacheFlushed.php create mode 100644 src/Illuminate/Cache/Events/CacheFlushing.php diff --git a/src/Illuminate/Cache/Events/CacheFlushed.php b/src/Illuminate/Cache/Events/CacheFlushed.php new file mode 100644 index 000000000000..fb0275f06e50 --- /dev/null +++ b/src/Illuminate/Cache/Events/CacheFlushed.php @@ -0,0 +1,24 @@ +storeName = $storeName; + } +} diff --git a/src/Illuminate/Cache/Events/CacheFlushing.php b/src/Illuminate/Cache/Events/CacheFlushing.php new file mode 100644 index 000000000000..21054c8b718a --- /dev/null +++ b/src/Illuminate/Cache/Events/CacheFlushing.php @@ -0,0 +1,24 @@ +storeName = $storeName; + } +} diff --git a/src/Illuminate/Cache/Repository.php b/src/Illuminate/Cache/Repository.php index a5f1df9db137..6158320c41c4 100755 --- a/src/Illuminate/Cache/Repository.php +++ b/src/Illuminate/Cache/Repository.php @@ -6,6 +6,8 @@ use BadMethodCallException; use Closure; use DateTimeInterface; +use Illuminate\Cache\Events\CacheFlushed; +use Illuminate\Cache\Events\CacheFlushing; use Illuminate\Cache\Events\CacheHit; use Illuminate\Cache\Events\CacheMissed; use Illuminate\Cache\Events\ForgettingKey; @@ -575,7 +577,15 @@ public function deleteMultiple($keys): bool */ public function clear(): bool { - return $this->store->flush(); + $this->event(new CacheFlushing($this->getName())); + + $result = $this->store->flush(); + + if ($result) { + $this->event(new CacheFlushed($this->getName())); + } + + return $result; } /** diff --git a/tests/Cache/CacheEventsTest.php b/tests/Cache/CacheEventsTest.php index 04036db602fd..243101eb3eec 100755 --- a/tests/Cache/CacheEventsTest.php +++ b/tests/Cache/CacheEventsTest.php @@ -3,6 +3,8 @@ namespace Illuminate\Tests\Cache; use Illuminate\Cache\ArrayStore; +use Illuminate\Cache\Events\CacheFlushed; +use Illuminate\Cache\Events\CacheFlushing; use Illuminate\Cache\Events\CacheHit; use Illuminate\Cache\Events\CacheMissed; use Illuminate\Cache\Events\ForgettingKey; @@ -238,6 +240,44 @@ protected function assertEventMatches($eventClass, $properties = []) }); } + public function testFlushTriggersEvents() + { + $dispatcher = $this->getDispatcher(); + $repository = $this->getRepository($dispatcher); + + $dispatcher->shouldReceive('dispatch')->once()->with( + $this->assertEventMatches(CacheFlushing::class, [ + 'storeName' => 'array', + ]) + ); + + $dispatcher->shouldReceive('dispatch')->once()->with( + $this->assertEventMatches(CacheFlushed::class, [ + 'storeName' => 'array', + ]) + ); + $this->assertTrue($repository->clear()); + } + + public function testFlushFailureDoesNotDispatchEvent() + { + $dispatcher = $this->getDispatcher(); + + // Create a store that fails to flush + $failingStore = m::mock(Store::class); + $failingStore->shouldReceive('flush')->andReturn(false); + + $repository = new Repository($failingStore, ['store' => 'array']); + $repository->setEventDispatcher($dispatcher); + + $dispatcher->shouldReceive('dispatch')->once()->with( + $this->assertEventMatches(CacheFlushing::class, [ + 'storeName' => 'array', + ]) + ); + $this->assertFalse($repository->clear()); + } + protected function getDispatcher() { return m::mock(Dispatcher::class); diff --git a/tests/Support/SupportFacadesEventTest.php b/tests/Support/SupportFacadesEventTest.php index 3a438ff08ef3..e9aeb38bfeb4 100644 --- a/tests/Support/SupportFacadesEventTest.php +++ b/tests/Support/SupportFacadesEventTest.php @@ -3,6 +3,8 @@ namespace Illuminate\Tests\Support; use Illuminate\Cache\CacheManager; +use Illuminate\Cache\Events\CacheFlushed; +use Illuminate\Cache\Events\CacheFlushing; use Illuminate\Cache\Events\CacheMissed; use Illuminate\Cache\Events\RetrievingKey; use Illuminate\Config\Repository as ConfigRepository; @@ -87,6 +89,17 @@ public function testFakeSwapsDispatchersInResolvedCacheRepositories() Event::assertDispatched(CacheMissed::class); } + public function testCacheFlushDispatchesEvent() + { + $arrayRepository = Cache::store('array'); + Event::fake(); + + $arrayRepository->clear(); + + Event::assertDispatched(CacheFlushing::class); + Event::assertDispatched(CacheFlushed::class); + } + protected function getCacheConfig() { return [ @@ -103,7 +116,7 @@ protected function getCacheConfig() class FakeForStub { - public function dispatch() +public function dispatch() { Event::dispatch(EventStub::class); } From 236d941838360bc3fb6c9413412912965e3587a7 Mon Sep 17 00:00:00 2001 From: Tech Wolf TW Date: Mon, 24 Mar 2025 10:14:33 +0530 Subject: [PATCH 2/2] StyleCLI Issue Resolved --- tests/Support/SupportFacadesEventTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/SupportFacadesEventTest.php b/tests/Support/SupportFacadesEventTest.php index e9aeb38bfeb4..928ce440e3f0 100644 --- a/tests/Support/SupportFacadesEventTest.php +++ b/tests/Support/SupportFacadesEventTest.php @@ -116,7 +116,7 @@ protected function getCacheConfig() class FakeForStub { -public function dispatch() + public function dispatch() { Event::dispatch(EventStub::class); }