From 3ae8ba4be114be6aea3f6338b102567fac88047e Mon Sep 17 00:00:00 2001 From: Colin DeCarlo Date: Mon, 13 Jan 2020 01:09:48 -0500 Subject: [PATCH 1/5] Extract setting event dispatcher to a method --- src/Illuminate/Cache/CacheManager.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Cache/CacheManager.php b/src/Illuminate/Cache/CacheManager.php index 240b78e31f29..4c1f0fc52d04 100755 --- a/src/Illuminate/Cache/CacheManager.php +++ b/src/Illuminate/Cache/CacheManager.php @@ -258,15 +258,23 @@ protected function createDynamodbDriver(array $config) */ public function repository(Store $store) { - $repository = new Repository($store); + return tap(new Repository($store), function ($repository) { + $this->setEventDispatcher($repository); + }); + } - if ($this->app->bound(DispatcherContract::class)) { - $repository->setEventDispatcher( - $this->app[DispatcherContract::class] - ); + /** + * @param Repository $repository + */ + protected function setEventDispatcher(Repository $repository): void + { + if (!$this->app->bound(DispatcherContract::class)) { + return; } - return $repository; + $repository->setEventDispatcher( + $this->app[DispatcherContract::class] + ); } /** From d5c46e5f7b07fb6524a16f88c809ca9f2f7789f3 Mon Sep 17 00:00:00 2001 From: Colin DeCarlo Date: Mon, 13 Jan 2020 01:10:42 -0500 Subject: [PATCH 2/5] Refresh the cache event dispatcher when faked --- src/Illuminate/Cache/CacheManager.php | 9 ++++++ src/Illuminate/Support/Facades/Event.php | 1 + tests/Support/SupportFacadesEventTest.php | 35 +++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/src/Illuminate/Cache/CacheManager.php b/src/Illuminate/Cache/CacheManager.php index 4c1f0fc52d04..b12877250da7 100755 --- a/src/Illuminate/Cache/CacheManager.php +++ b/src/Illuminate/Cache/CacheManager.php @@ -277,6 +277,15 @@ protected function setEventDispatcher(Repository $repository): void ); } + /** + * Refreshes the event dispatcher of all resolved repositories + * with the currently bound event dispatcher implementation. + */ + public function refreshEventDispatcher() + { + array_map([$this, 'setEventDispatcher'], $this->stores); + } + /** * Get the cache prefix. * diff --git a/src/Illuminate/Support/Facades/Event.php b/src/Illuminate/Support/Facades/Event.php index dafaa3c6f373..1f6525e2c8b1 100755 --- a/src/Illuminate/Support/Facades/Event.php +++ b/src/Illuminate/Support/Facades/Event.php @@ -38,6 +38,7 @@ public static function fake($eventsToFake = []) static::swap($fake = new EventFake(static::getFacadeRoot(), $eventsToFake)); Model::setEventDispatcher($fake); + Cache::refreshEventDispatcher(); return $fake; } diff --git a/tests/Support/SupportFacadesEventTest.php b/tests/Support/SupportFacadesEventTest.php index fe646a6900e6..b8470ee3d90f 100644 --- a/tests/Support/SupportFacadesEventTest.php +++ b/tests/Support/SupportFacadesEventTest.php @@ -2,9 +2,14 @@ namespace Illuminate\Tests\Support; +use Illuminate\Cache\CacheManager; +use Illuminate\Cache\Events\CacheMissed; +use Illuminate\Config\Repository as ConfigRepository; use Illuminate\Container\Container; +use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; use Illuminate\Database\Eloquent\Model; use Illuminate\Events\Dispatcher; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Facade; use Illuminate\Support\Testing\Fakes\EventFake; @@ -23,6 +28,9 @@ protected function setUp(): void $container = new Container; $container->instance('events', $this->events); + $container->alias('events', DispatcherContract::class); + $container->instance('cache', new CacheManager($container)); + $container->instance('config', new ConfigRepository($this->getCacheConfig())); Facade::setFacadeApplication($container); } @@ -57,6 +65,33 @@ public function testFakeForSwapsDispatchers() $this->assertSame($this->events, Event::getFacadeRoot()); $this->assertSame($this->events, Model::getEventDispatcher()); } + + public function testFakeSwapsDispatchersInResolvedCacheRepositories() + { + $arrayRepository = Cache::store('array'); + + $this->events->shouldReceive('dispatch')->once(); + $arrayRepository->get('foo'); + + Event::fake(); + + $arrayRepository->get('bar'); + + Event::assertDispatched(CacheMissed::class); + } + + protected function getCacheConfig() + { + return [ + 'cache' => [ + 'stores' => [ + 'array' => [ + 'driver' => 'array' + ] + ] + ] + ]; + } } class FakeForStub From cfc72b7af9e8d6044645ea4d52de65564fd3d612 Mon Sep 17 00:00:00 2001 From: Colin DeCarlo Date: Mon, 13 Jan 2020 01:24:18 -0500 Subject: [PATCH 3/5] Refresh cache event dispatcher after fakeFor --- src/Illuminate/Cache/Repository.php | 10 ++++++++++ src/Illuminate/Support/Facades/Event.php | 1 + tests/Support/SupportFacadesEventTest.php | 6 +++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Cache/Repository.php b/src/Illuminate/Cache/Repository.php index 970d285d24a5..17b9a112b8a4 100755 --- a/src/Illuminate/Cache/Repository.php +++ b/src/Illuminate/Cache/Repository.php @@ -558,6 +558,16 @@ public function setEventDispatcher(Dispatcher $events) $this->events = $events; } + /** + * Get the event dispatcher instance. + * + * @return \Illuminate\Contracts\Events\Dispatcher $events + */ + public function getEventDispatcher() + { + return $this->events; + } + /** * Determine if a cached value exists. * diff --git a/src/Illuminate/Support/Facades/Event.php b/src/Illuminate/Support/Facades/Event.php index 1f6525e2c8b1..a37d0de0e2fc 100755 --- a/src/Illuminate/Support/Facades/Event.php +++ b/src/Illuminate/Support/Facades/Event.php @@ -60,6 +60,7 @@ public static function fakeFor(callable $callable, array $eventsToFake = []) static::swap($originalDispatcher); Model::setEventDispatcher($originalDispatcher); + Cache::refreshEventDispatcher(); }); } diff --git a/tests/Support/SupportFacadesEventTest.php b/tests/Support/SupportFacadesEventTest.php index b8470ee3d90f..7184b2d954ee 100644 --- a/tests/Support/SupportFacadesEventTest.php +++ b/tests/Support/SupportFacadesEventTest.php @@ -57,13 +57,17 @@ public function testFakeFor() public function testFakeForSwapsDispatchers() { - Event::fakeFor(function () { + $arrayRepository = Cache::store('array'); + + Event::fakeFor(function () use ($arrayRepository) { $this->assertInstanceOf(EventFake::class, Event::getFacadeRoot()); $this->assertInstanceOf(EventFake::class, Model::getEventDispatcher()); + $this->assertInstanceOf(EventFake::class, $arrayRepository->getEventDispatcher()); }); $this->assertSame($this->events, Event::getFacadeRoot()); $this->assertSame($this->events, Model::getEventDispatcher()); + $this->assertSame($this->events, $arrayRepository->getEventDispatcher()); } public function testFakeSwapsDispatchersInResolvedCacheRepositories() From 0ae84deec579d23b79cf01b8cd57d9ff41735cbc Mon Sep 17 00:00:00 2001 From: Colin DeCarlo Date: Mon, 13 Jan 2020 20:23:51 -0500 Subject: [PATCH 4/5] Rename setEventDispatcher to addEventDispatcher --- src/Illuminate/Cache/CacheManager.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Cache/CacheManager.php b/src/Illuminate/Cache/CacheManager.php index b12877250da7..c274a6a6064b 100755 --- a/src/Illuminate/Cache/CacheManager.php +++ b/src/Illuminate/Cache/CacheManager.php @@ -259,14 +259,14 @@ protected function createDynamodbDriver(array $config) public function repository(Store $store) { return tap(new Repository($store), function ($repository) { - $this->setEventDispatcher($repository); + $this->addEventDispatcher($repository); }); } /** * @param Repository $repository */ - protected function setEventDispatcher(Repository $repository): void + protected function addEventDispatcher(Repository $repository) { if (!$this->app->bound(DispatcherContract::class)) { return; @@ -283,7 +283,7 @@ protected function setEventDispatcher(Repository $repository): void */ public function refreshEventDispatcher() { - array_map([$this, 'setEventDispatcher'], $this->stores); + array_map([$this, 'addEventDispatcher'], $this->stores); } /** From 381ee1f4f1d04347cc19fa1c966bfa8814de1d5d Mon Sep 17 00:00:00 2001 From: Colin DeCarlo Date: Mon, 13 Jan 2020 20:56:18 -0500 Subject: [PATCH 5/5] Sacrificial commit to the lint gods --- src/Illuminate/Cache/CacheManager.php | 2 +- tests/Support/SupportFacadesEventTest.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Cache/CacheManager.php b/src/Illuminate/Cache/CacheManager.php index c274a6a6064b..67276fad3a0f 100755 --- a/src/Illuminate/Cache/CacheManager.php +++ b/src/Illuminate/Cache/CacheManager.php @@ -268,7 +268,7 @@ public function repository(Store $store) */ protected function addEventDispatcher(Repository $repository) { - if (!$this->app->bound(DispatcherContract::class)) { + if (! $this->app->bound(DispatcherContract::class)) { return; } diff --git a/tests/Support/SupportFacadesEventTest.php b/tests/Support/SupportFacadesEventTest.php index 7184b2d954ee..c7acd9fb53fc 100644 --- a/tests/Support/SupportFacadesEventTest.php +++ b/tests/Support/SupportFacadesEventTest.php @@ -90,10 +90,10 @@ protected function getCacheConfig() 'cache' => [ 'stores' => [ 'array' => [ - 'driver' => 'array' - ] - ] - ] + 'driver' => 'array', + ], + ], + ], ]; } }