From c4d6aa299404ba6c61e25d4e2e207592a3abb281 Mon Sep 17 00:00:00 2001 From: James Hemery Date: Sun, 11 Dec 2022 17:01:36 +0100 Subject: [PATCH 1/3] granular notifications queue connections --- .../Notifications/NotificationSender.php | 8 +++- .../Notifications/NotificationSenderTest.php | 42 ++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Notifications/NotificationSender.php b/src/Illuminate/Notifications/NotificationSender.php index d64b710530f3..698d19dcb95f 100644 --- a/src/Illuminate/Notifications/NotificationSender.php +++ b/src/Illuminate/Notifications/NotificationSender.php @@ -199,6 +199,12 @@ protected function queueNotification($notifiables, $notification) $notification->locale = $this->locale; } + $connection = $notification->connection; + + if (method_exists($notification, 'viaConnections')) { + $connection = $notification->viaConnections()[$channel] ?? null; + } + $queue = $notification->queue; if (method_exists($notification, 'viaQueues')) { @@ -222,7 +228,7 @@ protected function queueNotification($notifiables, $notification) $this->bus->dispatch( (new SendQueuedNotifications($notifiable, $notification, [$channel])) - ->onConnection($notification->connection) + ->onConnection($connection) ->onQueue($queue) ->delay(is_array($delay) ? ($delay[$channel] ?? null) : $delay) ->through($middleware) diff --git a/tests/Notifications/NotificationSenderTest.php b/tests/Notifications/NotificationSenderTest.php index d0fba449c849..b2d50a7c00cc 100644 --- a/tests/Notifications/NotificationSenderTest.php +++ b/tests/Notifications/NotificationSenderTest.php @@ -104,6 +104,29 @@ public function testItCanSendQueuedMultiChannelNotificationsThroughDifferentMidd $sender->send($notifiable, new DummyMultiChannelNotificationWithConditionalMiddlware); } + + public function testItCanSendQueuedWithViaConnectionsNotifications() + { + $notifiable = new AnonymousNotifiable; + $manager = m::mock(ChannelManager::class); + $bus = m::mock(BusDispatcher::class); + $bus->shouldReceive('dispatch') + ->once() + ->withArgs(function ($job) { + return $job->connection === 'sync' && $job->channels === ['database']; + }); + $bus->shouldReceive('dispatch') + ->once() + ->withArgs(function ($job) { + return $job->connection === null && $job->channels === ['mail']; + }); + + $events = m::mock(EventDispatcher::class); + + $sender = new NotificationSender($manager, $bus, $events); + + $sender->send($notifiable, new DummyNotificationWithViaConnections); + } } class DummyQueuedNotificationWithStringVia extends Notification implements ShouldQueue @@ -138,7 +161,7 @@ public function via($notifiable) } } -class DummyNotificationWithDatabaseVia extends Notification +class DummyNotificationWithDatabaseVia extends Notification implements ShouldQueue { use Queueable; @@ -154,6 +177,23 @@ public function via($notifiable) } } +class DummyNotificationWithViaConnections extends Notification implements ShouldQueue +{ + use Queueable; + + public function via($notifiable) + { + return ['mail', 'database']; + } + + public function viaConnections() + { + return [ + 'database' => 'sync' + ]; + } +} + class DummyNotificationWithMiddleware extends Notification implements ShouldQueue { use Queueable; From e83ec188108160eb5561e98bca32f781a2d4a049 Mon Sep 17 00:00:00 2001 From: James Hemery Date: Sun, 11 Dec 2022 17:04:20 +0100 Subject: [PATCH 2/3] fix style --- tests/Notifications/NotificationSenderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Notifications/NotificationSenderTest.php b/tests/Notifications/NotificationSenderTest.php index b2d50a7c00cc..01b147bf8bb3 100644 --- a/tests/Notifications/NotificationSenderTest.php +++ b/tests/Notifications/NotificationSenderTest.php @@ -189,7 +189,7 @@ public function via($notifiable) public function viaConnections() { return [ - 'database' => 'sync' + 'database' => 'sync', ]; } } From a11ac2178b04b83d99c34d46d3ce6c29c44dea31 Mon Sep 17 00:00:00 2001 From: James Hemery Date: Mon, 12 Dec 2022 09:24:36 +0100 Subject: [PATCH 3/3] fix: bad test update --- tests/Notifications/NotificationSenderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Notifications/NotificationSenderTest.php b/tests/Notifications/NotificationSenderTest.php index 01b147bf8bb3..d969e40c532f 100644 --- a/tests/Notifications/NotificationSenderTest.php +++ b/tests/Notifications/NotificationSenderTest.php @@ -161,7 +161,7 @@ public function via($notifiable) } } -class DummyNotificationWithDatabaseVia extends Notification implements ShouldQueue +class DummyNotificationWithDatabaseVia extends Notification { use Queueable;