Skip to content
32 changes: 26 additions & 6 deletions src/Support/Testing/Fakes/MailFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class MailFake extends \Illuminate\Support\Testing\Fakes\MailFake
protected function mailablesOf($type)
{
return collect($this->mailables)->filter(function ($mailable) use ($type) {
return $mailable->view === $type;
return $mailable instanceof $type ||
($mailable instanceof Mailable && ($mailable->view === $type || $mailable->textView === $type));
});
}

Expand All @@ -26,7 +27,8 @@ protected function mailablesOf($type)
protected function queuedMailablesOf($type)
{
return collect($this->queuedMailables)->filter(function ($mailable) use ($type) {
return $mailable->view === $type;
return $mailable instanceof $type ||
($mailable instanceof Mailable && ($mailable->view === $type || $mailable->textView === $type));
});
}

Expand Down Expand Up @@ -76,12 +78,30 @@ public function queue($view, $data = null, $callback = null, $queue = null)
*/
public function buildMailable($view, $data, $callback, $queued = false)
{
$html = $text = null;
$mailable = new Mailable;

if ($queued) {
$mailable->view($view)->withSerializedData($data);
} else {
$mailable->view($view, $data);
if (is_string($view)) {
$html = $view;
} else if (is_array($view)) {
$html = array_get($view, 0, array_get($view, 'html'));
$text = array_get($view, 1, array_get($view, 'text'));
}

if ($html) {
if ($queued) {
$mailable->view($html)->withSerializedData($data);
} else {
$mailable->view($html, $data);
}
}

if ($text) {
if ($queued) {
$mailable->text($text)->withSerializedData($data);
} else {
$mailable->text($text, $data);
}
}

if ($callback !== null) {
Expand Down
75 changes: 66 additions & 9 deletions tests/Support/MailFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,100 @@ class_alias('\Winter\Storm\Support\Facades\Mail', 'Mail');
}
Mail::swap(new MailFake());

$this->view = 'mail-test-view';
$this->recipient = 'fake@localhost';
$this->subject = 'MailFake test';
}

public function testSend()
{
Mail::send($this->view, [], function ($mailer) {
$view = 'mail-test-view';

Mail::send($view, [], function ($mailer) {
$mailer->to($this->recipient);
$mailer->subject($this->subject);
});
Mail::assertSent($this->view, 1);
Mail::assertSent($view, 1);

Mail::assertSent($this->view, function ($mailer) {
Mail::assertSent($view, function ($mailer) {
return $mailer->hasTo($this->recipient);
});

Mail::assertSent($this->view, function ($mailer) {
Mail::assertSent($view, function ($mailer) {
return $mailer->subject === $this->subject;
});
}

public function testQueue()
{
Mail::queue($this->view, [], function ($mailer) {
$view = 'mail-test-queued-view';

Mail::queue($view, [], function ($mailer) {
$mailer->to($this->recipient);
$mailer->subject($this->subject);
});

Mail::assertQueued($this->view, 1);
Mail::assertQueued($view, 1);

Mail::assertQueued($this->view, function ($mailer) {
Mail::assertQueued($view, function ($mailer) {
return $mailer->hasTo($this->recipient);
});

Mail::assertQueued($this->view, function ($mailer) {
Mail::assertQueued($view, function ($mailer) {
return $mailer->subject === $this->subject;
});
}

public function testIndexedArrayViews()
{
$views = ['html-view', 'plain-view'];
$subject = 'test indexed array views';

$this->arrayTests($views, $subject);
}

public function testNamedArrayViews()
{
$views = ['html' => 'html-view', 'text' => 'plain-view'];
$subject = 'test named array views';

$this->arrayTests($views, $subject);
}

public function testIndexedArrayViews_Queued()
{
$views = ['html-view', 'plain-view'];
$subject = 'test indexed array views queued';

$this->arrayTests($views, $subject, true);
}

public function testNamedArrayViews_Queued()
{
$views = ['html' => 'html-view', 'text' => 'plain-view'];
$subject = 'test named array views queued';

$this->arrayTests($views, $subject, true);
}

public function arrayTests($views, $subject, $queued = false)
{
$sendMethod = $queued ? 'queue' : 'send';
$assertMethod = $queued ? 'assertQueued' : 'assertSent';

Mail::{$sendMethod}($views, [], function ($mailer) use ($subject) {
$mailer->to($this->recipient);
$mailer->subject = $subject;
});

foreach (array_values($views) as $view) {
Mail::{$assertMethod}($view, 1);

Mail::{$assertMethod}($view, function ($mailer) {
return $mailer->hasTo($this->recipient);
});
Mail::{$assertMethod}($view, function ($mailer) use ($subject) {
return $mailer->subject === $subject;
});
}
}
}