diff --git a/src/Support/Testing/Fakes/MailFake.php b/src/Support/Testing/Fakes/MailFake.php index 2a974878b..ddfee4760 100644 --- a/src/Support/Testing/Fakes/MailFake.php +++ b/src/Support/Testing/Fakes/MailFake.php @@ -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)); }); } @@ -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)); }); } @@ -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) { diff --git a/tests/Support/MailFakeTest.php b/tests/Support/MailFakeTest.php index ac658884f..73c99d5eb 100644 --- a/tests/Support/MailFakeTest.php +++ b/tests/Support/MailFakeTest.php @@ -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; + }); + } + } }