Download the PHP package rpkamp/mailhog-behat-extension without Composer
On this page you can find all versions of the php package rpkamp/mailhog-behat-extension. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rpkamp/mailhog-behat-extension
More information about rpkamp/mailhog-behat-extension
Files in rpkamp/mailhog-behat-extension
Package mailhog-behat-extension
Short Description Mailhog Extension for Behat
License MIT
Informations about the package mailhog-behat-extension
Mailhog Behat Extension
A simple PHP (8.1+) Behat extension for Mailhog.
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 extension, otherwise installation will fail.
For more information please refer to the HTTPlug documentation for Library Users.
Usage
Register extension in Behat
Add the extension to your behat.yml
like so:
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
The easiest way to get started is to configure behat to use rpkamp\Behat\MailhogExtension\Context\MailhogContext
like so:
This enables the following Gherkin for your scenarios to make assumptions on received email messages:
-
Given my inbox is empty
will actually purge all emails from Mailhog. - The
2
inThen 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:
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
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:
Now every time your FeatureContext is initialized Behat will inject an rpkamp\MailhogClient
in it you can use using the $mailhog
property of your context. For example:
Use email tag to purge emails before scenarios
In scenarios where you want to make sure you have received the correct number of messages you will want to purge mailhog before the scenario is started. In order to do that add the @email
tag to either the scenario or the feature. As usual, when you apply it to the feature it applies to all scenarios within that feature.
If you want to use a different tag you can supply the name (without the initial @) in the purge_tag
setting of this extension.
Implement OpenedEmailStorageAwareContext (Advanced)
If you want to write an extension/context for behat that uses the feature of opening email from this extension you can have your Context
implement OpenedEmailStorageAwareContext
:
Run tests
Make sure you have Mailhog running and run:
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:
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 3025 (instead of the default 1025) and to HTTP traffic on port 10025 (instead of the default 8025).
All versions of mailhog-behat-extension with dependencies
behat/behat Version ^3.8.0
rpkamp/mailhog-client Version ^2.0
symfony/dependency-injection Version ^6.4 || ^7.0
psr-discovery/http-client-implementations Version ^1.0
psr-discovery/http-factory-implementations Version ^1.0
symfony/http-client Version ^6.0