From 4c30f9c34cc218619008250d3063f10a665d54eb Mon Sep 17 00:00:00 2001 From: Luke <58695005+lukemorcom@users.noreply.github.com> Date: Mon, 2 Dec 2024 15:41:05 +0000 Subject: [PATCH] Add support for notification class overrides (#1518) * Add support for notification class overrides * use container * add test --------- Co-authored-by: Luke Morcom Co-authored-by: Taylor Otwell --- .../LongWaitDetectedNotification.php | 8 ++++ src/Events/LongWaitDetected.php | 11 +++-- src/Notifications/LongWaitDetected.php | 3 +- src/ServiceBindings.php | 3 ++ tests/Feature/NotificationOverridesTest.php | 48 +++++++++++++++++++ 5 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 src/Contracts/LongWaitDetectedNotification.php create mode 100644 tests/Feature/NotificationOverridesTest.php diff --git a/src/Contracts/LongWaitDetectedNotification.php b/src/Contracts/LongWaitDetectedNotification.php new file mode 100644 index 00000000..48efa656 --- /dev/null +++ b/src/Contracts/LongWaitDetectedNotification.php @@ -0,0 +1,8 @@ +connection, $this->queue, $this->seconds - ); + return Container::getInstance()->make(LongWaitDetectedNotification::class, [ + 'connection' => $this->connection, + 'queue' => $this->queue, + 'seconds' => $this->seconds, + ]); } } diff --git a/src/Notifications/LongWaitDetected.php b/src/Notifications/LongWaitDetected.php index 5d974c59..f724f777 100644 --- a/src/Notifications/LongWaitDetected.php +++ b/src/Notifications/LongWaitDetected.php @@ -10,9 +10,10 @@ use Illuminate\Notifications\Slack\BlockKit\Blocks\SectionBlock; use Illuminate\Notifications\Slack\SlackMessage as ChannelIdSlackMessage; use Illuminate\Support\Str; +use Laravel\Horizon\Contracts\LongWaitDetectedNotification; use Laravel\Horizon\Horizon; -class LongWaitDetected extends Notification +class LongWaitDetected extends Notification implements LongWaitDetectedNotification { use Queueable; diff --git a/src/ServiceBindings.php b/src/ServiceBindings.php index cff53682..054d08b0 100644 --- a/src/ServiceBindings.php +++ b/src/ServiceBindings.php @@ -27,5 +27,8 @@ trait ServiceBindings Contracts\SupervisorRepository::class => Repositories\RedisSupervisorRepository::class, Contracts\TagRepository::class => Repositories\RedisTagRepository::class, Contracts\WorkloadRepository::class => Repositories\RedisWorkloadRepository::class, + + // Notifications... + Contracts\LongWaitDetectedNotification::class => Notifications\LongWaitDetected::class, ]; } diff --git a/tests/Feature/NotificationOverridesTest.php b/tests/Feature/NotificationOverridesTest.php new file mode 100644 index 00000000..6076d0d6 --- /dev/null +++ b/tests/Feature/NotificationOverridesTest.php @@ -0,0 +1,48 @@ +bind(LongWaitDetectedNotificationContract::class, CustomLongWaitDetectedNotification::class); + + event(new LongWaitDetected('redis', 'test-queue-2', 60)); + + Notification::assertSentOnDemand(CustomLongWaitDetectedNotification::class); + } + + public function test_normal_notifications_are_sent_if_not_specified() + { + Notification::fake(); + + Horizon::routeMailNotificationsTo('taylor@laravel.com'); + + event(new LongWaitDetected('redis', 'test-queue-2', 60)); + + Notification::assertSentOnDemand(LongWaitDetectedNotification::class); + } +} + +class CustomLongWaitDetectedNotification extends LongWaitDetectedNotification implements LongWaitDetectedNotificationContract +{ + public function toMail($notifiable) + { + return (new MailMessage) + ->line('This is a custom notification for a long wait.'); + } +}