Skip to content

[7.x] Allow doing truth-test assertions with just a closure - #32626

Merged
taylorotwell merged 9 commits into
laravel:7.xfrom
SjorsO:do-some-reflection
May 1, 2020
Merged

taylorotwell merged 9 commits into
laravel:7.xfrom
SjorsO:do-some-reflection

Conversation

@SjorsO

@SjorsO SjorsO commented May 1, 2020 •

Copy link
Copy Markdown
Contributor

Currently, for all the truth-test assertions, you have to first pass in a class, then a closure. If you want your editor to assist you with auto-completion, you have to type the class twice. Once as the first argument for the assertQueued method, and once as the type hint for the parameter of the closure.

Mail::assertQueued(SubpictureFinishedEmail::class, function (SubpictureFinishedEmail $email) use ($user) {
    return $email->user->id === $user->id;
});

// or with a short closure

Mail::assertQueued(SubpictureFinishedEmail::class, fn (SubpictureFinishedEmail $email) => $email->user->id === $user->id);

This PR, using a little reflection magic, infers the type from the parameter of the closure. This removes the duplication, and allows you to write the assertions like this instead:

Mail::assertQueued(function (SubpictureFinishedEmail $email) use ($user) {
    return $email->user->id === $user->id;
});

// or with a short closure

Mail::assertQueued(fn (SubpictureFinishedEmail $email) => $email->user->id === $user->id);

This isn't a breaking change, all modified methods will currently throw an exception if you give them a closure as the first argument.

@taylorotwell
taylorotwell merged commit 56b11d4 into laravel:7.x May 1, 2020
@taylorotwell

Copy link
Copy Markdown
Member

Nice PR. Thanks ❤️

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