Skip to content

Commit

Permalink
[5.3] A notification can be broadcasted to custom channels (#16170)
Browse files Browse the repository at this point in the history
* A notification can be broadcasted to custom channels

* Apply StyleCi formatting
  • Loading branch information
shadoWalker89 authored and taylorotwell committed Oct 31, 2016
1 parent 04a430a commit 174490b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public function __construct($notifiable, $notification, $data)
*/
public function broadcastOn()
{
$channels = $this->notification->broadcastOn();

if (! empty($channels)) {
return $channels;
}

return [new PrivateChannel($this->channelName())];
}

Expand Down
29 changes: 29 additions & 0 deletions tests/Notifications/NotificationBroadcastChannelTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Notifications\Notification;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Notifications\Channels\BroadcastChannel;

class NotificationBroadcastChannelTest extends PHPUnit_Framework_TestCase
Expand All @@ -21,6 +22,21 @@ public function testDatabaseChannelCreatesDatabaseRecordWithProperData()
$channel = new BroadcastChannel($events);
$channel->send($notifiable, $notification);
}

public function testNotificationIsBroadcastedOnCustomChannels()
{
$notification = new CustomChannelsTestNotification;
$notification->id = 1;
$notifiable = Mockery::mock();

$event = new Illuminate\Notifications\Events\BroadcastNotificationCreated(
$notifiable, $notification, $notification->toArray($notifiable)
);

$channels = $event->broadcastOn();

$this->assertEquals(new PrivateChannel('custom-channel'), $channels[0]);
}
}

class NotificationBroadcastChannelTestNotification extends Notification
Expand All @@ -30,3 +46,16 @@ public function toArray($notifiable)
return ['invoice_id' => 1];
}
}

class CustomChannelsTestNotification extends Notification
{
public function toArray($notifiable)
{
return ['invoice_id' => 1];
}

public function broadcastOn()
{
return [new PrivateChannel('custom-channel')];
}
}

0 comments on commit 174490b

Please sign in to comment.