Skip to content

Commit

Permalink
[8.x] Notification assertions respect shouldSend method on notifica…
Browse files Browse the repository at this point in the history
…tion (#38979)

* added `shouldSend` support to notification assertions

* changed count to empty
  • Loading branch information
romalytvynenko authored Sep 27, 2021
1 parent 02da36c commit 314de9c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Illuminate/Support/Testing/Fakes/NotificationFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 22 additions & 0 deletions tests/Support/SupportTestingNotificationFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
{
//
Expand Down

0 comments on commit 314de9c

Please sign in to comment.