NotificationDatabaseChannelTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Illuminate\Tests\Notifications;
  3. use Illuminate\Notifications\Channels\DatabaseChannel;
  4. use Illuminate\Notifications\Messages\DatabaseMessage;
  5. use Illuminate\Notifications\Notification;
  6. use Mockery as m;
  7. use PHPUnit\Framework\TestCase;
  8. class NotificationDatabaseChannelTest extends TestCase
  9. {
  10. protected function tearDown(): void
  11. {
  12. m::close();
  13. }
  14. public function testDatabaseChannelCreatesDatabaseRecordWithProperData()
  15. {
  16. $notification = new NotificationDatabaseChannelTestNotification;
  17. $notification->id = 1;
  18. $notifiable = m::mock();
  19. $notifiable->shouldReceive('routeNotificationFor->create')->with([
  20. 'id' => 1,
  21. 'type' => get_class($notification),
  22. 'data' => ['invoice_id' => 1],
  23. 'read_at' => null,
  24. ]);
  25. $channel = new DatabaseChannel;
  26. $channel->send($notifiable, $notification);
  27. }
  28. public function testCorrectPayloadIsSentToDatabase()
  29. {
  30. $notification = new NotificationDatabaseChannelTestNotification;
  31. $notification->id = 1;
  32. $notifiable = m::mock();
  33. $notifiable->shouldReceive('routeNotificationFor->create')->with([
  34. 'id' => 1,
  35. 'type' => get_class($notification),
  36. 'data' => ['invoice_id' => 1],
  37. 'read_at' => null,
  38. 'something' => 'else',
  39. ]);
  40. $channel = new ExtendedDatabaseChannel;
  41. $channel->send($notifiable, $notification);
  42. }
  43. public function testCustomizeTypeIsSentToDatabase()
  44. {
  45. $notification = new NotificationDatabaseChannelCustomizeTypeTestNotification;
  46. $notification->id = 1;
  47. $notifiable = m::mock();
  48. $notifiable->shouldReceive('routeNotificationFor->create')->with([
  49. 'id' => 1,
  50. 'type' => 'MONTHLY',
  51. 'data' => ['invoice_id' => 1],
  52. 'read_at' => null,
  53. 'something' => 'else',
  54. ]);
  55. $channel = new ExtendedDatabaseChannel;
  56. $channel->send($notifiable, $notification);
  57. }
  58. }
  59. class NotificationDatabaseChannelTestNotification extends Notification
  60. {
  61. public function toDatabase($notifiable)
  62. {
  63. return new DatabaseMessage(['invoice_id' => 1]);
  64. }
  65. }
  66. class NotificationDatabaseChannelCustomizeTypeTestNotification extends Notification
  67. {
  68. public function toDatabase($notifiable)
  69. {
  70. return new DatabaseMessage(['invoice_id' => 1]);
  71. }
  72. public function databaseType()
  73. {
  74. return 'MONTHLY';
  75. }
  76. }
  77. class ExtendedDatabaseChannel extends DatabaseChannel
  78. {
  79. protected function buildPayload($notifiable, Notification $notification)
  80. {
  81. return array_merge(parent::buildPayload($notifiable, $notification), [
  82. 'something' => 'else',
  83. ]);
  84. }
  85. }