From 174490baa3707b26f0bc118e44e2256587b1ff2a Mon Sep 17 00:00:00 2001 From: Melek REBAI Date: Mon, 31 Oct 2016 15:21:10 +0100 Subject: [PATCH] [5.3] A notification can be broadcasted to custom channels (#16170) * A notification can be broadcasted to custom channels * Apply StyleCi formatting --- .../Events/BroadcastNotificationCreated.php | 6 ++++ .../NotificationBroadcastChannelTest.php | 29 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php b/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php index 5b25944ace1d..de66f9503727 100644 --- a/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php +++ b/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php @@ -54,6 +54,12 @@ public function __construct($notifiable, $notification, $data) */ public function broadcastOn() { + $channels = $this->notification->broadcastOn(); + + if (! empty($channels)) { + return $channels; + } + return [new PrivateChannel($this->channelName())]; } diff --git a/tests/Notifications/NotificationBroadcastChannelTest.php b/tests/Notifications/NotificationBroadcastChannelTest.php index db7954fcb17e..440432b5f2c4 100644 --- a/tests/Notifications/NotificationBroadcastChannelTest.php +++ b/tests/Notifications/NotificationBroadcastChannelTest.php @@ -1,6 +1,7 @@ send($notifiable, $notification); } + + public function testNotificationIsBroadcastedOnCustomChannels() + { + $notification = new CustomChannelsTestNotification; + $notification->id = 1; + $notifiable = Mockery::mock(); + + $event = new Illuminate\Notifications\Events\BroadcastNotificationCreated( + $notifiable, $notification, $notification->toArray($notifiable) + ); + + $channels = $event->broadcastOn(); + + $this->assertEquals(new PrivateChannel('custom-channel'), $channels[0]); + } } class NotificationBroadcastChannelTestNotification extends Notification @@ -30,3 +46,16 @@ public function toArray($notifiable) return ['invoice_id' => 1]; } } + +class CustomChannelsTestNotification extends Notification +{ + public function toArray($notifiable) + { + return ['invoice_id' => 1]; + } + + public function broadcastOn() + { + return [new PrivateChannel('custom-channel')]; + } +}