Skip to content

Commit 07df8bc

Browse files
authored
[12.x] feat: Add ability to override SendQueuedNotifications job class (#55942)
* Setup the binding and ability to swap SendQueuedNotifications::class Signed-off-by: Kevin Ullyott <ullyott.kevin@gmail.com> * Add a test Signed-off-by: Kevin Ullyott <ullyott.kevin@gmail.com> * Remove binding Signed-off-by: Kevin Ullyott <ullyott.kevin@gmail.com> --------- Signed-off-by: Kevin Ullyott <ullyott.kevin@gmail.com>
1 parent 7ae03b1 commit 07df8bc

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/Illuminate/Notifications/NotificationSender.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,11 @@ protected function queueNotification($notifiables, $notification)
249249
}
250250

251251
$this->bus->dispatch(
252-
(new SendQueuedNotifications($notifiable, $notification, [$channel]))
252+
$this->manager->getContainer()->make(SendQueuedNotifications::class, [
253+
'notifiables' => $notifiable,
254+
'notification' => $notification,
255+
'channels' => [$channel],
256+
])
253257
->onConnection($connection)
254258
->onQueue($queue)
255259
->delay(is_array($delay) ? ($delay[$channel] ?? null) : $delay)

tests/Notifications/NotificationChannelManagerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Illuminate\Notifications\Notifiable;
1616
use Illuminate\Notifications\Notification;
1717
use Illuminate\Notifications\SendQueuedNotifications;
18+
use Illuminate\Queue\InteractsWithQueue;
19+
use Illuminate\Queue\SerializesModels;
1820
use Illuminate\Support\Collection;
1921
use Mockery as m;
2022
use PHPUnit\Framework\TestCase;
@@ -159,6 +161,26 @@ public function testNotificationCanBeQueued()
159161

160162
$manager->send([new NotificationChannelManagerTestNotifiable], new NotificationChannelManagerTestQueuedNotification);
161163
}
164+
165+
public function testSendQueuedNotificationsCanBeOverrideViaContainer()
166+
{
167+
$container = new Container;
168+
$container->instance('config', ['app.name' => 'Name', 'app.logo' => 'Logo']);
169+
$container->instance(Dispatcher::class, $events = m::mock());
170+
$container->instance(Bus::class, $bus = m::mock());
171+
$bus->shouldReceive('dispatch')->with(m::type(TestSendQueuedNotifications::class));
172+
$container->bind(SendQueuedNotifications::class, TestSendQueuedNotifications::class);
173+
Container::setInstance($container);
174+
$manager = m::mock(ChannelManager::class.'[driver]', [$container]);
175+
$events->shouldReceive('listen')->once();
176+
177+
$manager->send([new NotificationChannelManagerTestNotifiable], new NotificationChannelManagerTestQueuedNotification);
178+
}
179+
}
180+
181+
class TestSendQueuedNotifications implements ShouldQueue
182+
{
183+
use InteractsWithQueue, Queueable, SerializesModels;
162184
}
163185

164186
class NotificationChannelManagerTestNotifiable

tests/Notifications/NotificationSenderTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function testItCanSendQueuedNotificationsWithAStringVia()
2727
{
2828
$notifiable = m::mock(Notifiable::class);
2929
$manager = m::mock(ChannelManager::class);
30+
$manager->shouldReceive('getContainer')->andReturn(app());
3031
$bus = m::mock(BusDispatcher::class);
3132
$bus->shouldReceive('dispatch');
3233
$events = m::mock(EventDispatcher::class);
@@ -55,6 +56,7 @@ public function testItCannotSendNotificationsViaDatabaseForAnonymousNotifiables(
5556
{
5657
$notifiable = new AnonymousNotifiable;
5758
$manager = m::mock(ChannelManager::class);
59+
$manager->shouldReceive('getContainer')->andReturn(app());
5860
$bus = m::mock(BusDispatcher::class);
5961
$bus->shouldNotReceive('dispatch');
6062
$events = m::mock(EventDispatcher::class);
@@ -76,6 +78,7 @@ public function testItCanSendQueuedNotificationsThroughMiddleware()
7678
});
7779
$events = m::mock(EventDispatcher::class);
7880
$events->shouldReceive('listen')->once();
81+
$manager->shouldReceive('getContainer')->andReturn(app());
7982

8083
$sender = new NotificationSender($manager, $bus, $events);
8184

@@ -86,6 +89,7 @@ public function testItCanSendQueuedMultiChannelNotificationsThroughDifferentMidd
8689
{
8790
$notifiable = m::mock(Notifiable::class);
8891
$manager = m::mock(ChannelManager::class);
92+
$manager->shouldReceive('getContainer')->andReturn(app());
8993
$bus = m::mock(BusDispatcher::class);
9094
$bus->shouldReceive('dispatch')
9195
->once()
@@ -114,6 +118,7 @@ public function testItCanSendQueuedWithViaConnectionsNotifications()
114118
{
115119
$notifiable = new AnonymousNotifiable;
116120
$manager = m::mock(ChannelManager::class);
121+
$manager->shouldReceive('getContainer')->andReturn(app());
117122
$bus = m::mock(BusDispatcher::class);
118123
$bus->shouldReceive('dispatch')
119124
->once()

0 commit comments

Comments
 (0)