{
"require": {
"pmaasz/behat-mail-extension": "dev-master"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/pmaasz/BehatMailExtension.git"
}
]
}
-
Implement the MailAwareContext in your feature context
-
Use the MailTrait in your context.
use BehatMailExtension\Context\MailAwareContext;
use BehatMailExtension\Context\MailTrait;
class FeatureContext implements MailAwareContext
{
use MailTrait;
...
Using the mail trait will add a mail property to your feature context.
Add the MailExtension to your behat.yaml file:
default:
extensions:
BehatMailExtension\ServiceContainer\MailExtension:
driver: imap //required
server: 'imap.gmail.com' //required
port: //defaults to '993' //required
flags: //defaults to '/imap/ssl/validate-cert' //required
username: //required
password: //required
Access the mail property from your feature context to test any emails sent.
/**
* @Then I should receive a welcome email
*/
public function iShouldReceiveAWelcomeEmail()
{
$message = $this->mail->getMessages();
PHPUnit_Framework_Assert::assertEquals('Welcome!', $message->getSubject());
PHPUnit_Framework_Assert::assertContains('Please confirm your account', $message->getBodyText());
}
The mail driver, accessible via the mail property on the feature context, offers the following methods. These methods are described in the MailDriverInterface.
- getMailbox()
- getMailboxes()
- analyzeMailbox()
- analyzeMailboxes()
- setMailboxFlag()
- getMessages()
- sendMessage()
- sendMessages()
- searchMessages()
- moveMessage()
- downloadMessageAttachments()
- deleteMessage()
- deleteMessages() (This is called automatically after scenarios tagged @mail)
The mail driver will return a message object with the following API:
- getTo()
- getFrom()
- getDate() (DateTimeImmutable)
- getSubject()
- isAnswered()
- isDeleted()
- isDraft()
- isSeen()
- getBodyHtml()
- getBodyText()
- markAsSeen()
- setFlag()
- clearFlag()
- getAttachements()