Permalink
Browse files

Add Messages Accessors (#56)

  • Loading branch information...
1 parent a422774 commit 9e073e4cf63fd9425eb85278352380faa83d72af @mcordingley mcordingley committed with besologic Nov 30, 2016
Showing with 94 additions and 0 deletions.
  1. +34 −0 src/Testing/InteractsWithMail.php
  2. +60 −0 tests/InteractsWithMailTest.php
@@ -3,6 +3,8 @@
namespace MailThief\Testing;
use MailThief\Facades\MailThief;
+use MailThief\Message;
+use MailThief\Support\MailThiefCollection;
use Illuminate\Contracts\Mail\Mailer;
trait InteractsWithMail
@@ -19,6 +21,38 @@ private function getMailer()
return $this->mailer ?: MailThief::getFacadeRoot();
}
+ /**
+ * @return MailThiefCollection
+ */
+ public function getMessages()
+ {
+ return $this->getMailer()->messages;
+ }
+
+ /**
+ * @param array|string $emails
+ * @return MailThiefCollection
+ */
+ public function getMessagesFor($emails)
+ {
+ $emails = collect((array) $emails);
+
+ return $this->getMessages()->filter(function (Message $message) use ($emails) {
+ return $emails->contains(function ($email) use ($message) {
+ return $message->hasRecipient($email);
+ });
+ });
+ }
+
+ /**
+ * @param array|string $email
+ * @return Message
+ */
+ public function getLastMessageFor($email)
+ {
+ return $this->getMessagesFor($email)->last();
+ }
+
/** @before */
public function hijackMail()
{
@@ -20,6 +20,66 @@ public function hijackMail()
return;
}
+ public function test_get_messages()
+ {
+ $mailer = $this->mailer = $this->getMailThief();
+
+ $mailer->send('example-view', [], function ($m) {
+ $m->to('john@example.com');
+ });
+
+ $message = $this->getMessages()->first();
+
+ static::assertEquals(['john@example.com'], $message->to->toArray());
+ }
+
+ public function test_get_messages_for()
+ {
+ $mailer = $this->mailer = $this->getMailThief();
+
+ $mailer->send('example-view', [], function ($m) {
+ $m->to('john@example.com');
+ });
+
+ $mailer->send('example-view', [], function ($m) {
+ $m->to(['john@example.com', 'jay@example.com']);
+ });
+
+ $mailer->send('example-view', [], function ($m) {
+ $m->to('jay@example.com');
+ });
+
+ $messages = $this->getMessagesFor(['john@example.com']);
+
+ static::assertEquals(2, $messages->count());
+
+ foreach ($messages as $message) {
+ static::assertTrue($message->to->contains('john@example.com'));
+ }
+ }
+
+ public function test_get_last_message_for()
+ {
+ $mailer = $this->mailer = $this->getMailThief();
+
+ $mailer->send('example-view', [], function ($m) {
+ $m->to('john@example.com');
+ });
+
+ $mailer->send('example-view', [], function ($m) {
+ $m->to(['john@example.com', 'jay@example.com']);
+ });
+
+ $mailer->send('example-view', [], function ($m) {
+ $m->to('jay@example.com');
+ });
+
+ $message = $this->getLastMessageFor(['john@example.com']);
+
+ static::assertTrue($message->to->contains('john@example.com'));
+ static::assertTrue($message->to->contains('jay@example.com'));
+ }
+
public function test_hijack_mail()
{
$mailer = $this->mailer = $this->getMockMailThief();

0 comments on commit 9e073e4

Please sign in to comment.