Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Notification assertions respect shouldSend method on notification #38979

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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