Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Granular notifications queue connections #45264

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
42 changes: 41 additions & 1 deletion 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 @@ -138,7 +161,7 @@ public function via($notifiable)
}
}

class DummyNotificationWithDatabaseVia extends Notification
class DummyNotificationWithDatabaseVia extends Notification implements ShouldQueue
driesvints marked this conversation as resolved.
Show resolved Hide resolved
{
use Queueable;

Expand All @@ -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