rpkamp/mailhog-mink-behat-context

Mailhog Extension for Behat

1.1.0 2023-05-20 15:07 UTC

This package is auto-updated.

Last update: 2024-10-20 18:38:28 UTC


README

A PHP (7.4+) Behat context for Mailhog with Mink integration.

Allows to simulate link clicks in emails and continue from the links URL in Mink. Suitable in browser-email-browser flows such as forgotten password resets.

This context is based on the MailhogAwareContext from rpkamp/mailhog-behat-extension.

Installation

This package does not require any specific HTTP client implementation, but it requires rpkamp/mailhog-client, which is based on 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 context, otherwise installation will fail.

composer require rpkamp/mailhog-mink-behat-context <your-http-client-of-choice>

For more information please refer to the HTTPlug documentation for Library Users.

Usage

Register extension in Behat

This context require rpkamp/mailhog-behat-extension to be enabled in your behat configuration.

If you have not already done so, add the MailhogExtension extension to your behat.yml like so:

default:
  suites:
    # your suite configuration here
  extensions:
    rpkamp\Behat\MailhogExtension:
      base_url: http://localhost:8025

The base_url is the URL where the Mailhog Web UI is listening to (by default this is http://localhost:8025).

And also include the \rpkamp\Behat\MailhogExtension\Context\MinkAwareMailogContext in your contexts for behat:

default:
  suites:
    contexts:
      - rpkamp\Behat\Context\MinkAwareMailhogContext

Note that this context does not extend rpkamp\Behat\MailhogExtension\Context\MailhogContext from rpkamp/mailhog-behat-extension, so if you need functionality from both contexts you need to include both contexts in your behat configuration.

Gherkin steps

This context contains the following gherkin steps:

When I click the link "link-description" in the last received email
When I click the link "link-description" in the opened email 

This first step will fetch the last received email from Mailhog and search for a link with "link-description" as (in order):

  • id attribute (<a id="link-description"></a>)
  • link text (<a>link-description</a>)
  • title attribute (<a title="link-description"></a>)
  • alt attribute (<a alt="link-description"></a>)
  • partial link text (<a>some link-description</a>)

It will stop when it finds any of the above criteria fits and that tell Mink to follow that link, so from there on you can continue in Mink. This enables scenarios like password reset where you receive an email, click a link in that email and then do something in a browser.

The second step fetches the email that was opened using rpkamp/mailhog-behat-extension and open the link with "link-description" in that, using the same procedure as outline above.

Run tests

Make sure you have Mailhog running and run:

make test

Running Mailhog for tests

You can either run your own instance of Mailhog or use the provided docker-compose file to run one for you. To run Mailhog with Docker make sure you have Docker and docker-compose installed and run:

docker-compose up -d

Mailhog ports for tests

To prevent port collisions with any other Mailhog instances while testing the tests expect Mailhog to listen to SMTP on port 4025 (instead of the default 1025) and to HTTP traffic on port 11025 (instead of the default 8025).