From 6f28b66e568f529e7fc979fc6ecb01315b65962c Mon Sep 17 00:00:00 2001 From: a-komarev Date: Wed, 21 Feb 2018 04:51:07 +0300 Subject: [PATCH] Add broadcastAs method to Broadcasting Notifications --- .../Events/BroadcastNotificationCreated.php | 16 ++++++- .../NotificationBroadcastChannelTest.php | 43 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php b/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php index c93fe5c57a6d..e5bf6b4ea871 100644 --- a/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php +++ b/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php @@ -63,6 +63,20 @@ public function broadcastOn() return [new PrivateChannel($this->channelName())]; } + /** + * Get the type of the broadcasted event. + * + * @return string + */ + public function broadcastAs() + { + if (method_exists($this->notification, 'broadcastAs')) { + return $this->notification->broadcastAs(); + } + + return get_class($this->notification); + } + /** * Get the data that should be sent with the broadcasted event. * @@ -72,7 +86,7 @@ public function broadcastWith() { return array_merge($this->data, [ 'id' => $this->notification->id, - 'type' => get_class($this->notification), + 'type' => $this->broadcastAs(), ]); } diff --git a/tests/Notifications/NotificationBroadcastChannelTest.php b/tests/Notifications/NotificationBroadcastChannelTest.php index e67c7aa63133..e360ab903be1 100644 --- a/tests/Notifications/NotificationBroadcastChannelTest.php +++ b/tests/Notifications/NotificationBroadcastChannelTest.php @@ -42,6 +42,36 @@ public function testNotificationIsBroadcastedOnCustomChannels() $this->assertEquals(new PrivateChannel('custom-channel'), $channels[0]); } + public function testNotificationIsBroadcastedWithCustomEventName() + { + $notification = new CustomEventNameTestNotification; + $notification->id = 1; + $notifiable = Mockery::mock(); + + $event = new \Illuminate\Notifications\Events\BroadcastNotificationCreated( + $notifiable, $notification, $notification->toArray($notifiable) + ); + + $eventName = $event->broadcastAs(); + + $this->assertSame('custom.type', $eventName); + } + + public function testNotificationIsBroadcastedWithCustomDataType() + { + $notification = new CustomEventNameTestNotification; + $notification->id = 1; + $notifiable = Mockery::mock(); + + $event = new \Illuminate\Notifications\Events\BroadcastNotificationCreated( + $notifiable, $notification, $notification->toArray($notifiable) + ); + + $data = $event->broadcastWith(); + + $this->assertSame('custom.type', $data['type']); + } + public function testNotificationIsBroadcastedNow() { $notification = new TestNotificationBroadCastedNow; @@ -78,6 +108,19 @@ public function broadcastOn() } } +class CustomEventNameTestNotification extends Notification +{ + public function toArray($notifiable) + { + return ['invoice_id' => 1]; + } + + public function broadcastAs() + { + return 'custom.type'; + } +} + class TestNotificationBroadCastedNow extends Notification { public function toArray($notifiable)