PHPackages                             rpkamp/mailhog-behat-extension - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. rpkamp/mailhog-behat-extension

ActiveLibrary[Testing &amp; Quality](/categories/testing)

rpkamp/mailhog-behat-extension
==============================

Mailhog Extension for Behat

1.3.0(2y ago)13414.5k↓13.9%4[1 PRs](https://github.com/rpkamp/mailhog-behat-extension/pulls)2MITPHPPHP ^8.1

Since Dec 3Pushed 1y ago2 watchersCompare

[ Source](https://github.com/rpkamp/mailhog-behat-extension)[ Packagist](https://packagist.org/packages/rpkamp/mailhog-behat-extension)[ RSS](/packages/rpkamp-mailhog-behat-extension/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (17)Versions (22)Used By (2)

Mailhog Behat Extension [![Packagist Version](https://camo.githubusercontent.com/f89d043bec89f521644149a35e81e436fb3e1d29838ab9ffc2ae31912557ea8b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f72706b616d702f6d61696c686f672d62656861742d657874656e73696f6e)](https://camo.githubusercontent.com/f89d043bec89f521644149a35e81e436fb3e1d29838ab9ffc2ae31912557ea8b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f72706b616d702f6d61696c686f672d62656861742d657874656e73696f6e)
==============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#mailhog-behat-extension-)

A simple PHP (8.1+) [Behat](http://behat.org/) extension for [Mailhog](https://github.com/mailhog/MailHog).

Installation
------------

[](#installation)

This package does not require any specific HTTP client implementation, but it requires [rpkamp/mailhog-client](https://github.com/rpkamp/mailhog-client), which is based on [HTTPlug](https://github.com/php-http/httplug), so you can inject your own HTTP client of choice. So you when you install this extension make sure you either already have an HTTP client installed, or install one at the same time as installing this extension, otherwise installation will fail.

```
composer require rpkamp/mailhog-behat-extension
```

For more information please refer to the [HTTPlug documentation for Library Users](http://docs.php-http.org/en/latest/httplug/users.html).

Usage
-----

[](#usage)

### Register extension in Behat

[](#register-extension-in-behat)

Add the extension to your `behat.yml` like so:

```
default:
  suites:
    # your suite configuration here
  extensions:
    rpkamp\Behat\MailhogExtension:
      base_url: http://localhost:8025
      purge_tag: email # optional, defaults to 'email'
```

The `base_url` is the URL where the Mailhog Web UI is listening to (by default this is `http://localhost:8025). The `purge\_tag` is the behat tag that triggers a purge in mailhog before the scenario/feature (see "Use email tag to purge emails before scenarios")

### Use MailhogContext

[](#use-mailhogcontext)

The easiest way to get started is to configure behat to use `rpkamp\Behat\MailhogExtension\Context\MailhogContext` like so:

```
default:
  suites:
    contexts:
      - rpkamp\Behat\MailhogExtension\Context\MailhogContext
```

This enables the following Gherkin for your scenarios to make assumptions on received email messages:

```
Given my inbox is empty
Then I should see an email with subject "subject"
Then I should see an email with body "body"
Then I should see an email from "sender@domain.example"
Then I should see an email with subject "subject" and body "body"
Then I should see an email with subject "subject" and body "body" from "sender@domain.example"
Then I should see an email with subject "subject" from "sender@domain.example"
Then I should see an email to "recipient@domain.example"
Then I should see an email with subject "subject" to "recipient@domain.example"
Then I should see an email with body "body" to "recipient@domain.example"
Then I should see an email from "sender@domain.example" to "recipient@domain.example"
Then I should see an email with subject "subject" and body "body" to "recipient@domain.example"
Then I should see an email with subject "subject" and body "body" from "sender@domain.example" to "recipient@domain.example"
Then I should see an email with subject "subject" from "sender@domain.example" to "recipient@domain.example"
Then I should see "some text" in email
Then there should be 2 emails in my inbox
Then I should see an email with attachment "lorem-ipsum.pdf"
```

- `Given my inbox is empty` will actually purge all emails from Mailhog.
- The `2` in `Then there should be 2 emails in my inbox` is variable, and the 's' in 'emails' is optional, so 'Then there is 1 email in my inbox' also works.

Alternatively you can "open" an email an run assumptions on the opened email:

```
When I open the latest email from "sender@domain.example"
When I open the latest email to "recipient@domain.example"
When I open the latest email with subject "Hello world"
When I open the latest email from "sender@domain.example" with subject "Hello world"
When I open the latest email to "recipient@domain.example" with subject "Hello world"
When I open the latest email with body "body"
When I open the latest email with subject "subject" and body "body"
When I open the latest email from "sender@domain.example" to "recipient@domain.example"
When I open the latest email from "sender@domain.example" with body "body"
When I open the latest email to "recipient@domain.example" with body "body"
When I open the latest email from "sender@domain.example" with subject "subject" and body "body"
When I open the latest email to "recipient@domain.example" with subject "subject" and body "body"
When I open the latest email from "sender@domain.example" to "recipient@domain.example" with subject "subject" and body "body"
Then I should see "Hello world" in the opened email
Then I should see an attachment with filename "lorem-ipsum.pdf" in the opened email
```

Take care that the implementation of this currently isn't very efficient. If there are a lot of emails in Mailhog it might take a while, especially when the email you're looking for is not there.

### Implement MailhogAwareContext

[](#implement-mailhogawarecontext)

If you want to implement something more advanced than `rpkamp\Behat\MailhogExtension\Context\MailhogContext` offers you can also implement `rpkamp\Behat\MailhogExtension\Context\MailhogAwareContext` in your own context and implement the method in that interface:

```
