From 10e32da7484dd3efe33a17d1548eaec131846bd2 Mon Sep 17 00:00:00 2001 From: Melek REBAI Date: Sat, 29 Oct 2016 19:55:04 +0100 Subject: [PATCH 1/2] A notification can be broadcasted to custom channels --- .../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..233f79af9118 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')]; + } +} From 7aa4df5989bd4d63f23cfee510f7aca6247fb21c Mon Sep 17 00:00:00 2001 From: Melek REBAI Date: Sat, 29 Oct 2016 20:03:04 +0100 Subject: [PATCH 2/2] Apply StyleCi formatting --- .../Notifications/Events/BroadcastNotificationCreated.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php b/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php index 233f79af9118..de66f9503727 100644 --- a/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php +++ b/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php @@ -56,7 +56,7 @@ public function broadcastOn() { $channels = $this->notification->broadcastOn(); - if (!empty($channels)) { + if (! empty($channels)) { return $channels; }