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

[5.6] Add broadcastAs method to Broadcasted Notifications #23236

Merged
merged 4 commits into from
Feb 21, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -72,7 +86,7 @@ public function broadcastWith()
{
return array_merge($this->data, [
'id' => $this->notification->id,
'type' => get_class($this->notification),
'type' => $this->broadcastAs(),
]);
}

Expand Down
43 changes: 43 additions & 0 deletions tests/Notifications/NotificationBroadcastChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down