From b3257863c7e92e6ce41c8e572840216df1511c9b Mon Sep 17 00:00:00 2001 From: Randall Wilk <22842525+rawilk@users.noreply.github.com> Date: Thu, 28 Sep 2023 13:13:50 -0500 Subject: [PATCH] Dispatch Events (#33) --- src/Events/SettingWasDeleted.php | 24 ++++++++++++++ src/Events/SettingWasStored.php | 25 +++++++++++++++ src/Events/SettingsFlushed.php | 23 ++++++++++++++ src/Settings.php | 28 ++++++++++++++++- tests/ArchTest.php | 13 ++++++++ tests/Feature/SettingsTest.php | 54 ++++++++++++++++++++++++++++++++ 6 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 src/Events/SettingWasDeleted.php create mode 100644 src/Events/SettingWasStored.php create mode 100644 src/Events/SettingsFlushed.php diff --git a/src/Events/SettingWasDeleted.php b/src/Events/SettingWasDeleted.php new file mode 100644 index 0000000..d622771 --- /dev/null +++ b/src/Events/SettingWasDeleted.php @@ -0,0 +1,24 @@ +teams ? $this->teamId : false, ); + SettingWasDeleted::dispatch( + $key, + $generatedKey, + $this->getCacheKey($generatedKey), + $this->teams ? $this->teamId : false, + $this->context, + ); + if ($this->temporarilyDisableCache || $this->cacheIsEnabled()) { $this->cache->forget($this->getCacheKey($generatedKey)); } @@ -218,7 +229,7 @@ public function has($key): bool return $has; } - public function set(string $key, $value = null) + public function set(string $key, $value = null): mixed { $key = $this->normalizeKey($key); @@ -239,6 +250,15 @@ public function set(string $key, $value = null) teamId: $this->teams ? $this->teamId : false, ); + SettingWasStored::dispatch( + $key, + $generatedKey, + $this->getCacheKey($generatedKey), + $value, + $this->teams ? $this->teamId : false, + $this->context, + ); + if ($this->temporarilyDisableCache || $this->cacheIsEnabled()) { $this->cache->forget($this->getCacheKey($generatedKey)); } @@ -272,6 +292,12 @@ public function flush($keys = null): mixed keys: $keys, ); + SettingsFlushed::dispatch( + $keys, + $this->teams ? $this->teamId : false, + $this->context, + ); + // Flush the cache for all deleted keys. // Note: Only works when a subset of keys is specified. if ($keys instanceof Collection && ($this->temporarilyDisableCache || $this->cacheIsEnabled())) { diff --git a/tests/ArchTest.php b/tests/ArchTest.php index 2cffd83..38c0499 100644 --- a/tests/ArchTest.php +++ b/tests/ArchTest.php @@ -3,6 +3,8 @@ declare(strict_types=1); use Illuminate\Database\Eloquent\Model; +use Illuminate\Foundation\Events\Dispatchable; +use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Facade; use Rawilk\Settings\Contracts\ContextSerializer; use Rawilk\Settings\Contracts\Driver; @@ -54,3 +56,14 @@ ->toImplement(KeyGenerator::class) ->toExtendNothing() ->toHaveSuffix('Generator'); + +test('events are configured correctly') + ->expect('Rawilk\Settings\Events') + ->toBeClasses() + ->classes() + ->toExtendNothing() + ->toBeFinal() + ->toUse([ + Dispatchable::class, + SerializesModels::class, + ]); diff --git a/tests/Feature/SettingsTest.php b/tests/Feature/SettingsTest.php index 35bdd48..4f7c9dd 100644 --- a/tests/Feature/SettingsTest.php +++ b/tests/Feature/SettingsTest.php @@ -3,8 +3,12 @@ declare(strict_types=1); use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Event; use Rawilk\Settings\Contracts\Setting; use Rawilk\Settings\Drivers\EloquentDriver; +use Rawilk\Settings\Events\SettingsFlushed; +use Rawilk\Settings\Events\SettingWasDeleted; +use Rawilk\Settings\Events\SettingWasStored; use Rawilk\Settings\Exceptions\InvalidKeyGenerator; use Rawilk\Settings\Facades\Settings as SettingsFacade; use Rawilk\Settings\Support\Context; @@ -431,6 +435,56 @@ $this->assertDatabaseCount('settings', 1); }); +it('dispatches an event when settings are flushed', function () { + $settings = settings(); + (fn () => $this->keyGenerator = (new ReadableKeyGenerator)->setContextSerializer(new DotNotationContextSerializer))->call($settings); + + Event::fake(); + + $settings->set('one', 'value 1'); + $settings->set('two', 'value 2'); + + $settings->flush(); + + Event::assertDispatched(SettingsFlushed::class); +}); + +it('dispatches an event when a setting is deleted', function () { + Event::fake(); + + SettingsFacade::set('foo', 'bar'); + SettingsFacade::forget('foo'); + + Event::assertDispatched(function (SettingWasDeleted $event) { + return $event->key === 'foo' + && $event->teamId === false + && is_null($event->context); + }); +}); + +it('dispatches an event when a setting is saved', function () { + Event::fake(); + + SettingsFacade::set('foo', 'bar'); + + Event::assertDispatched(function (SettingWasStored $event) { + return $event->key === 'foo' + && $event->value === 'bar'; + }); +}); + +it('does not dispatch the stored event if the setting value has not changed', function () { + Event::fake(); + + // This only works when caching is enabled. + SettingsFacade::enableCache(); + + SettingsFacade::set('foo', 'bar'); + SettingsFacade::set('foo', 'bar'); + + Event::assertDispatchedTimes(SettingWasStored::class, 1); +}); + // Helpers... function assertQueryCount(int $expected): void