From 279e4193be16642439a999cd1463b6a15cdfc725 Mon Sep 17 00:00:00 2001 From: Ahmed Date: Sat, 7 Nov 2020 16:15:15 +0300 Subject: [PATCH 1/3] Allow custom broadcastWith in notification broadcast channel --- .../Events/BroadcastNotificationCreated.php | 5 ++++ .../NotificationBroadcastChannelTest.php | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php b/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php index 77498ea39874..5ff4e5a15c93 100644 --- a/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php +++ b/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php @@ -92,6 +92,11 @@ protected function channelName() */ public function broadcastWith() { + if(method_exists($this->notification, 'broadcastWith')) + { + return $this->notification->broadcastWith(); + } + return array_merge($this->data, [ 'id' => $this->notification->id, 'type' => $this->broadcastType(), diff --git a/tests/Notifications/NotificationBroadcastChannelTest.php b/tests/Notifications/NotificationBroadcastChannelTest.php index 033cf3f8b3f3..e4cae95c3219 100644 --- a/tests/Notifications/NotificationBroadcastChannelTest.php +++ b/tests/Notifications/NotificationBroadcastChannelTest.php @@ -88,6 +88,21 @@ public function testNotificationIsBroadcastedNow() $channel = new BroadcastChannel($events); $channel->send($notifiable, $notification); } + + public function testNotificationIsBroadcastedWithCustomAdditionalPayload() + { + $notification = new CustomBroadcastWithTestNotification; + $notification->id = 1; + $notifiable = m::mock(); + + $event = new BroadcastNotificationCreated( + $notifiable, $notification, $notification->toArray($notifiable) + ); + + $data = $event->broadcastWith(); + + $this->assertArrayHasKey('additional', $data); + } } class NotificationBroadcastChannelTestNotification extends Notification @@ -136,3 +151,16 @@ public function toBroadcast() return (new BroadcastMessage([]))->onConnection('sync'); } } + +class CustomBroadcastWithTestNotification extends Notification +{ + public function toArray($notifiable) + { + return ['invoice_id' => 1]; + } + + public function broadcastWith() + { + return ['id' => 1, 'type' => 'custom', 'additional' => 'custom']; + } +} From 472cebab1173cdd8eef1427e3257f71f20e206e2 Mon Sep 17 00:00:00 2001 From: Ahmed Date: Sat, 7 Nov 2020 16:39:13 +0300 Subject: [PATCH 2/3] Fixed style --- .../Notifications/Events/BroadcastNotificationCreated.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php b/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php index 5ff4e5a15c93..c705c5d6b2b7 100644 --- a/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php +++ b/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php @@ -92,8 +92,7 @@ protected function channelName() */ public function broadcastWith() { - if(method_exists($this->notification, 'broadcastWith')) - { + if(method_exists($this->notification, 'broadcastWith')) { return $this->notification->broadcastWith(); } From 9d7783402a6e78d7be1729be96659ef55197a5bf Mon Sep 17 00:00:00 2001 From: Ahmed Date: Sat, 7 Nov 2020 16:41:08 +0300 Subject: [PATCH 3/3] Fixed style --- .../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 c705c5d6b2b7..24958852758b 100644 --- a/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php +++ b/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php @@ -92,7 +92,7 @@ protected function channelName() */ public function broadcastWith() { - if(method_exists($this->notification, 'broadcastWith')) { + if (method_exists($this->notification, 'broadcastWith')) { return $this->notification->broadcastWith(); }