Permalink
Browse files

Add support for adding and inspecting custom mail headers (#44)

  • Loading branch information...
1 parent ed0301a commit 0f2dd08762d38587c326036e33d05175a4be48c3 @besologic besologic committed on GitHub Sep 8, 2016
Showing with 43 additions and 1 deletion.
  1. +7 −1 readme.md
  2. +9 −0 src/Message.php
  3. +13 −0 src/Testing/InteractsWithMail.php
  4. +14 −0 tests/InteractsWithMailTest.php
View
@@ -26,6 +26,7 @@ Route::post('register', function () {
$m->subject('Welcome to my app!');
$m->from('noreply@example.com');
$m->bcc('notifications@example.com');
+ $m->getHeaders()->addTextHeader('X-MailThief-Variables', 'mailthief');
});
// <snip> Return response </snip>
@@ -62,9 +63,14 @@ class RegistrationTest extends TestCase
$this->seeMessageWithSubject('Welcome to my app!');
// Make sure the email was sent from the correct address
- // (`from` can be a list, so we return it as a collection)
$this->seeMessageFrom('noreply@example.com');
+ // Make sure a given header is set on an email
+ $this->seeHeaders('X-MailThief-Variables');
+
+ // Make sure the header is set to a given value
+ $this->seeHeaders('X-MailThief-Variables', 'mailthief');
+
// Make sure the email contains text in the body of the message
// Default is to search the html rendered view
$this->assertTrue($this->lastMessage()->contains('Some text in the message'));
View
@@ -19,13 +19,15 @@ class Message
public $reply_to;
public $priority;
public $attachments;
+ public $headers;
public $delay = 0;
/**
* Methods that are available in Laravel but not provided by MailThief
* @var array
*/
public $valid_methods = [
'addPart',
+ 'getHeaders',
'setReadReceiptTo',
'setCharset',
'setMaxLineLength',
@@ -41,6 +43,7 @@ public function __construct($view, $data)
$this->bcc = collect();
$this->reply_to = collect();
$this->attachments = collect();
+ $this->headers = collect();
}
public function __call($name, $arguments)
@@ -172,6 +175,12 @@ public function attachData($data, $name, array $options = [])
return $this;
}
+ public function addTextHeader($name, $value = null)
+ {
+ $this->headers[] = ['name' => $name, 'value' => $value];
+ return $this;
+ }
+
public function getSwiftMessage()
{
throw new Exception("Cannot get Swift message from MailThief message.");
@@ -80,6 +80,19 @@ public function lastMessage()
return $this->getMailer()->lastMessage();
}
+ public function seeHeaders($name, $value = null)
+ {
+ $this->assertTrue($this->lastMessage()->headers->contains(function ($header) use ($name, $value) {
+ if (is_null($value)) {
+ return $header['name'] === $name;
+ }
+
+ return $header['name'] === $name && $header['value'] === $value;
+ }));
+
+ return $this;
+ }
+
protected function seeMessage()
{
$this->assertNotNull(
@@ -19,6 +19,7 @@ public function render()
}
};
});
+
return $factory;
}
@@ -83,4 +84,17 @@ public function test_see_message_from()
$this->seeMessageFrom('me@example.com');
}
+
+ public function test_see_headers_for()
+ {
+ $mailer = $this->mailer = $this->getMailThief();
+
+ $mailer->send('example-view', [], function ($m) {
+ $m->to('john@example.com');
+ $m->getHeaders()->addTextHeader('X-MailThief-Variables', json_encode(['mailthief_id' => 2]));
+ });
+
+ $this->seeHeaders('X-MailThief-Variables');
+ $this->seeHeaders('X-MailThief-Variables', json_encode(['mailthief_id' => 2]));
+ }
}

0 comments on commit 0f2dd08

Please sign in to comment.