| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace Illuminate\Tests\Notifications;
- use Illuminate\Broadcasting\PrivateChannel;
- use Illuminate\Contracts\Events\Dispatcher;
- use Illuminate\Notifications\Channels\BroadcastChannel;
- use Illuminate\Notifications\Events\BroadcastNotificationCreated;
- use Illuminate\Notifications\Messages\BroadcastMessage;
- use Illuminate\Notifications\Notification;
- use Mockery as m;
- use PHPUnit\Framework\TestCase;
- class NotificationBroadcastChannelTest extends TestCase
- {
- protected function tearDown(): void
- {
- m::close();
- }
- public function testDatabaseChannelCreatesDatabaseRecordWithProperData()
- {
- $notification = new NotificationBroadcastChannelTestNotification;
- $notification->id = 1;
- $notifiable = m::mock();
- $events = m::mock(Dispatcher::class);
- $events->shouldReceive('dispatch')->once()->with(m::type(BroadcastNotificationCreated::class));
- $channel = new BroadcastChannel($events);
- $channel->send($notifiable, $notification);
- }
- public function testNotificationIsBroadcastedOnCustomChannels()
- {
- $notification = new CustomChannelsTestNotification;
- $notification->id = 1;
- $notifiable = m::mock();
- $event = new BroadcastNotificationCreated(
- $notifiable, $notification, $notification->toArray($notifiable)
- );
- $channels = $event->broadcastOn();
- $this->assertEquals(new PrivateChannel('custom-channel'), $channels[0]);
- }
- public function testNotificationIsBroadcastedWithCustomEventName()
- {
- $notification = new CustomEventNameTestNotification;
- $notification->id = 1;
- $notifiable = m::mock();
- $event = new BroadcastNotificationCreated(
- $notifiable, $notification, $notification->toArray($notifiable)
- );
- $eventName = $event->broadcastType();
- $this->assertSame('custom.type', $eventName);
- }
- public function testNotificationIsBroadcastedWithCustomDataType()
- {
- $notification = new CustomEventNameTestNotification;
- $notification->id = 1;
- $notifiable = m::mock();
- $event = new BroadcastNotificationCreated(
- $notifiable, $notification, $notification->toArray($notifiable)
- );
- $data = $event->broadcastWith();
- $this->assertSame('custom.type', $data['type']);
- }
- public function testNotificationIsBroadcastedNow()
- {
- $notification = new TestNotificationBroadCastedNow;
- $notification->id = 1;
- $notifiable = m::mock();
- $events = m::mock(Dispatcher::class);
- $events->shouldReceive('dispatch')->once()->with(m::on(function ($event) {
- return $event->connection === 'sync';
- }));
- $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
- {
- 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')];
- }
- }
- class CustomEventNameTestNotification extends Notification
- {
- public function toArray($notifiable)
- {
- return ['invoice_id' => 1];
- }
- public function broadcastType()
- {
- return 'custom.type';
- }
- }
- class TestNotificationBroadCastedNow extends Notification
- {
- public function toArray($notifiable)
- {
- return ['invoice_id' => 1];
- }
- 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'];
- }
- }
|