NotificationSenderTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Illuminate\Tests\Notifications;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Bus\Dispatcher as BusDispatcher;
  5. use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Notifications\AnonymousNotifiable;
  8. use Illuminate\Notifications\ChannelManager;
  9. use Illuminate\Notifications\Notifiable;
  10. use Illuminate\Notifications\Notification;
  11. use Illuminate\Notifications\NotificationSender;
  12. use Mockery as m;
  13. use PHPUnit\Framework\TestCase;
  14. class NotificationSenderTest extends TestCase
  15. {
  16. protected function tearDown(): void
  17. {
  18. parent::tearDown();
  19. m::close();
  20. }
  21. public function testItCanSendQueuedNotificationsWithAStringVia()
  22. {
  23. $notifiable = m::mock(Notifiable::class);
  24. $manager = m::mock(ChannelManager::class);
  25. $bus = m::mock(BusDispatcher::class);
  26. $bus->shouldReceive('dispatch');
  27. $events = m::mock(EventDispatcher::class);
  28. $sender = new NotificationSender($manager, $bus, $events);
  29. $sender->send($notifiable, new DummyQueuedNotificationWithStringVia);
  30. }
  31. public function testItCanSendNotificationsWithAnEmptyStringVia()
  32. {
  33. $notifiable = new AnonymousNotifiable;
  34. $manager = m::mock(ChannelManager::class);
  35. $bus = m::mock(BusDispatcher::class);
  36. $bus->shouldNotReceive('dispatch');
  37. $events = m::mock(EventDispatcher::class);
  38. $sender = new NotificationSender($manager, $bus, $events);
  39. $sender->sendNow($notifiable, new DummyNotificationWithEmptyStringVia);
  40. }
  41. public function testItCannotSendNotificationsViaDatabaseForAnonymousNotifiables()
  42. {
  43. $notifiable = new AnonymousNotifiable;
  44. $manager = m::mock(ChannelManager::class);
  45. $bus = m::mock(BusDispatcher::class);
  46. $bus->shouldNotReceive('dispatch');
  47. $events = m::mock(EventDispatcher::class);
  48. $sender = new NotificationSender($manager, $bus, $events);
  49. $sender->sendNow($notifiable, new DummyNotificationWithDatabaseVia);
  50. }
  51. }
  52. class DummyQueuedNotificationWithStringVia extends Notification implements ShouldQueue
  53. {
  54. use Queueable;
  55. /**
  56. * Get the notification channels.
  57. *
  58. * @param mixed $notifiable
  59. * @return array|string
  60. */
  61. public function via($notifiable)
  62. {
  63. return 'mail';
  64. }
  65. }
  66. class DummyNotificationWithEmptyStringVia extends Notification
  67. {
  68. use Queueable;
  69. /**
  70. * Get the notification channels.
  71. *
  72. * @param mixed $notifiable
  73. * @return array|string
  74. */
  75. public function via($notifiable)
  76. {
  77. return '';
  78. }
  79. }
  80. class DummyNotificationWithDatabaseVia extends Notification
  81. {
  82. use Queueable;
  83. /**
  84. * Get the notification channels.
  85. *
  86. * @param mixed $notifiable
  87. * @return array|string
  88. */
  89. public function via($notifiable)
  90. {
  91. return 'database';
  92. }
  93. }