Using the \AKlump\DrupalTest\EndToEndTestCase tests, you can:
In setUpBeforeClass indicate the email handler to use. At this time there are two handlers, AKlump\DrupalTest\Utilities\UnixMail and \AKlump\DrupalTest\Utilities\MailhogMail, and you may write your own by implementing \AKlump\DrupalTest\Utilities\EmailHandlerInterface.
use AKlump\DrupalTest\Utilities\UnixMail;
...
public static function setUpBeforeClass() {
static::setEmailHandler(new UnixMail());
}
Do something in a test like this example, which waits for a password reset email and then visits the contained URL.
public function testWelcomeEmailContainsPasswordResetUrl() {
$email = $this->waitForEmail();
// ::waitForEmail always returns an array, we just want the first email.
$email = reset($email);
$body = $email->getMessageBody('text');
$this->assertSame(1, preg_match('/(http:\/\/.+\/user\/reset.+)\n/', $body, $matches));
$reset_pass_url = $matches[1];
$this->loadPageByUrl($reset_pass_url);
}
waitForEmail will return an array of PhpMimeMailParser\Parserinstances, which makes it easy to assert against parts of each email. To learn more about that class click here.
| Parser |
|---|
| decodeContentTransfer ($encodedString, $encodingType) |
| decodeHeader ($input) |
| decodeSingleHeader ($input) |
| getAddresses ($name) |
| getAttachments ($include_inline = true) |
| getAttachmentStream (&$part) |
| getData () |
| getEmbeddedData ($contentId) |
| getHeader ($name) |
| getHeaders () |
| getHeadersRaw () |
| getInlineParts ($type = 'text') |
| getMessageBody ($type = 'text') |
| getPart ($type, $parts) |
| getPartBody (&$part) |
| getPartBodyFromFile (&$part) |
| getPartBodyFromText (&$part) |
| getPartCharset ($part) |
| getPartComplete (&$part) |
| getPartFromFile (&$part) |
| getPartFromText (&$part) |
| getPartHeader (&$part) |
| getPartHeaderFromFile (&$part) |
| getPartHeaderFromText (&$part) |
| getParts () |
| getRawHeader ($name) |
| parse () |
| partIdIsChildOfAnAttachment ($checkPartId) |
| partIdIsChildOfPart ($partId, $parentPartId) |
EmailHandlerInterface can retrieve it.You can determine the email address used by your handler with \AKlump\DrupalTest\Utilities\EmailHandlerInterface::getInboxAddress. For example, you could do this temporarily and then read the console output:
public function setUp() {
$this->setEmailHandler(new UnixMail());
echo $this->emailHandler->getInboxAddress(); die;
}
When using Lando this is the strategy to use; use MailHog and \AKlump\DrupalTest\Utilities\MailhogMail.
echo "MESSAGE" | mail -s "SUBJECT" "USER@HOST"
When implementing waitForEmail if observation mode is enabled you will see the emails as observerPopups.