Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Allow custom broadcastWith in notification broadcast channel #35142

Merged
merged 3 commits into from
Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'];
}
}