Skip to content

Commit e969ffc

Browse files
authored
[9.x] Granular notifications queue connections (#45264)
* granular notifications queue connections * fix style * fix: bad test update
1 parent ffb2f4e commit e969ffc

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/Illuminate/Notifications/NotificationSender.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ protected function queueNotification($notifiables, $notification)
199199
$notification->locale = $this->locale;
200200
}
201201

202+
$connection = $notification->connection;
203+
204+
if (method_exists($notification, 'viaConnections')) {
205+
$connection = $notification->viaConnections()[$channel] ?? null;
206+
}
207+
202208
$queue = $notification->queue;
203209

204210
if (method_exists($notification, 'viaQueues')) {
@@ -222,7 +228,7 @@ protected function queueNotification($notifiables, $notification)
222228

223229
$this->bus->dispatch(
224230
(new SendQueuedNotifications($notifiable, $notification, [$channel]))
225-
->onConnection($notification->connection)
231+
->onConnection($connection)
226232
->onQueue($queue)
227233
->delay(is_array($delay) ? ($delay[$channel] ?? null) : $delay)
228234
->through($middleware)

tests/Notifications/NotificationSenderTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,29 @@ public function testItCanSendQueuedMultiChannelNotificationsThroughDifferentMidd
104104

105105
$sender->send($notifiable, new DummyMultiChannelNotificationWithConditionalMiddlware);
106106
}
107+
108+
public function testItCanSendQueuedWithViaConnectionsNotifications()
109+
{
110+
$notifiable = new AnonymousNotifiable;
111+
$manager = m::mock(ChannelManager::class);
112+
$bus = m::mock(BusDispatcher::class);
113+
$bus->shouldReceive('dispatch')
114+
->once()
115+
->withArgs(function ($job) {
116+
return $job->connection === 'sync' && $job->channels === ['database'];
117+
});
118+
$bus->shouldReceive('dispatch')
119+
->once()
120+
->withArgs(function ($job) {
121+
return $job->connection === null && $job->channels === ['mail'];
122+
});
123+
124+
$events = m::mock(EventDispatcher::class);
125+
126+
$sender = new NotificationSender($manager, $bus, $events);
127+
128+
$sender->send($notifiable, new DummyNotificationWithViaConnections);
129+
}
107130
}
108131

109132
class DummyQueuedNotificationWithStringVia extends Notification implements ShouldQueue
@@ -154,6 +177,23 @@ public function via($notifiable)
154177
}
155178
}
156179

180+
class DummyNotificationWithViaConnections extends Notification implements ShouldQueue
181+
{
182+
use Queueable;
183+
184+
public function via($notifiable)
185+
{
186+
return ['mail', 'database'];
187+
}
188+
189+
public function viaConnections()
190+
{
191+
return [
192+
'database' => 'sync',
193+
];
194+
}
195+
}
196+
157197
class DummyNotificationWithMiddleware extends Notification implements ShouldQueue
158198
{
159199
use Queueable;

0 commit comments

Comments
 (0)