NotificationBroadcastChannelTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace Illuminate\Tests\Notifications;
  3. use Illuminate\Broadcasting\PrivateChannel;
  4. use Illuminate\Contracts\Events\Dispatcher;
  5. use Illuminate\Notifications\Channels\BroadcastChannel;
  6. use Illuminate\Notifications\Events\BroadcastNotificationCreated;
  7. use Illuminate\Notifications\Messages\BroadcastMessage;
  8. use Illuminate\Notifications\Notification;
  9. use Mockery as m;
  10. use PHPUnit\Framework\TestCase;
  11. class NotificationBroadcastChannelTest extends TestCase
  12. {
  13. protected function tearDown(): void
  14. {
  15. m::close();
  16. }
  17. public function testDatabaseChannelCreatesDatabaseRecordWithProperData()
  18. {
  19. $notification = new NotificationBroadcastChannelTestNotification;
  20. $notification->id = 1;
  21. $notifiable = m::mock();
  22. $events = m::mock(Dispatcher::class);
  23. $events->shouldReceive('dispatch')->once()->with(m::type(BroadcastNotificationCreated::class));
  24. $channel = new BroadcastChannel($events);
  25. $channel->send($notifiable, $notification);
  26. }
  27. public function testNotificationIsBroadcastedOnCustomChannels()
  28. {
  29. $notification = new CustomChannelsTestNotification;
  30. $notification->id = 1;
  31. $notifiable = m::mock();
  32. $event = new BroadcastNotificationCreated(
  33. $notifiable, $notification, $notification->toArray($notifiable)
  34. );
  35. $channels = $event->broadcastOn();
  36. $this->assertEquals(new PrivateChannel('custom-channel'), $channels[0]);
  37. }
  38. public function testNotificationIsBroadcastedWithCustomEventName()
  39. {
  40. $notification = new CustomEventNameTestNotification;
  41. $notification->id = 1;
  42. $notifiable = m::mock();
  43. $event = new BroadcastNotificationCreated(
  44. $notifiable, $notification, $notification->toArray($notifiable)
  45. );
  46. $eventName = $event->broadcastType();
  47. $this->assertSame('custom.type', $eventName);
  48. }
  49. public function testNotificationIsBroadcastedWithCustomDataType()
  50. {
  51. $notification = new CustomEventNameTestNotification;
  52. $notification->id = 1;
  53. $notifiable = m::mock();
  54. $event = new BroadcastNotificationCreated(
  55. $notifiable, $notification, $notification->toArray($notifiable)
  56. );
  57. $data = $event->broadcastWith();
  58. $this->assertSame('custom.type', $data['type']);
  59. }
  60. public function testNotificationIsBroadcastedNow()
  61. {
  62. $notification = new TestNotificationBroadCastedNow;
  63. $notification->id = 1;
  64. $notifiable = m::mock();
  65. $events = m::mock(Dispatcher::class);
  66. $events->shouldReceive('dispatch')->once()->with(m::on(function ($event) {
  67. return $event->connection === 'sync';
  68. }));
  69. $channel = new BroadcastChannel($events);
  70. $channel->send($notifiable, $notification);
  71. }
  72. public function testNotificationIsBroadcastedWithCustomAdditionalPayload()
  73. {
  74. $notification = new CustomBroadcastWithTestNotification;
  75. $notification->id = 1;
  76. $notifiable = m::mock();
  77. $event = new BroadcastNotificationCreated(
  78. $notifiable, $notification, $notification->toArray($notifiable)
  79. );
  80. $data = $event->broadcastWith();
  81. $this->assertArrayHasKey('additional', $data);
  82. }
  83. }
  84. class NotificationBroadcastChannelTestNotification extends Notification
  85. {
  86. public function toArray($notifiable)
  87. {
  88. return ['invoice_id' => 1];
  89. }
  90. }
  91. class CustomChannelsTestNotification extends Notification
  92. {
  93. public function toArray($notifiable)
  94. {
  95. return ['invoice_id' => 1];
  96. }
  97. public function broadcastOn()
  98. {
  99. return [new PrivateChannel('custom-channel')];
  100. }
  101. }
  102. class CustomEventNameTestNotification extends Notification
  103. {
  104. public function toArray($notifiable)
  105. {
  106. return ['invoice_id' => 1];
  107. }
  108. public function broadcastType()
  109. {
  110. return 'custom.type';
  111. }
  112. }
  113. class TestNotificationBroadCastedNow extends Notification
  114. {
  115. public function toArray($notifiable)
  116. {
  117. return ['invoice_id' => 1];
  118. }
  119. public function toBroadcast()
  120. {
  121. return (new BroadcastMessage([]))->onConnection('sync');
  122. }
  123. }
  124. class CustomBroadcastWithTestNotification extends Notification
  125. {
  126. public function toArray($notifiable)
  127. {
  128. return ['invoice_id' => 1];
  129. }
  130. public function broadcastWith()
  131. {
  132. return ['id' => 1, 'type' => 'custom', 'additional' => 'custom'];
  133. }
  134. }