Skip to content

Commit

Permalink
[8.x] Allow custom broadcastWith in notification broadcast channel (#…
Browse files Browse the repository at this point in the history
…35142)

* Allow custom broadcastWith in notification broadcast channel

* Fixed style

* Fixed style
  • Loading branch information
aalyusuf authored Nov 7, 2020
1 parent d1a52d4 commit a9af73f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ 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(),
Expand Down
28 changes: 28 additions & 0 deletions tests/Notifications/NotificationBroadcastChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'];
}
}

0 comments on commit a9af73f

Please sign in to comment.