From a42277459092ae87f5ac11271f969625509f7f49 Mon Sep 17 00:00:00 2001 From: David Hemphill Date: Wed, 12 Oct 2016 17:32:47 -0500 Subject: [PATCH] Add ability to get subjects and make assertions against a group of them (#54) --- src/MailThief.php | 5 +++++ src/Testing/InteractsWithMail.php | 9 +++++++++ tests/InteractsWithMailTest.php | 26 ++++++++++++++++++++++++++ tests/MailThiefTest.php | 23 +++++++++++++++++++---- 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/MailThief.php b/src/MailThief.php index 7a8e922..16b4932 100644 --- a/src/MailThief.php +++ b/src/MailThief.php @@ -164,4 +164,9 @@ public function alwaysFrom($address, $name = null) { $this->from = ['address' => $address, 'name' => $name]; } + + public function subjects() + { + return $this->messages->pluck('subject'); + } } diff --git a/src/Testing/InteractsWithMail.php b/src/Testing/InteractsWithMail.php index 23790f0..92d0a27 100644 --- a/src/Testing/InteractsWithMail.php +++ b/src/Testing/InteractsWithMail.php @@ -93,6 +93,15 @@ public function seeHeaders($name, $value = null) return $this; } + protected function seeInSubjects($subjects) + { + $subjects = (array) $subjects; + + foreach ($subjects as $subject) { + $this->assertTrue(in_array($subject, $this->mailer->subjects()->all())); + } + } + protected function seeMessage() { $this->assertNotNull( diff --git a/tests/InteractsWithMailTest.php b/tests/InteractsWithMailTest.php index a528103..0fe853c 100644 --- a/tests/InteractsWithMailTest.php +++ b/tests/InteractsWithMailTest.php @@ -52,6 +52,32 @@ public function test_see_message_with_subject() $this->seeMessageWithSubject('foo'); } + public function test_see_in_subjects() + { + $mailer = $this->mailer = $this->getMailThief(); + + collect(['foo@bar.tld', 'baz@qux.tld'])->each(function ($email) use ($mailer) { + $mailer->send('example-view', [], function ($m) use ($email) { + $m->subject("Message for {$email}"); + }); + }); + + $this->seeInSubjects("Message for baz@qux.tld"); + } + + public function test_see_in_subjects_with_array() + { + $mailer = $this->mailer = $this->getMailThief(); + + collect(['Taylor Otwell', 'Adam Wathan'])->each(function ($name) use ($mailer) { + $mailer->send('example-view', [], function ($m) use ($name) { + $m->subject("Message for {$name}"); + }); + }); + + $this->seeInSubjects(["Message for Taylor Otwell", "Message for Adam Wathan"]); + } + public function test_see_message_from() { $mailer = $this->mailer = $this->getMailThief(); diff --git a/tests/MailThiefTest.php b/tests/MailThiefTest.php index b51ce67..be53cc2 100644 --- a/tests/MailThiefTest.php +++ b/tests/MailThiefTest.php @@ -102,7 +102,7 @@ public function test_from_overrides_previous_from() public function test_global_from_is_respected() { $mailer = $this->getMailThief(); - + $mailer->alwaysFrom('john@example.com'); $mailer->send('example-view', [], function ($m) { @@ -115,7 +115,7 @@ public function test_global_from_is_respected() public function test_global_from_gets_overwritten_if_specified() { $mailer = $this->getMailThief(); - + $mailer->alwaysFrom('john@example.com'); $mailer->send('example-view', [], function ($m) { @@ -226,7 +226,7 @@ public function test_queue_on_is_sent_immediately() public function test_global_from_is_respected_when_email_is_queued() { $mailer = $this->getMailThief(); - + $mailer->alwaysFrom('john@example.com'); $mailer->queue('example-view', [], function ($m) { @@ -272,7 +272,7 @@ public function test_later_messages_are_not_included_when_checking_to_see_if_an_ public function test_global_from_is_respected_when_email_set_to_later_with_a_delay() { $mailer = $this->getMailThief(); - + $mailer->alwaysFrom('john@example.com'); $mailer->later(10, 'example-view', [], function ($m) { @@ -403,4 +403,19 @@ public function test_it_reads_values_from_the_config_helper_function() $this->assertEquals(['foo@bar.tld' => 'First Last'], $mailer->lastMessage()->from->all()); } + + public function test_it_gets_subjects() + { + $mailer = $this->getMailThief(); + + collect(['foo@bar.tld', 'baz@qux.tld'])->each(function ($email) use ($mailer) { + $mailer->send('example-view', [], function ($m) use ($email) { + $m->subject("Message for {$email}"); + }); + }); + + $messages = ["Message for foo@bar.tld", "Message for baz@qux.tld"]; + + $this->assertEquals($messages, $mailer->subjects()->all()); + } }