SendingNotificationsViaAnonymousNotifiableTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Illuminate\Tests\Integration\Notifications;
  3. use Illuminate\Notifications\AnonymousNotifiable;
  4. use Illuminate\Notifications\Notification;
  5. use Illuminate\Support\Facades\Notification as NotificationFacade;
  6. use Illuminate\Support\Testing\Fakes\NotificationFake;
  7. use Orchestra\Testbench\TestCase;
  8. class SendingNotificationsViaAnonymousNotifiableTest extends TestCase
  9. {
  10. public $mailer;
  11. public function testMailIsSent()
  12. {
  13. $notifiable = (new AnonymousNotifiable)
  14. ->route('testchannel', 'enzo')
  15. ->route('anothertestchannel', 'enzo@deepblue.com');
  16. NotificationFacade::send(
  17. $notifiable,
  18. new TestMailNotificationForAnonymousNotifiable
  19. );
  20. $this->assertEquals([
  21. 'enzo', 'enzo@deepblue.com',
  22. ], $_SERVER['__notifiable.route']);
  23. }
  24. public function testFaking()
  25. {
  26. $fake = NotificationFacade::fake();
  27. $this->assertInstanceOf(NotificationFake::class, $fake);
  28. $notifiable = (new AnonymousNotifiable)
  29. ->route('testchannel', 'enzo')
  30. ->route('anothertestchannel', 'enzo@deepblue.com');
  31. NotificationFacade::locale('it')->send(
  32. $notifiable,
  33. new TestMailNotificationForAnonymousNotifiable
  34. );
  35. NotificationFacade::assertSentTo(new AnonymousNotifiable, TestMailNotificationForAnonymousNotifiable::class,
  36. function ($notification, $channels, $notifiable, $locale) {
  37. return $notifiable->routes['testchannel'] === 'enzo' &&
  38. $notifiable->routes['anothertestchannel'] === 'enzo@deepblue.com' &&
  39. $locale === 'it';
  40. }
  41. );
  42. }
  43. }
  44. class TestMailNotificationForAnonymousNotifiable extends Notification
  45. {
  46. public function via($notifiable)
  47. {
  48. return [TestCustomChannel::class, AnotherTestCustomChannel::class];
  49. }
  50. }
  51. class TestCustomChannel
  52. {
  53. public function send($notifiable, $notification)
  54. {
  55. $_SERVER['__notifiable.route'][] = $notifiable->routeNotificationFor('testchannel');
  56. }
  57. }
  58. class AnotherTestCustomChannel
  59. {
  60. public function send($notifiable, $notification)
  61. {
  62. $_SERVER['__notifiable.route'][] = $notifiable->routeNotificationFor('anothertestchannel');
  63. }
  64. }