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

Add assertSentTo shorthand #52083

Merged
merged 4 commits into from
Jul 15, 2024
Merged

Conversation

jasonmccreary
Copy link
Contributor

An easy, but strong assertion when testing mail is to assert that a particular mailable was sent to a specific email address.

Currently to do this, you need to write out this logic and wrap it in a closure. The following is as trim as you can get:

Mail::assertSent(MeetingMinutes::class, fn ($mail) => $mail->hasTo($user->email));

With this PR, you may now simply write:

Mail::assertSentTo(MeetingMinutes::class, $user->email);

In addition, you may also pass an array as the second argument to assert a mailable was sent to multiple users. For example:

Mail::assertSentTo(MeetingMinutes::class, ['taylor@...', 'dries@...', 'jess@...']);

An assertQueuedTo is included for symmetry.

@jasonmccreary
Copy link
Contributor Author

If merged, I'm glad to submit a fast-follow PR to add the negative assertions (i.e. assertNotSentTo and assertNotQueuedTo)

@taylorotwell
Copy link
Member

Could you just see if the second argument given to assertSent is a string?

@taylorotwell taylorotwell marked this pull request as draft July 10, 2024 18:54
@jasonmccreary
Copy link
Contributor Author

jasonmccreary commented Jul 10, 2024

@taylorotwell, you could. This method is already overloaded though. So be able to pass a closure, int, string, array, or null feels bloated, imo.

@jasonmccreary
Copy link
Contributor Author

jasonmccreary commented Jul 14, 2024

@taylorotwell, I realized assertSentTo was an old assertion (5.4), so have unified everything under assertSent and assertQueued. It has 3 different paths based on the second argument, but the code is straightforward and tested.

I also added this feature to assertNotSent and assertNotQueued.

@jasonmccreary jasonmccreary marked this pull request as ready for review July 14, 2024 13:45
@jasonmccreary
Copy link
Contributor Author

jasonmccreary commented Jul 14, 2024

The failing tests relate to RedisQueue. Given these are failing on several other PRs, I'm going to assume this is CI related and not my code. 😅

@taylorotwell taylorotwell merged commit ddda68e into laravel:11.x Jul 15, 2024
12 of 28 checks passed
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.

None yet

2 participants