Skip to content

Commit 0fc5528

Browse files
aydinfatihFatih Aydın
andauthored
[12.x] Fix: Correctly fallback to notification's connection/queue when using viaConnections/viaQueues (laravel#57625)
* fix: Ensure default connection fallback in `viaConnections` method * fix: Ensure default queue fallback in `viaQueues` method * test: Add tests for queued notifications with array and viaQueues --------- Co-authored-by: Fatih Aydın <[email protected]>
1 parent 344c7ea commit 0fc5528

File tree

2 files changed

+105
-4
lines changed

2 files changed

+105
-4
lines changed

src/Illuminate/Notifications/NotificationSender.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,13 @@ protected function queueNotification($notifiables, $notification)
232232
$connection = $notification->connection;
233233

234234
if (method_exists($notification, 'viaConnections')) {
235-
$connection = $notification->viaConnections()[$channel] ?? null;
235+
$connection = $notification->viaConnections()[$channel] ?? $connection;
236236
}
237237

238238
$queue = $notification->queue;
239239

240240
if (method_exists($notification, 'viaQueues')) {
241-
$queue = $notification->viaQueues()[$channel] ?? null;
241+
$queue = $notification->viaQueues()[$channel] ?? $queue;
242242
}
243243

244244
$delay = $notification->delay;

tests/Notifications/NotificationSenderTest.php

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,31 @@ public function testItCanSendQueuedNotificationsWithAStringVia()
4343
$sender->send($notifiable, new DummyQueuedNotificationWithStringVia);
4444
}
4545

46+
public function testItCanSendQueuedNotificationsWithAnArrayVia()
47+
{
48+
$notifiable = m::mock(Notifiable::class);
49+
$manager = m::mock(ChannelManager::class);
50+
$manager->shouldReceive('getContainer')->andReturn(app());
51+
$bus = m::mock(BusDispatcher::class);
52+
$bus->shouldReceive('dispatch')
53+
->once()
54+
->withArgs(function ($job) {
55+
return $job->queue === 'dummy' && $job->channels === ['database'] && $job->connection === 'redis';
56+
});
57+
$bus->shouldReceive('dispatch')
58+
->once()
59+
->withArgs(function ($job) {
60+
return $job->queue === 'dummy' && $job->channels === ['mail'] && $job->connection === 'redis';
61+
});
62+
63+
$events = m::mock(EventDispatcher::class);
64+
$events->shouldReceive('listen')->once();
65+
66+
$sender = new NotificationSender($manager, $bus, $events);
67+
68+
$sender->send($notifiable, new DummyQueuedNotificationWithArrayVia);
69+
}
70+
4671
public function testItCanSendNotificationsWithAnEmptyStringVia()
4772
{
4873
$notifiable = new AnonymousNotifiable;
@@ -128,12 +153,12 @@ public function testItCanSendQueuedWithViaConnectionsNotifications()
128153
$bus->shouldReceive('dispatch')
129154
->once()
130155
->withArgs(function ($job) {
131-
return $job->connection === 'sync' && $job->channels === ['database'];
156+
return $job->connection === 'sync' && $job->channels === ['database'] && $job->queue === 'dummy';
132157
});
133158
$bus->shouldReceive('dispatch')
134159
->once()
135160
->withArgs(function ($job) {
136-
return $job->connection === null && $job->channels === ['mail'];
161+
return $job->connection === 'redis' && $job->channels === ['mail'] && $job->queue === 'dummy';
137162
});
138163

139164
$events = m::mock(EventDispatcher::class);
@@ -144,6 +169,31 @@ public function testItCanSendQueuedWithViaConnectionsNotifications()
144169
$sender->send($notifiable, new DummyNotificationWithViaConnections);
145170
}
146171

172+
public function testItCanSendQueuedWithViaQueuesNotifications()
173+
{
174+
$notifiable = new AnonymousNotifiable;
175+
$manager = m::mock(ChannelManager::class);
176+
$manager->shouldReceive('getContainer')->andReturn(app());
177+
$bus = m::mock(BusDispatcher::class);
178+
$bus->shouldReceive('dispatch')
179+
->once()
180+
->withArgs(function ($job) {
181+
return $job->queue === 'dummy' && $job->channels === ['database'] && $job->connection === 'redis';
182+
});
183+
$bus->shouldReceive('dispatch')
184+
->once()
185+
->withArgs(function ($job) {
186+
return $job->queue === 'admin_notifications' && $job->channels === ['mail'] && $job->connection === 'redis';
187+
});
188+
189+
$events = m::mock(EventDispatcher::class);
190+
$events->shouldReceive('listen')->once();
191+
192+
$sender = new NotificationSender($manager, $bus, $events);
193+
194+
$sender->send($notifiable, new DummyNotificationWithViaQueues);
195+
}
196+
147197
public function testNotificationFailedSentWithoutHttpTransportException()
148198
{
149199
$this->expectException(TransportException::class);
@@ -184,6 +234,28 @@ public function via($notifiable)
184234
}
185235
}
186236

237+
class DummyQueuedNotificationWithArrayVia extends Notification implements ShouldQueue
238+
{
239+
use Queueable;
240+
241+
public function __construct()
242+
{
243+
$this->connection = 'redis';
244+
$this->queue = 'dummy';
245+
}
246+
247+
/**
248+
* Get the notification channels.
249+
*
250+
* @param mixed $notifiable
251+
* @return array|string
252+
*/
253+
public function via($notifiable)
254+
{
255+
return ['mail', 'database'];
256+
}
257+
}
258+
187259
class DummyNotificationWithEmptyStringVia extends Notification
188260
{
189261
use Queueable;
@@ -220,6 +292,12 @@ class DummyNotificationWithViaConnections extends Notification implements Should
220292
{
221293
use Queueable;
222294

295+
public function __construct()
296+
{
297+
$this->connection = 'redis';
298+
$this->queue = 'dummy';
299+
}
300+
223301
public function via($notifiable)
224302
{
225303
return ['mail', 'database'];
@@ -233,6 +311,29 @@ public function viaConnections()
233311
}
234312
}
235313

314+
class DummyNotificationWithViaQueues extends Notification implements ShouldQueue
315+
{
316+
use Queueable;
317+
318+
public function __construct()
319+
{
320+
$this->connection = 'redis';
321+
$this->queue = 'dummy';
322+
}
323+
324+
public function via($notifiable)
325+
{
326+
return ['mail', 'database'];
327+
}
328+
329+
public function viaQueues()
330+
{
331+
return [
332+
'mail' => 'admin_notifications',
333+
];
334+
}
335+
}
336+
236337
class DummyNotificationWithMiddleware extends Notification implements ShouldQueue
237338
{
238339
use Queueable;

0 commit comments

Comments
 (0)