Skip to content

Commit

Permalink
[9.x] Granular notifications queue connections (#45264)
Browse files Browse the repository at this point in the history
* granular notifications queue connections

* fix style

* fix: bad test update
  • Loading branch information
JamesHemery committed Dec 12, 2022
1 parent ffb2f4e commit e969ffc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Illuminate/Notifications/NotificationSender.php
Expand Up @@ -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')) {
Expand All @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions tests/Notifications/NotificationSenderTest.php
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e969ffc

Please sign in to comment.