Skip to content

[9.x] Add NotificationFake::assertNothingSentTo() - #41232

Merged
taylorotwell merged 2 commits into
laravel:9.xfrom
axlon:patch-6
Feb 24, 2022
Merged

[9.x] Add NotificationFake::assertNothingSentTo()#41232
taylorotwell merged 2 commits into
laravel:9.xfrom
axlon:patch-6

Conversation

@axlon

@axlon axlon commented Feb 24, 2022

Copy link
Copy Markdown
Contributor

Right now there is no easy way to assert that no notifications have been sent to a specific notifiable. Laravel does allow you to assert that no notifications have been sent at all, but this isn't always applicable.

This is primarily an issue when a piece of code dispatches various notification types to different notifiables, for example:

$uploader = User::factory()->createOne();
$podcast = Podcast::factory()->for($uploader)->createOne();
$episode = PodcastEpisode::factory()->for($podcast)->createOne();

$subscriber = User::factory()->hasAttached($podcast)->createOne();
$randomUser = User::factory()->createOne();

Bus::dispatchSync(new ProcessPodcast($episode));

Notification::assertSentTo($uploader, EpisodeProcessed::class);
Notification::assertSentTo($subscriber, NewEpisodeAvailable::class);

Notification::assertNotSentTo($randomUser, EpisodeProcessed::class);
Notification::assertNotSentTo($randomUser, NewEpisodeAvailable::class);

The example above is quite simple and only sends 2 notifications, but the number of calls to assertNotSentTo would increase by one for every additional notification type that is dispatched.

This PR aims to simplify testing in these kinds of scenario's by adding Notification::assertNothingSentTo($notifiable)

PS. This PR includes a test for NotificationFake::assertNothingSent(), which appeared to be missing

@taylorotwell
taylorotwell merged commit 29f5cac into laravel:9.x Feb 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants