From c9416d0647af1df857f04668fba3f7728819c623 Mon Sep 17 00:00:00 2001 From: Kevin Ullyott Date: Thu, 5 Jun 2025 23:24:57 -0400 Subject: [PATCH 1/3] Setup the binding and ability to swap SendQueuedNotifications::class Signed-off-by: Kevin Ullyott --- src/Illuminate/Notifications/NotificationSender.php | 6 +++++- .../Notifications/NotificationServiceProvider.php | 2 ++ tests/Notifications/NotificationSenderTest.php | 5 +++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Notifications/NotificationSender.php b/src/Illuminate/Notifications/NotificationSender.php index 46ef9e88cf15..238653e7042f 100644 --- a/src/Illuminate/Notifications/NotificationSender.php +++ b/src/Illuminate/Notifications/NotificationSender.php @@ -249,7 +249,11 @@ protected function queueNotification($notifiables, $notification) } $this->bus->dispatch( - (new SendQueuedNotifications($notifiable, $notification, [$channel])) + $this->manager->getContainer()->make(SendQueuedNotifications::class, [ + 'notifiables' => $notifiable, + 'notification' => $notification, + 'channels' => [$channel], + ]) ->onConnection($connection) ->onQueue($queue) ->delay(is_array($delay) ? ($delay[$channel] ?? null) : $delay) diff --git a/src/Illuminate/Notifications/NotificationServiceProvider.php b/src/Illuminate/Notifications/NotificationServiceProvider.php index a8286eae8a9f..5576d01b0d2d 100644 --- a/src/Illuminate/Notifications/NotificationServiceProvider.php +++ b/src/Illuminate/Notifications/NotificationServiceProvider.php @@ -40,5 +40,7 @@ public function register() $this->app->alias( ChannelManager::class, FactoryContract::class ); + + $this->app->bind(SendQueuedNotifications::class, SendQueuedNotifications::class); } } diff --git a/tests/Notifications/NotificationSenderTest.php b/tests/Notifications/NotificationSenderTest.php index 6e3a9954938c..bd35a7a8e0a5 100644 --- a/tests/Notifications/NotificationSenderTest.php +++ b/tests/Notifications/NotificationSenderTest.php @@ -27,6 +27,7 @@ public function testItCanSendQueuedNotificationsWithAStringVia() { $notifiable = m::mock(Notifiable::class); $manager = m::mock(ChannelManager::class); + $manager->shouldReceive('getContainer')->andReturn(app()); $bus = m::mock(BusDispatcher::class); $bus->shouldReceive('dispatch'); $events = m::mock(EventDispatcher::class); @@ -55,6 +56,7 @@ public function testItCannotSendNotificationsViaDatabaseForAnonymousNotifiables( { $notifiable = new AnonymousNotifiable; $manager = m::mock(ChannelManager::class); + $manager->shouldReceive('getContainer')->andReturn(app()); $bus = m::mock(BusDispatcher::class); $bus->shouldNotReceive('dispatch'); $events = m::mock(EventDispatcher::class); @@ -76,6 +78,7 @@ public function testItCanSendQueuedNotificationsThroughMiddleware() }); $events = m::mock(EventDispatcher::class); $events->shouldReceive('listen')->once(); + $manager->shouldReceive('getContainer')->andReturn(app()); $sender = new NotificationSender($manager, $bus, $events); @@ -86,6 +89,7 @@ public function testItCanSendQueuedMultiChannelNotificationsThroughDifferentMidd { $notifiable = m::mock(Notifiable::class); $manager = m::mock(ChannelManager::class); + $manager->shouldReceive('getContainer')->andReturn(app()); $bus = m::mock(BusDispatcher::class); $bus->shouldReceive('dispatch') ->once() @@ -114,6 +118,7 @@ public function testItCanSendQueuedWithViaConnectionsNotifications() { $notifiable = new AnonymousNotifiable; $manager = m::mock(ChannelManager::class); + $manager->shouldReceive('getContainer')->andReturn(app()); $bus = m::mock(BusDispatcher::class); $bus->shouldReceive('dispatch') ->once() From 2ea64676b06fcd7b50ab53d04b38b63859f12a31 Mon Sep 17 00:00:00 2001 From: Kevin Ullyott Date: Thu, 5 Jun 2025 23:28:30 -0400 Subject: [PATCH 2/3] Add a test Signed-off-by: Kevin Ullyott --- .../NotificationChannelManagerTest.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/Notifications/NotificationChannelManagerTest.php b/tests/Notifications/NotificationChannelManagerTest.php index 4b272db8399f..f72333761aa8 100644 --- a/tests/Notifications/NotificationChannelManagerTest.php +++ b/tests/Notifications/NotificationChannelManagerTest.php @@ -15,6 +15,8 @@ use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notification; use Illuminate\Notifications\SendQueuedNotifications; +use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Queue\SerializesModels; use Illuminate\Support\Collection; use Mockery as m; use PHPUnit\Framework\TestCase; @@ -159,6 +161,26 @@ public function testNotificationCanBeQueued() $manager->send([new NotificationChannelManagerTestNotifiable], new NotificationChannelManagerTestQueuedNotification); } + + public function testSendQueuedNotificationsCanBeOverrideViaContainer() + { + $container = new Container; + $container->instance('config', ['app.name' => 'Name', 'app.logo' => 'Logo']); + $container->instance(Dispatcher::class, $events = m::mock()); + $container->instance(Bus::class, $bus = m::mock()); + $bus->shouldReceive('dispatch')->with(m::type(TestSendQueuedNotifications::class)); + $container->bind(SendQueuedNotifications::class, TestSendQueuedNotifications::class); + Container::setInstance($container); + $manager = m::mock(ChannelManager::class.'[driver]', [$container]); + $events->shouldReceive('listen')->once(); + + $manager->send([new NotificationChannelManagerTestNotifiable], new NotificationChannelManagerTestQueuedNotification); + } +} + +class TestSendQueuedNotifications implements ShouldQueue +{ + use InteractsWithQueue, Queueable, SerializesModels; } class NotificationChannelManagerTestNotifiable From defe3677d74b6fd1cf5d9798302389dfda91efc4 Mon Sep 17 00:00:00 2001 From: Kevin Ullyott Date: Fri, 6 Jun 2025 09:08:28 -0400 Subject: [PATCH 3/3] Remove binding Signed-off-by: Kevin Ullyott --- src/Illuminate/Notifications/NotificationServiceProvider.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Illuminate/Notifications/NotificationServiceProvider.php b/src/Illuminate/Notifications/NotificationServiceProvider.php index 5576d01b0d2d..a8286eae8a9f 100644 --- a/src/Illuminate/Notifications/NotificationServiceProvider.php +++ b/src/Illuminate/Notifications/NotificationServiceProvider.php @@ -40,7 +40,5 @@ public function register() $this->app->alias( ChannelManager::class, FactoryContract::class ); - - $this->app->bind(SendQueuedNotifications::class, SendQueuedNotifications::class); } }