NotificationChannelManagerTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace Illuminate\Tests\Notifications;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Container\Container;
  5. use Illuminate\Contracts\Bus\Dispatcher as Bus;
  6. use Illuminate\Contracts\Events\Dispatcher;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Notifications\ChannelManager;
  9. use Illuminate\Notifications\Events\NotificationSending;
  10. use Illuminate\Notifications\Events\NotificationSent;
  11. use Illuminate\Notifications\Notifiable;
  12. use Illuminate\Notifications\Notification;
  13. use Illuminate\Notifications\SendQueuedNotifications;
  14. use Mockery as m;
  15. use PHPUnit\Framework\TestCase;
  16. class NotificationChannelManagerTest extends TestCase
  17. {
  18. protected function tearDown(): void
  19. {
  20. m::close();
  21. Container::setInstance(null);
  22. }
  23. public function testNotificationCanBeDispatchedToDriver()
  24. {
  25. $container = new Container;
  26. $container->instance('config', ['app.name' => 'Name', 'app.logo' => 'Logo']);
  27. $container->instance(Bus::class, $bus = m::mock());
  28. $container->instance(Dispatcher::class, $events = m::mock());
  29. Container::setInstance($container);
  30. $manager = m::mock(ChannelManager::class.'[driver]', [$container]);
  31. $manager->shouldReceive('driver')->andReturn($driver = m::mock());
  32. $events->shouldReceive('until')->with(m::type(NotificationSending::class))->andReturn(true);
  33. $driver->shouldReceive('send')->once();
  34. $events->shouldReceive('dispatch')->with(m::type(NotificationSent::class));
  35. $manager->send(new NotificationChannelManagerTestNotifiable, new NotificationChannelManagerTestNotification);
  36. }
  37. public function testNotificationNotSentOnHalt()
  38. {
  39. $container = new Container;
  40. $container->instance('config', ['app.name' => 'Name', 'app.logo' => 'Logo']);
  41. $container->instance(Bus::class, $bus = m::mock());
  42. $container->instance(Dispatcher::class, $events = m::mock());
  43. Container::setInstance($container);
  44. $manager = m::mock(ChannelManager::class.'[driver]', [$container]);
  45. $events->shouldReceive('until')->once()->with(m::type(NotificationSending::class))->andReturn(false);
  46. $events->shouldReceive('until')->with(m::type(NotificationSending::class))->andReturn(true);
  47. $manager->shouldReceive('driver')->once()->andReturn($driver = m::mock());
  48. $driver->shouldReceive('send')->once();
  49. $events->shouldReceive('dispatch')->with(m::type(NotificationSent::class));
  50. $manager->send([new NotificationChannelManagerTestNotifiable], new NotificationChannelManagerTestNotificationWithTwoChannels);
  51. }
  52. public function testNotificationNotSentWhenCancelled()
  53. {
  54. $container = new Container;
  55. $container->instance('config', ['app.name' => 'Name', 'app.logo' => 'Logo']);
  56. $container->instance(Bus::class, $bus = m::mock());
  57. $container->instance(Dispatcher::class, $events = m::mock());
  58. Container::setInstance($container);
  59. $manager = m::mock(ChannelManager::class.'[driver]', [$container]);
  60. $events->shouldReceive('until')->with(m::type(NotificationSending::class))->andReturn(true);
  61. $manager->shouldNotReceive('driver');
  62. $events->shouldNotReceive('dispatch');
  63. $manager->send([new NotificationChannelManagerTestNotifiable], new NotificationChannelManagerTestCancelledNotification);
  64. }
  65. public function testNotificationSentWhenNotCancelled()
  66. {
  67. $container = new Container;
  68. $container->instance('config', ['app.name' => 'Name', 'app.logo' => 'Logo']);
  69. $container->instance(Bus::class, $bus = m::mock());
  70. $container->instance(Dispatcher::class, $events = m::mock());
  71. Container::setInstance($container);
  72. $manager = m::mock(ChannelManager::class.'[driver]', [$container]);
  73. $events->shouldReceive('until')->with(m::type(NotificationSending::class))->andReturn(true);
  74. $manager->shouldReceive('driver')->once()->andReturn($driver = m::mock());
  75. $driver->shouldReceive('send')->once();
  76. $events->shouldReceive('dispatch')->once()->with(m::type(NotificationSent::class));
  77. $manager->send([new NotificationChannelManagerTestNotifiable], new NotificationChannelManagerTestNotCancelledNotification);
  78. }
  79. public function testNotificationCanBeQueued()
  80. {
  81. $container = new Container;
  82. $container->instance('config', ['app.name' => 'Name', 'app.logo' => 'Logo']);
  83. $container->instance(Dispatcher::class, $events = m::mock());
  84. $container->instance(Bus::class, $bus = m::mock());
  85. $bus->shouldReceive('dispatch')->with(m::type(SendQueuedNotifications::class));
  86. Container::setInstance($container);
  87. $manager = m::mock(ChannelManager::class.'[driver]', [$container]);
  88. $manager->send([new NotificationChannelManagerTestNotifiable], new NotificationChannelManagerTestQueuedNotification);
  89. }
  90. }
  91. class NotificationChannelManagerTestNotifiable
  92. {
  93. use Notifiable;
  94. }
  95. class NotificationChannelManagerTestNotification extends Notification
  96. {
  97. public function via()
  98. {
  99. return ['test'];
  100. }
  101. public function message()
  102. {
  103. return $this->line('test')->action('Text', 'url');
  104. }
  105. }
  106. class NotificationChannelManagerTestNotificationWithTwoChannels extends Notification
  107. {
  108. public function via()
  109. {
  110. return ['test', 'test2'];
  111. }
  112. public function message()
  113. {
  114. return $this->line('test')->action('Text', 'url');
  115. }
  116. }
  117. class NotificationChannelManagerTestCancelledNotification extends Notification
  118. {
  119. public function via()
  120. {
  121. return ['test'];
  122. }
  123. public function message()
  124. {
  125. return $this->line('test')->action('Text', 'url');
  126. }
  127. public function shouldSend($notifiable, $channel)
  128. {
  129. return false;
  130. }
  131. }
  132. class NotificationChannelManagerTestNotCancelledNotification extends Notification
  133. {
  134. public function via()
  135. {
  136. return ['test'];
  137. }
  138. public function message()
  139. {
  140. return $this->line('test')->action('Text', 'url');
  141. }
  142. public function shouldSend($notifiable, $channel)
  143. {
  144. return true;
  145. }
  146. }
  147. class NotificationChannelManagerTestQueuedNotification extends Notification implements ShouldQueue
  148. {
  149. use Queueable;
  150. public function via()
  151. {
  152. return ['test'];
  153. }
  154. public function message()
  155. {
  156. return $this->line('test')->action('Text', 'url');
  157. }
  158. }