From 8ea6caa5ef077bbf82844631fbaeb24762d1e6f4 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Mon, 14 Dec 2020 15:03:29 -0500 Subject: [PATCH 1/7] MailFake now works with array views --- src/Mail/Mailable.php | 6 ++++++ src/Support/Testing/Fakes/MailFake.php | 28 +++++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/Mail/Mailable.php b/src/Mail/Mailable.php index a54865617..25b9fd5c7 100644 --- a/src/Mail/Mailable.php +++ b/src/Mail/Mailable.php @@ -77,4 +77,10 @@ protected function buildSubject($message) } return $this; } + + public function __toString() + { + return get_class($this); + } + } diff --git a/src/Support/Testing/Fakes/MailFake.php b/src/Support/Testing/Fakes/MailFake.php index 1499a3c6d..85f89fe67 100644 --- a/src/Support/Testing/Fakes/MailFake.php +++ b/src/Support/Testing/Fakes/MailFake.php @@ -13,7 +13,7 @@ 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->view === $type || $mailable->textView === $type; }); } @@ -76,12 +76,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) { From 12be3d5c633b58b256226806a1fc08f0cadbb770 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 15 Dec 2020 10:23:39 -0500 Subject: [PATCH 2/7] fix queuedMailablesOf() for array views --- src/Support/Testing/Fakes/MailFake.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Support/Testing/Fakes/MailFake.php b/src/Support/Testing/Fakes/MailFake.php index 85f89fe67..6bcc55ce1 100644 --- a/src/Support/Testing/Fakes/MailFake.php +++ b/src/Support/Testing/Fakes/MailFake.php @@ -26,7 +26,7 @@ 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->view === $type || $mailable->textView === $type; }); } From f847924fb2c533a9d3d2ec1d028f524b60a66708 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 15 Dec 2020 10:55:38 -0500 Subject: [PATCH 3/7] add array views tests --- tests/Support/MailFakeTest.php | 75 ++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 9 deletions(-) diff --git a/tests/Support/MailFakeTest.php b/tests/Support/MailFakeTest.php index 3bf8585a2..63d563bb5 100644 --- a/tests/Support/MailFakeTest.php +++ b/tests/Support/MailFakeTest.php @@ -18,43 +18,100 @@ class_alias('\October\Rain\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) { + $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) { + return $mailer->subject === $subject; + }); + } + } } From 46f4f8cb5b7e6d91d080b73db97a0b659c48665e Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 15 Dec 2020 10:58:13 -0500 Subject: [PATCH 4/7] remove unused code --- src/Mail/Mailable.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Mail/Mailable.php b/src/Mail/Mailable.php index 25b9fd5c7..a54865617 100644 --- a/src/Mail/Mailable.php +++ b/src/Mail/Mailable.php @@ -77,10 +77,4 @@ protected function buildSubject($message) } return $this; } - - public function __toString() - { - return get_class($this); - } - } From 958b839a00afd84a13cd895c2d0c05c11c50ac87 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 15 Dec 2020 11:06:58 -0500 Subject: [PATCH 5/7] pass subject variable to the closure --- tests/Support/MailFakeTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Support/MailFakeTest.php b/tests/Support/MailFakeTest.php index 63d563bb5..0a9c5fcd4 100644 --- a/tests/Support/MailFakeTest.php +++ b/tests/Support/MailFakeTest.php @@ -93,12 +93,12 @@ public function testNamedArrayViews_Queued() $this->arrayTests($views, $subject, true); } - public function arrayTests($views, $subject, $queued=false) + public function arrayTests($views, $subject, $queued = false) { $sendMethod = $queued ? 'queue' : 'send'; $assertMethod = $queued ? 'assertQueued' : 'assertSent'; - Mail::{$sendMethod}($views, [], function ($mailer) { + Mail::{$sendMethod}($views, [], function ($mailer) use ($subject) { $mailer->to($this->recipient); $mailer->subject = $subject; }); @@ -109,7 +109,7 @@ public function arrayTests($views, $subject, $queued=false) Mail::{$assertMethod}($view, function ($mailer) { return $mailer->hasTo($this->recipient); }); - Mail::{$assertMethod}($view, function ($mailer) { + Mail::{$assertMethod}($view, function ($mailer) use ($subject) { return $mailer->subject === $subject; }); } From 8e04c7932c62bc7587f846ab76ae637c0235be87 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Thu, 25 Mar 2021 09:54:30 -0400 Subject: [PATCH 6/7] make sure we access the properties of a Mailable object --- src/Support/Testing/Fakes/MailFake.php | 6 ++++-- tests/Database/Attach/ResizerTest.php | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Support/Testing/Fakes/MailFake.php b/src/Support/Testing/Fakes/MailFake.php index d155254c7..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 instanceof $type || $mailable->view === $type || $mailable->textView === $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 instanceof $type || $mailable->view === $type || $mailable->textView === $type; + return $mailable instanceof $type || + ($mailable instanceof Mailable && ($mailable->view === $type || $mailable->textView === $type)); }); } diff --git a/tests/Database/Attach/ResizerTest.php b/tests/Database/Attach/ResizerTest.php index 99087d81e..6881dc25e 100644 --- a/tests/Database/Attach/ResizerTest.php +++ b/tests/Database/Attach/ResizerTest.php @@ -4,7 +4,7 @@ class ResizerTest extends TestCase { - use \AssertGD\GDAssertTrait; + #use \AssertGD\GDAssertTrait; // Controls whether the test compares against existing fixtures or generates new ones // Should be false when we are actually running the tests, true when we are generating fixtures From 51547ee82715d6ed36e943c4fdbfccec32e81aa6 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Thu, 25 Mar 2021 10:13:48 -0400 Subject: [PATCH 7/7] add the GDAssertTrait back --- tests/Database/Attach/ResizerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Database/Attach/ResizerTest.php b/tests/Database/Attach/ResizerTest.php index 6881dc25e..99087d81e 100644 --- a/tests/Database/Attach/ResizerTest.php +++ b/tests/Database/Attach/ResizerTest.php @@ -4,7 +4,7 @@ class ResizerTest extends TestCase { - #use \AssertGD\GDAssertTrait; + use \AssertGD\GDAssertTrait; // Controls whether the test compares against existing fixtures or generates new ones // Should be false when we are actually running the tests, true when we are generating fixtures