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] Allow for closure reflection on all MailFake assertions #38328

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 30 additions & 10 deletions src/Illuminate/Support/Testing/Fakes/MailFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ class MailFake implements Factory, Mailer, MailQueue
*/
public function assertSent($mailable, $callback = null)
{
if ($mailable instanceof Closure) {
[$mailable, $callback] = [$this->firstClosureParameterType($mailable), $mailable];
}
[$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);

if (is_numeric($callback)) {
return $this->assertSentTimes($mailable, $callback);
Expand Down Expand Up @@ -85,12 +83,14 @@ protected function assertSentTimes($mailable, $times = 1)
/**
* Determine if a mailable was not sent based on a truth-test callback.
*
* @param string $mailable
* @param string|\Closure $mailable
* @param callable|null $callback
* @return void
*/
public function assertNotSent($mailable, $callback = null)
{
[$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);

PHPUnit::assertCount(
0, $this->sent($mailable, $callback),
"The unexpected [{$mailable}] mailable was sent."
Expand Down Expand Up @@ -120,9 +120,7 @@ public function assertNothingSent()
*/
public function assertQueued($mailable, $callback = null)
{
if ($mailable instanceof Closure) {
[$mailable, $callback] = [$this->firstClosureParameterType($mailable), $mailable];
}
[$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);

if (is_numeric($callback)) {
return $this->assertQueuedTimes($mailable, $callback);
Expand Down Expand Up @@ -154,12 +152,14 @@ protected function assertQueuedTimes($mailable, $times = 1)
/**
* Determine if a mailable was not queued based on a truth-test callback.
*
* @param string $mailable
* @param \Closure|string $mailable
driesvints marked this conversation as resolved.
Show resolved Hide resolved
* @param callable|null $callback
* @return void
*/
public function assertNotQueued($mailable, $callback = null)
{
[$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);

PHPUnit::assertCount(
0, $this->queued($mailable, $callback),
"The unexpected [{$mailable}] mailable was queued."
Expand All @@ -183,12 +183,14 @@ public function assertNothingQueued()
/**
* Get all of the mailables matching a truth-test callback.
*
* @param string $mailable
* @param string|\Closure $mailable
* @param callable|null $callback
* @return \Illuminate\Support\Collection
*/
public function sent($mailable, $callback = null)
{
[$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);

if (! $this->hasSent($mailable)) {
return collect();
}
Expand Down Expand Up @@ -216,12 +218,14 @@ public function hasSent($mailable)
/**
* Get all of the queued mailables matching a truth-test callback.
*
* @param string $mailable
* @param string|\Closure $mailable
* @param callable|null $callback
* @return \Illuminate\Support\Collection
*/
public function queued($mailable, $callback = null)
{
[$mailable, $callback] = $this->prepareMailableAndCallback($mailable, $callback);

if (! $this->hasQueued($mailable)) {
return collect();
}
Expand Down Expand Up @@ -386,4 +390,20 @@ public function failures()
{
return [];
}

/**
* Infer mailable class using reflection if a typehinted closure is passed to assertion.
*
* @param string|\Closure $mailable
* @param callable|null $callback
* @return array
*/
protected function prepareMailableAndCallback($mailable, $callback)
{
if ($mailable instanceof Closure) {
return [$this->firstClosureParameterType($mailable), $mailable];
}

return [$mailable, $callback];
}
}
16 changes: 16 additions & 0 deletions tests/Support/SupportTestingMailFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ public function testAssertNotSent()
}
}

public function testAssertNotSentWithClosure()
{
$callback = function (MailableStub $mail) {
return $mail->hasTo('[email protected]');
};

$this->fake->assertNotSent($callback);

$this->fake->to('[email protected]')->send($this->mailable);

$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessageMatches('/The unexpected \['.preg_quote(MailableStub::class, '/').'\] mailable was sent./m');

$this->fake->assertNotSent($callback);
}

public function testAssertSentTimes()
{
$this->fake->to('[email protected]')->send($this->mailable);
Expand Down