Skip to content

Commit

Permalink
[8.x] Allow for closure reflection on all MailFake assertions (#38328)
Browse files Browse the repository at this point in the history
* [8.x] Allow for closure reflection on all MailFake assertions

* Minor refactor

* Update src/Illuminate/Support/Testing/Fakes/MailFake.php

Co-authored-by: Dries Vints <[email protected]>
  • Loading branch information
inxilpro and driesvints authored Aug 10, 2021
1 parent bc1188d commit ebe072a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
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 string|\Closure $mailable
* @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

0 comments on commit ebe072a

Please sign in to comment.