From 050ddbfae66615ba74b8a1c880a9a7e5dce50db5 Mon Sep 17 00:00:00 2001 From: Tech Wolf TW Date: Fri, 21 Mar 2025 16:26:46 +0530 Subject: [PATCH 1/4] [12.x] Fix: Added CacheFlushed Event --- src/Illuminate/Cache/Events/CacheEvent.php | 6 +-- src/Illuminate/Cache/Events/CacheFlushed.php | 8 ++++ src/Illuminate/Cache/Repository.php | 8 +++- tests/Cache/CacheEventsTest.php | 40 ++++++++++++++++++++ tests/Support/SupportFacadesEventTest.php | 11 ++++++ 5 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 src/Illuminate/Cache/Events/CacheFlushed.php diff --git a/src/Illuminate/Cache/Events/CacheEvent.php b/src/Illuminate/Cache/Events/CacheEvent.php index b6bc49b15c96..8980a924e6d0 100644 --- a/src/Illuminate/Cache/Events/CacheEvent.php +++ b/src/Illuminate/Cache/Events/CacheEvent.php @@ -14,7 +14,7 @@ abstract class CacheEvent /** * The key of the event. * - * @var string + * @var string|null */ public $key; @@ -29,11 +29,11 @@ abstract class CacheEvent * Create a new event instance. * * @param string|null $storeName - * @param string $key + * @param string|null $key * @param array $tags * @return void */ - public function __construct($storeName, $key, array $tags = []) + public function __construct($storeName, $key = null, array $tags = []) { $this->storeName = $storeName; $this->key = $key; diff --git a/src/Illuminate/Cache/Events/CacheFlushed.php b/src/Illuminate/Cache/Events/CacheFlushed.php new file mode 100644 index 000000000000..5ef4dfdfa5c2 --- /dev/null +++ b/src/Illuminate/Cache/Events/CacheFlushed.php @@ -0,0 +1,8 @@ +store->flush(); + $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..90b68ec635aa 100755 --- a/tests/Cache/CacheEventsTest.php +++ b/tests/Cache/CacheEventsTest.php @@ -3,6 +3,7 @@ namespace Illuminate\Tests\Cache; use Illuminate\Cache\ArrayStore; +use Illuminate\Cache\Events\CacheFlushed; use Illuminate\Cache\Events\CacheHit; use Illuminate\Cache\Events\CacheMissed; use Illuminate\Cache\Events\ForgettingKey; @@ -221,6 +222,45 @@ public function testForgetDoesTriggerFailedEventOnFailure() $this->assertFalse($repository->forget('baz')); } + public function testFlushTriggersEvents() + { + $dispatcher = $this->getDispatcher(); + $repository = $this->getRepository($dispatcher); + + $dispatcher->shouldReceive('dispatch')->once()->with( + $this->assertEventMatches(CacheFlushed::class, [ + 'storeName' => 'array' + ]) + ); + $this->assertTrue($repository->clear()); + + $taggedRepository = $repository->tags('taylor'); + $dispatcher->shouldReceive('dispatch')->once()->with( + $this->assertEventMatches(CacheFlushed::class, [ + 'storeName' => 'array', + 'tags' => ['taylor'] + ]) + ); + $this->assertTrue($taggedRepository->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); + $repository->setEventDispatcher($dispatcher); + + // Ensure no event is dispatched on failure + $dispatcher->shouldNotReceive('dispatch'); + $this->assertFalse($repository->clear()); + } + + protected function assertEventMatches($eventClass, $properties = []) { return m::on(function ($event) use ($eventClass, $properties) { diff --git a/tests/Support/SupportFacadesEventTest.php b/tests/Support/SupportFacadesEventTest.php index 3a438ff08ef3..8aab3743fdd5 100644 --- a/tests/Support/SupportFacadesEventTest.php +++ b/tests/Support/SupportFacadesEventTest.php @@ -3,6 +3,7 @@ namespace Illuminate\Tests\Support; use Illuminate\Cache\CacheManager; +use Illuminate\Cache\Events\CacheFlushed; use Illuminate\Cache\Events\CacheMissed; use Illuminate\Cache\Events\RetrievingKey; use Illuminate\Config\Repository as ConfigRepository; @@ -87,6 +88,16 @@ public function testFakeSwapsDispatchersInResolvedCacheRepositories() Event::assertDispatched(CacheMissed::class); } + public function testCacheFlushDispatchesEvent() + { + $arrayRepository = Cache::store('array'); + Event::fake(); + + $arrayRepository->clear(); + + Event::assertDispatched(CacheFlushed::class); + } + protected function getCacheConfig() { return [ From 4172a0d5db90dd86f76c744f92874dbdd627cea3 Mon Sep 17 00:00:00 2001 From: Tech Wolf TW Date: Fri, 21 Mar 2025 16:32:35 +0530 Subject: [PATCH 2/4] [12.x] StyleCLI Resolved --- src/Illuminate/Cache/Repository.php | 1 + tests/Cache/CacheEventsTest.php | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Cache/Repository.php b/src/Illuminate/Cache/Repository.php index bdbc8b02920b..8a1287dd83f2 100755 --- a/src/Illuminate/Cache/Repository.php +++ b/src/Illuminate/Cache/Repository.php @@ -581,6 +581,7 @@ public function clear(): bool if ($result) { $this->event(new CacheFlushed($this->getName())); } + return $result; } diff --git a/tests/Cache/CacheEventsTest.php b/tests/Cache/CacheEventsTest.php index 90b68ec635aa..4468cc1f0195 100755 --- a/tests/Cache/CacheEventsTest.php +++ b/tests/Cache/CacheEventsTest.php @@ -229,7 +229,7 @@ public function testFlushTriggersEvents() $dispatcher->shouldReceive('dispatch')->once()->with( $this->assertEventMatches(CacheFlushed::class, [ - 'storeName' => 'array' + 'storeName' => 'array', ]) ); $this->assertTrue($repository->clear()); @@ -238,7 +238,7 @@ public function testFlushTriggersEvents() $dispatcher->shouldReceive('dispatch')->once()->with( $this->assertEventMatches(CacheFlushed::class, [ 'storeName' => 'array', - 'tags' => ['taylor'] + 'tags' => ['taylor'], ]) ); $this->assertTrue($taggedRepository->clear()); From c8554fe5948aa143a35e49ee2009f1bf07cecbec Mon Sep 17 00:00:00 2001 From: Tech Wolf TW Date: Fri, 21 Mar 2025 16:33:42 +0530 Subject: [PATCH 3/4] [12.x] StyleCLI Resolved --- tests/Cache/CacheEventsTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Cache/CacheEventsTest.php b/tests/Cache/CacheEventsTest.php index 4468cc1f0195..07cad58d66a9 100755 --- a/tests/Cache/CacheEventsTest.php +++ b/tests/Cache/CacheEventsTest.php @@ -260,7 +260,6 @@ public function testFlushFailureDoesNotDispatchEvent() $this->assertFalse($repository->clear()); } - protected function assertEventMatches($eventClass, $properties = []) { return m::on(function ($event) use ($eventClass, $properties) { From e128b06f92b0d8663f3d570a26196165115aba28 Mon Sep 17 00:00:00 2001 From: Tech Wolf TW Date: Fri, 21 Mar 2025 17:04:18 +0530 Subject: [PATCH 4/4] [12.x] CacheFlushing Event added --- src/Illuminate/Cache/Events/CacheEvent.php | 6 +++--- src/Illuminate/Cache/Events/CacheFlushing.php | 8 ++++++++ src/Illuminate/Cache/Repository.php | 5 ++++- tests/Cache/CacheEventsTest.php | 17 +++++++++-------- tests/Support/SupportFacadesEventTest.php | 2 ++ 5 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 src/Illuminate/Cache/Events/CacheFlushing.php diff --git a/src/Illuminate/Cache/Events/CacheEvent.php b/src/Illuminate/Cache/Events/CacheEvent.php index 8980a924e6d0..b6bc49b15c96 100644 --- a/src/Illuminate/Cache/Events/CacheEvent.php +++ b/src/Illuminate/Cache/Events/CacheEvent.php @@ -14,7 +14,7 @@ abstract class CacheEvent /** * The key of the event. * - * @var string|null + * @var string */ public $key; @@ -29,11 +29,11 @@ abstract class CacheEvent * Create a new event instance. * * @param string|null $storeName - * @param string|null $key + * @param string $key * @param array $tags * @return void */ - public function __construct($storeName, $key = null, array $tags = []) + public function __construct($storeName, $key, array $tags = []) { $this->storeName = $storeName; $this->key = $key; diff --git a/src/Illuminate/Cache/Events/CacheFlushing.php b/src/Illuminate/Cache/Events/CacheFlushing.php new file mode 100644 index 000000000000..a7c2e019824c --- /dev/null +++ b/src/Illuminate/Cache/Events/CacheFlushing.php @@ -0,0 +1,8 @@ +event(new CacheFlushing($this->getName(), '')); + $result = $this->store->flush(); if ($result) { - $this->event(new CacheFlushed($this->getName())); + $this->event(new CacheFlushed($this->getName(), '')); } return $result; diff --git a/tests/Cache/CacheEventsTest.php b/tests/Cache/CacheEventsTest.php index 07cad58d66a9..77a9173506eb 100755 --- a/tests/Cache/CacheEventsTest.php +++ b/tests/Cache/CacheEventsTest.php @@ -4,6 +4,7 @@ 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; @@ -228,20 +229,17 @@ public function testFlushTriggersEvents() $repository = $this->getRepository($dispatcher); $dispatcher->shouldReceive('dispatch')->once()->with( - $this->assertEventMatches(CacheFlushed::class, [ + $this->assertEventMatches(CacheFlushing::class, [ 'storeName' => 'array', ]) ); - $this->assertTrue($repository->clear()); - $taggedRepository = $repository->tags('taylor'); $dispatcher->shouldReceive('dispatch')->once()->with( $this->assertEventMatches(CacheFlushed::class, [ 'storeName' => 'array', - 'tags' => ['taylor'], ]) ); - $this->assertTrue($taggedRepository->clear()); + $this->assertTrue($repository->clear()); } public function testFlushFailureDoesNotDispatchEvent() @@ -252,11 +250,14 @@ public function testFlushFailureDoesNotDispatchEvent() $failingStore = m::mock(Store::class); $failingStore->shouldReceive('flush')->andReturn(false); - $repository = new Repository($failingStore); + $repository = new Repository($failingStore, ['store' => 'array']); $repository->setEventDispatcher($dispatcher); - // Ensure no event is dispatched on failure - $dispatcher->shouldNotReceive('dispatch'); + $dispatcher->shouldReceive('dispatch')->once()->with( + $this->assertEventMatches(CacheFlushing::class, [ + 'storeName' => 'array', + ]) + ); $this->assertFalse($repository->clear()); } diff --git a/tests/Support/SupportFacadesEventTest.php b/tests/Support/SupportFacadesEventTest.php index 8aab3743fdd5..928ce440e3f0 100644 --- a/tests/Support/SupportFacadesEventTest.php +++ b/tests/Support/SupportFacadesEventTest.php @@ -4,6 +4,7 @@ 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; @@ -95,6 +96,7 @@ public function testCacheFlushDispatchesEvent() $arrayRepository->clear(); + Event::assertDispatched(CacheFlushing::class); Event::assertDispatched(CacheFlushed::class); }