diff --git a/src/Illuminate/Support/Testing/Fakes/NotificationFake.php b/src/Illuminate/Support/Testing/Fakes/NotificationFake.php index 28526d592556..e136a5438aaf 100644 --- a/src/Illuminate/Support/Testing/Fakes/NotificationFake.php +++ b/src/Illuminate/Support/Testing/Fakes/NotificationFake.php @@ -232,9 +232,24 @@ public function sendNow($notifiables, $notification, array $channels = null) $notification->id = Str::uuid()->toString(); } + $notifiableChannels = $channels ?: $notification->via($notifiable); + + if (method_exists($notification, 'shouldSend')) { + $notifiableChannels = array_filter( + $notifiableChannels, + function ($channel) use ($notification, $notifiable) { + return $notification->shouldSend($notifiable, $channel) !== false; + } + ); + + if (empty($notifiableChannels)) { + continue; + } + } + $this->notifications[get_class($notifiable)][$notifiable->getKey()][get_class($notification)][] = [ 'notification' => $notification, - 'channels' => $channels ?: $notification->via($notifiable), + 'channels' => $notifiableChannels, 'notifiable' => $notifiable, 'locale' => $notification->locale ?? $this->locale ?? value(function () use ($notifiable) { if ($notifiable instanceof HasLocalePreference) { diff --git a/tests/Support/SupportTestingNotificationFakeTest.php b/tests/Support/SupportTestingNotificationFakeTest.php index 0774d442f4b0..8c0038d3d6c0 100644 --- a/tests/Support/SupportTestingNotificationFakeTest.php +++ b/tests/Support/SupportTestingNotificationFakeTest.php @@ -144,6 +144,15 @@ public function testAssertSentToWhenNotifiableHasPreferredLocale() return $notifiable === $user && $locale === 'au'; }); } + + public function testAssertSentToWhenNotifiableHasFalsyShouldSend() + { + $user = new LocalizedUserStub; + + $this->fake->send($user, new NotificationWithFalsyShouldSendStub); + + $this->fake->assertNotSentTo($user, NotificationWithFalsyShouldSendStub::class); + } } class NotificationStub extends Notification @@ -154,6 +163,19 @@ public function via($notifiable) } } +class NotificationWithFalsyShouldSendStub extends Notification +{ + public function via($notifiable) + { + return ['mail']; + } + + public function shouldSend($notifiable, $channel) + { + return false; + } +} + class UserStub extends User { //