Test how many times a Laravel Notification was sent | RJS

Test how many times a Laravel Notification was sent

Ralph J. Smit Laravel & PHP-developer.

Until a few months ago I barely did any automated testing on the code I wrote. That changed after I encountered the concept dubbed 'TDD'. Sounds scary, but really isn't. It stands for Test-Driven Development and it basically means that you write an automated test before you actually implement the feature.

The advantage of this is that you don't need to do so much manual testing as before (you can now run a simple command to check whether the test already passes) ánd you've got good test coverage when you're ready. You need to get used to it a little bit, but it feels awesome once you get the hang of it.

One of the special advantages of Laravel are all the fakes you can use in your testing, like Notification::fake() and Http::fake(). (It works great with any facade as well!)

For example, with Laravel notifications you can simply use PHPunit helpers like this:

Notification::fake(); // At the start of every test or in the setUp() method
 
Notification::assertNothingSent();
 
// Tests whether his is at least true once
Notification::assertSentTo(
[$user], OrderConfirmed::class
);

The problem with the second method, Notification::assertSentTo(), is that is only asserts whether there has been at least one notification of this kind. In other words, if the notification has been sent twice, this test still passes. It could very well be that sending a notification twice is not want you want...

In this tutorial I'll show you how to assert that a certain Laravel Notification has been sent only a specific number of times.

How to assert that a Laravel notification was only sent a specific number of times

In order to solve the above problem, we can use the Notification::assertSentToTimes() method and the Notification::assertTimesSent() helpers.

Laravel `Notification::assertSentToTimes()`

The first helper allows you to check whether a specific notification has been sent X times to a specific notifiable (usually a $user).

The method is very similar to assertSentTo, except that it accepts an additional parameter, which is the number of times the notification should have been sent.

Notification::fake(); // At the start of every test or in the setUp() method
 
// Assert that nothing has sent, just to be sure
Notification::assertNothingSent();
 
// Interact with your application
 
// Assert that the notification has been sent once to the user
Notification::assertSentToTimes(
[$user], OrderConfirmed::class, 1
);

Laravel `Notification::assertTimesSent()`

The second helper method, Notification::assertTimesSent(), is similar to the previous assertion, except that it doesn't check the recipient of so-called $notifiable. It just checks whether a specific Notification has been sent X times in total, without looking at the individual recipient.

This is very useful in situations where you send the same notification to multiple users in bulk.

Notification::fake(); // At the start of every test or in the setUp() method
 
// Assert that nothing has sent, just to be sure
Notification::assertNothingSent();
 
// Interact with your application
 
// Assert that this specific notification has been 2 times in total
Notification::assertTimesSent(
OrderConfirmed::class, 2
);

Conclusion

As you've seen, testing notifications in Laravel is really easy, but you just need to use the right tricks.

Have you got any other nice testing tricks that you want to share? Tell me below in the comments👇

Published by Ralph J. Smit on in Laravel. Last updated on 11 March 2022.


Hey!👋 Like what you read?

Every now and then I send out a newsletter with new articles, tutorials and packages, mainly related to PHP in general and Laravel.

Subscribe to the newsletter and stay up to date with all things Laravel, Tailwind, WordPress & PHP. Never miss an update.🙋‍