From 216381b5de4eb1154a98f61a5e56c9020458b3aa Mon Sep 17 00:00:00 2001 From: Roman Lytvynenko Date: Mon, 27 Sep 2021 15:43:50 +0300 Subject: [PATCH 1/2] added `shouldSend` support to notification assertions --- .../Testing/Fakes/NotificationFake.php | 17 +++++++++++++- .../SupportTestingNotificationFakeTest.php | 22 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Testing/Fakes/NotificationFake.php b/src/Illuminate/Support/Testing/Fakes/NotificationFake.php index 28526d592556..27c0df8428ff 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 (! count($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 { // From 0561d37eb551ef8568cbad5717936efefc7f93e6 Mon Sep 17 00:00:00 2001 From: Roman Lytvynenko Date: Mon, 27 Sep 2021 15:46:09 +0300 Subject: [PATCH 2/2] changed count to empty --- src/Illuminate/Support/Testing/Fakes/NotificationFake.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Testing/Fakes/NotificationFake.php b/src/Illuminate/Support/Testing/Fakes/NotificationFake.php index 27c0df8428ff..e136a5438aaf 100644 --- a/src/Illuminate/Support/Testing/Fakes/NotificationFake.php +++ b/src/Illuminate/Support/Testing/Fakes/NotificationFake.php @@ -242,7 +242,7 @@ function ($channel) use ($notification, $notifiable) { } ); - if (! count($notifiableChannels)) { + if (empty($notifiableChannels)) { continue; } }