dev-codeception-v3
dev-codeception-v3Test emails in your Codeception acceptance tests
The Requires
Wallogit.com
2017 © Pedro Peláez
Test emails in your Codeception acceptance tests
This module will let you test emails that are sent during your Codeception acceptance tests., (*2)
Add the package into your composer.json:, (*3)
{
"require-dev": {
"codeception/codeception": "*",
"fetch/zend-mail-codeception-module": "^1.0"
}
}
Tell Composer to download the package:, (*4)
php composer.phar update
Update your Zend mail configuration to use a file transport:, (*5)
function mail_filename(){
return uniqid() . '.mail';
}
$transportOptions = new Zend\Mail\Transport\FileOptions([
'path' => 'tests/_output/mail',
'callback' => 'mail_filename'
]);
$transport = new Zend\Mail\Transport\File($transportOptions);
Then enable it in your acceptance.suite.yml configuration and set path
to the transport directory., (*6)
class_name: WebGuy
modules:
enabled:
- ZendMail
config:
ZendMail:
path: 'tests/_output/mail'
You will then need to rebuild your actor class:, (*7)
php codecept.phar build
$I = new WebGuy($scenario);
$I->wantTo('Get a password reset email');
// Cleared old emails from path
$I->resetEmails();
// Reset
$I->amOnPage('forgotPassword.php');
$I->fillField("input[name='email']", 'user@example.com');
$I->click("Submit");
$I->see("Please check your email");
$I->seeInLastEmail("Please click this link to reset your password");
Clears the emails in the messages directory. This is prevents seeing emails sent during a previous test. You probably want to do this before you trigger any emails to be sent, (*8)
Example:, (*9)
// Clears all emails $I->resetEmails();
Checks that an email contains a value. It searches the full raw text of the email: headers, subject line, and body., (*10)
Example:, (*11)
$I->seeInLastEmail('Thanks for signing up!');
Checks that the last email sent to an address contains a value. It searches the full raw text of the email: headers, subject line, and body., (*12)
This is useful if, for example a page triggers both an email to the new user, and to the administrator., (*13)
Example:, (*14)
$I->seeInLastEmailTo('user@example.com', 'Thanks for signing up!');
$I->seeInLastEmailTo('admin@example.com', 'A new user has signed up!');
Checks that an email does NOT contain a value. It searches the full raw text of the email: headers, subject line, and body., (*15)
Example:, (*16)
$I->dontSeeInLastEmail('Hit me with those laser beams');
Checks that the last email sent to an address does NOT contain a value. It searches the full raw text of the email: headers, subject line, and body., (*17)
Example:, (*18)
$I->dontSeeInLastEmailTo('admin@example.com', 'But shoot it in the right direction');
Extracts an array of matches and sub-matches from the last email based on
a regular expression. It searches the full raw text of the email: headers,
subject line, and body. The return value is an array like that returned by
preg_match()., (*19)
Example:, (*20)
$matches = $I->grabMatchesFromLastEmail('@<strong>(.*)</strong>@');
Extracts a string from the last email based on a regular expression. It searches the full raw text of the email: headers, subject line, and body., (*21)
Example:, (*22)
$match = $I->grabFromLastEmail('@<strong>(.*)</strong>@');
Extracts an array of matches and sub-matches from the last email to a given
address based on a regular expression. It searches the full raw text of the
email: headers, subject line, and body. The return value is an array like that
returned by preg_match()., (*23)
Example:, (*24)
$matchs = $I->grabMatchesFromLastEmailTo('user@example.com', '@<strong>(.*)</strong>@');
Extracts a string from the last email to a given address based on a regular expression. It searches the full raw text of the email: headers, subject line, and body., (*25)
Example:, (*26)
$match = $I->grabFromLastEmailTo('user@example.com', '@<strong>(.*)</strong>@');
Asserts that a certain number of emails have been sent since the last time
resetEmails() was called., (*27)
Example:, (*28)
$match = $I->seeEmailCount(2);
Released under the same license as Codeception: MIT, (*29)
Test emails in your Codeception acceptance tests