NotificationSendQueuedNotificationTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Illuminate\Tests\Notifications;
  3. use Illuminate\Contracts\Database\ModelIdentifier;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Notifications\AnonymousNotifiable;
  6. use Illuminate\Notifications\ChannelManager;
  7. use Illuminate\Notifications\Notifiable;
  8. use Illuminate\Notifications\SendQueuedNotifications;
  9. use Illuminate\Support\Collection;
  10. use Mockery as m;
  11. use PHPUnit\Framework\TestCase;
  12. class NotificationSendQueuedNotificationTest extends TestCase
  13. {
  14. protected function tearDown(): void
  15. {
  16. m::close();
  17. }
  18. public function testNotificationsCanBeSent()
  19. {
  20. $job = new SendQueuedNotifications('notifiables', 'notification');
  21. $manager = m::mock(ChannelManager::class);
  22. $manager->shouldReceive('sendNow')->once()->withArgs(function ($notifiables, $notification, $channels) {
  23. return $notifiables instanceof Collection && $notifiables->toArray() === ['notifiables']
  24. && $notification === 'notification'
  25. && $channels === null;
  26. });
  27. $job->handle($manager);
  28. }
  29. public function testSerializationOfNotifiableModel()
  30. {
  31. $identifier = new ModelIdentifier(NotifiableUser::class, [null], [], null);
  32. $serializedIdentifier = serialize($identifier);
  33. $job = new SendQueuedNotifications(new NotifiableUser, 'notification');
  34. $serialized = serialize($job);
  35. $this->assertStringContainsString($serializedIdentifier, $serialized);
  36. }
  37. public function testSerializationOfNormalNotifiable()
  38. {
  39. $notifiable = new AnonymousNotifiable;
  40. $serializedNotifiable = serialize($notifiable);
  41. $job = new SendQueuedNotifications($notifiable, 'notification');
  42. $serialized = serialize($job);
  43. $this->assertStringContainsString($serializedNotifiable, $serialized);
  44. }
  45. }
  46. class NotifiableUser extends Model
  47. {
  48. use Notifiable;
  49. public $table = 'users';
  50. public $timestamps = false;
  51. }