Skip to content
this is a mail extension for behat
PHP
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
ReadMe.md
composer.json
composer.lock

ReadMe.md

Behat Mail Extension

Supported Drivers

Installation

{
     "require": {
            "pmaasz/behat-mail-extension": "dev-master"
        },
    
    "repositories": [
            {
                "type": "vcs",
                "url": "https://github.com/pmaasz/BehatMailExtension.git"
            }
        ]
}

Configure Your Context

  1. Implement the MailAwareContext in your feature context

  2. 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.

behat.yml

IMAP

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

Usage

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 IMAP Driver API

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 Message API

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()
You can’t perform that action at this time.