diff --git a/src/Illuminate/Notifications/InstantNotifiable.php b/src/Illuminate/Notifications/InstantNotifiable.php new file mode 100644 index 000000000000..fc0abcfbc719 --- /dev/null +++ b/src/Illuminate/Notifications/InstantNotifiable.php @@ -0,0 +1,51 @@ +send($this, $instance); + } + + /** + * Get the notification routing information for the given driver. + * + * @param string $driver + * @return mixed + */ + public function routeNotificationFor($driver) + { + return $this->routes[$driver] ?? null; + } + + /** + * Register the given route. + * + * @param mixed $route + * @param string $channel + * @return $this + */ + public function addRoute($route, $channel) + { + $this->routes[$channel] = $route; + + return $this; + } +} diff --git a/src/Illuminate/Notifications/NotificationSender.php b/src/Illuminate/Notifications/NotificationSender.php index 64caec24b601..6d19479dbe2d 100644 --- a/src/Illuminate/Notifications/NotificationSender.php +++ b/src/Illuminate/Notifications/NotificationSender.php @@ -154,7 +154,7 @@ protected function queueNotification($notifiables, $notification) $notification->id = $notificationId; $this->bus->dispatch( - (new SendQueuedNotifications($this->formatNotifiables($notifiable), $notification, [$channel])) + (new SendQueuedNotifications($notifiable, $notification, [$channel])) ->onConnection($notification->connection) ->onQueue($notification->queue) ->delay($notification->delay) diff --git a/tests/Integration/Notifications/SendingNotificationsViaInstantNotifiableTest.php b/tests/Integration/Notifications/SendingNotificationsViaInstantNotifiableTest.php new file mode 100644 index 000000000000..6065b615be80 --- /dev/null +++ b/tests/Integration/Notifications/SendingNotificationsViaInstantNotifiableTest.php @@ -0,0 +1,59 @@ +set('app.debug', 'true'); + } + + public function test_mail_is_sent() + { + $notifiable = (new InstantNotifiable()) + ->addRoute('enzo', 'testchannel') + ->addRoute('enzo@deepblue.com', 'anothertestchannel'); + + NotificationFacade::send( + $notifiable, + new TestMailNotificationForInstantNotifiable() + ); + + $this->assertEquals([ + 'enzo', 'enzo@deepblue.com', + ], $_SERVER['__notifiable.route']); + } +} + +class TestMailNotificationForInstantNotifiable extends Notification +{ + public function via($notifiable) + { + return [TestCustomChannel::class, AnotherTestCustomChannel::class]; + } +} + +class TestCustomChannel +{ + public function send($notifiable, $notification) + { + $_SERVER['__notifiable.route'][] = $notifiable->routeNotificationFor('testchannel'); + } +} + +class AnotherTestCustomChannel +{ + public function send($notifiable, $notification) + { + $_SERVER['__notifiable.route'][] = $notifiable->routeNotificationFor('anothertestchannel'); + } +}