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

[5.5] Allow MailFake to assert mailables were queued #20454

Merged
merged 16 commits into from
Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
123 changes: 122 additions & 1 deletion src/Illuminate/Support/Testing/Fakes/MailFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Contracts\Mail\Mailable;
use PHPUnit\Framework\Assert as PHPUnit;
use Illuminate\Contracts\Queue\ShouldQueue;

class MailFake implements Mailer
{
Expand All @@ -15,6 +16,13 @@ class MailFake implements Mailer
*/
protected $mailables = [];

/**
* All of the mailables that have been queued.
*
* @var array
*/
protected $queuedMailables = [];

/**
* Assert if a mailable was sent based on a truth-test callback.
*
Expand Down Expand Up @@ -74,6 +82,65 @@ public function assertNothingSent()
PHPUnit::assertEmpty($this->mailables, 'Mailables were sent unexpectedly.');
}

/**
* Assert if a mailable was queued based on a truth-test callback.
*
* @param string $mailable
* @param callable|int|null $callback
* @return void
*/
public function assertQueued($mailable, $callback = null)
{
if (is_numeric($callback)) {
return $this->assertQueuedTimes($mailable, $callback);
}

PHPUnit::assertTrue(
$this->queued($mailable, $callback)->count() > 0,
"The expected [{$mailable}] mailable was not queued."
);
}

/**
* Assert if a mailable was queued a number of times.
*
* @param string $mailable
* @param int $times
* @return void
*/
protected function assertQueuedTimes($mailable, $times = 1)
{
PHPUnit::assertTrue(
($count = $this->queued($mailable)->count()) === $times,
"The expected [{$mailable}] mailable was queued {$count} times instead of {$times} times."
);
}

/**
* Determine if a mailable was not queued based on a truth-test callback.
*
* @param string $mailable
* @param callable|null $callback
* @return void
*/
public function assertNotQueued($mailable, $callback = null)
{
PHPUnit::assertTrue(
$this->queued($mailable, $callback)->count() === 0,
"The unexpected [{$mailable}] mailable was queued."
);
}

/**
* Assert that no mailables were queued.
*
* @return void
*/
public function assertNothingQueued()
{
PHPUnit::assertEmpty($this->queuedMailables, 'Mailables were queued unexpectedly.');
}

/**
* Get all of the mailables matching a truth-test callback.
*
Expand Down Expand Up @@ -107,6 +174,39 @@ public function hasSent($mailable)
return $this->mailablesOf($mailable)->count() > 0;
}

/**
* Get all of the queued mailables matching a truth-test callback.
*
* @param string $mailable
* @param callable|null $callback
* @return \Illuminate\Support\Collection
*/
public function queued($mailable, $callback = null)
{
if (! $this->hasQueued($mailable)) {
return collect();
}

$callback = $callback ?: function () {
return true;
};

return $this->queuedMailablesOf($mailable)->filter(function ($mailable) use ($callback) {
return $callback($mailable);
});
}

/**
* Determine if the given mailable has been queued.
*
* @param string $mailable
* @return bool
*/
public function hasQueued($mailable)
{
return $this->queuedMailablesOf($mailable)->count() > 0;
}

/**
* Get all of the mailed mailables for a given type.
*
Expand All @@ -120,6 +220,19 @@ protected function mailablesOf($type)
});
}

/**
* Get all of the mailed mailables for a given type.
*
* @param string $type
* @return \Illuminate\Support\Collection
*/
protected function queuedMailablesOf($type)
{
return collect($this->queuedMailables)->filter(function ($mailable) use ($type) {
return $mailable instanceof $type;
});
}

/**
* Begin the process of mailing a mailable class instance.
*
Expand Down Expand Up @@ -168,6 +281,10 @@ public function send($view, array $data = [], $callback = null)
return;
}

if ($view instanceof ShouldQueue) {
return $this->queue($view, $data, $callback);
}

$this->mailables[] = $view;
}

Expand All @@ -182,7 +299,11 @@ public function send($view, array $data = [], $callback = null)
*/
public function queue($view, array $data = [], $callback = null, $queue = null)
{
$this->send($view);
if (! $view instanceof Mailable) {
return;
}

$this->queuedMailables[] = $view;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Testing/Fakes/PendingMailFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ public function sendNow(Mailable $mailable)
*/
public function queue(Mailable $mailable)
{
return $this->sendNow($mailable);
return $this->mailer->queue($mailable);
}
}
58 changes: 57 additions & 1 deletion tests/Support/SupportTestingMailFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Mail\Mailable;
use PHPUnit\Framework\TestCase;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Testing\Fakes\MailFake;

class MailFakeTest extends TestCase
Expand Down Expand Up @@ -51,10 +52,47 @@ public function testAssertSentTimes()
$this->fake->to('taylor@laravel.com')->send($this->mailable);

$this->fake->assertSent(MailableStub::class, 1);

$this->fake->assertSent(MailableStub::class, 2);
}

/**
* @expectedException PHPUnit\Framework\ExpectationFailedException
* @expectedExceptionMessage The expected [Illuminate\Tests\Support\MailableStub] mailable was not queued.
*/
public function testAssertQueued()
{
$this->fake->assertQueued(MailableStub::class);

$this->fake->to('taylor@laravel.com')->queue($this->mailable);

$this->fake->assertQueued(MailableStub::class);
}

/**
* @expectedException PHPUnit\Framework\ExpectationFailedException
* @expectedExceptionMessage The expected [Illuminate\Tests\Support\MailableStub] mailable was queued 2 times instead of 1 times.
*/
public function testAssertQueuedTimes()
{
$this->fake->to('taylor@laravel.com')->queue($this->mailable);
$this->fake->to('taylor@laravel.com')->queue($this->mailable);

$this->fake->assertQueued(MailableStub::class, 1);
$this->fake->assertQueued(MailableStub::class, 2);
}

/**
* @expectedException PHPUnit\Framework\ExpectationFailedException
* @expectedExceptionMessage The expected [Illuminate\Tests\Support\QueueableMailableStub] mailable was not sent.
*/
public function testSendQueuesAMailableThatShouldBeQueued()
{
$this->fake->to('taylor@laravel.com')->send(new QueueableMailableStub);

$this->fake->assertSent(QueueableMailableStub::class);
$this->fake->assertQueued(QueueableMailableStub::class);
}

/**
* @expectedException PHPUnit\Framework\ExpectationFailedException
* @expectedExceptionMessage Mailables were sent unexpectedly.
Expand Down Expand Up @@ -86,3 +124,21 @@ public function build()
->withLastName('Otwell');
}
}

class QueueableMailableStub extends Mailable implements ShouldQueue
{
public $framework = 'Laravel';

protected $version = '5.5';

/**
* Build the message.
*
* @return $this
*/
public function build()
{
$this->with('first_name', 'Taylor')
->withLastName('Otwell');
}
}