captbaritone / mailcatcher-codeception-module
在您的Codeception验收测试中测试电子邮件
Requires
- php: ^7.4 || ^8.0
- ext-json: *
- codeception/codeception: ^4.0 || ^5.0
- guzzlehttp/guzzle: ^6.0 || ^7.0
- zbateson/mail-mime-parser: ^1.2
Requires (Dev)
- codeception/module-asserts: ^1.1 || ^2
- phpmailer/phpmailer: ^6.1.6
Suggests
- codeception/module-asserts: Required if using Codeception >= 4.0
README
此模块将允许您在Codeception验收测试期间测试发送的电子邮件。它取决于您在开发服务器上安装了MailCatcher。
它受到了Codeception博客文章的启发:在PHP中测试电子邮件。目前它非常简单。如果您有更多功能的想法,请发送pull请求或提交问题。
安装
-
将包添加到您的
composer.json
composer require --dev captbaritone/mailcatcher-codeception-module
-
配置项目以在测试环境中通过
smtp://127.0.0.1:1025
发送电子邮件 -
在您的
acceptance.suite.yml
中启用模块modules: enabled: - MailCatcher config: MailCatcher: url: 'http://127.0.0.1' port: '1080'
可选配置
如果您需要指定一些特殊选项(例如SSL验证或身份验证头),您可以设置所有允许的Guzzle请求选项
class_name: WebGuy
modules:
enabled:
- MailCatcher
config:
MailCatcher:
url: 'http://127.0.0.1'
port: '1080'
guzzleRequestOptions:
verify: false
debug: true
version: 1.0
示例用法
<?php $I->wantTo('Get a password reset email'); // Clear old emails from MailCatcher $I->resetEmails(); // Reset password $I->amOnPage('forgotPassword.php'); $I->fillField("input[name='email']", 'user@example.com'); $I->click('Submit'); $I->see('Please check your inbox'); $I->seeInLastEmail('Please click this link to reset your password');
操作
resetEmails
清除MailCatcher列表中的电子邮件。这可以防止看到之前测试中发送的电子邮件。在触发任何电子邮件发送之前,您可能想要这样做。
示例
<?php
// Clears all emails
$I->resetEmails();
?>
seeEmailAttachmentCount
检查最后收到的电子邮件中附件的预期数量。
示例
<?php
$I->seeEmailAttachmentCount(1);
?>
- 参数 $expectCount
seeAttachmentInLastEmail
检查最后收到的电子邮件是否包含具有文件名的附件。
示例
<?php
$I->seeAttachmentInLastEmail('image.jpg');
?>
- 参数 $filename
seeInLastEmail
检查电子邮件是否包含一个值。它搜索电子邮件的全部原始文本:标题、主题行和正文。
示例
<?php
$I->seeInLastEmail('Thanks for signing up!');
?>
- 参数 $text
seeInLastEmailTo
检查最后发送到地址的电子邮件是否包含一个值。它搜索电子邮件的全部原始文本:标题、主题行和正文。
这很有用,例如,一个页面触发了对新用户的电子邮件,以及对管理员的电子邮件。
示例
<?php
$I->seeInLastEmailTo('user@example.com', 'Thanks for signing up!');
$I->seeInLastEmailTo('admin@example.com', 'A new user has signed up!');
?>
- 参数 $email
- 参数 $text
dontSeeInLastEmail
检查电子邮件是否不包含一个值。它搜索电子邮件的全部原始文本:标题、主题行和正文。
示例
<?php
$I->dontSeeInLastEmail('Hit me with those laser beams');
?>
- 参数 $text
dontSeeInLastEmailTo
检查最后发送到地址的电子邮件是否不包含一个值。它搜索电子邮件的全部原始文本:标题、主题行和正文。
示例
<?php
$I->dontSeeInLastEmailTo('admin@example.com', 'But shoot it in the right direction');
?>
- 参数 $email
- 参数 $text
grabAttachmentsFromLastEmail
从电子邮件中抓取附件
返回格式为 [ [filename1 => bytes1], [filename2 => bytes2], ...] 的数组
示例
<?php
$attachments = $I->grabAttachmentsFromLastEmail();
?>
grabMatchesFromLastEmail
根据正则表达式从最后收到的电子邮件中提取匹配项和子匹配项的数组。它搜索电子邮件的全部原始文本:标题、主题行和正文。返回值是一个类似于由preg_match()
返回的数组。
示例
<?php
$matches = $I->grabMatchesFromLastEmail('@<strong>(.*)</strong>@');
?>
- 参数 $regex
grabFromLastEmail
根据正则表达式从最后收到的电子邮件中提取字符串。它搜索电子邮件的全部原始文本:标题、主题行和正文。
示例
<?php
$match = $I->grabFromLastEmail('@<strong>(.*)</strong>@');
?>
- 参数 $regex
grabUrlsFromLastEmail
从最后收到的电子邮件中提取URL数组。它搜索电子邮件的全部原始正文。返回值是字符串数组。
示例
<?php
$urls = $I->grabUrlsFromLastEmail();
?>
lastMessageFrom
获取发送到指定地址的完整电子邮件对象。
示例
<?php
$email = $I->lastMessageFrom('example@example.com');
$I->assertNotEmpty($email['attachments']);
?>
lastMessage
从最后一封电子邮件中获取完整电子邮件对象。
示例
<?php
$email = $I->grabLastEmail();
$I->assertNotEmpty($email['attachments']);
?>
grabMatchesFromLastEmailTo
根据正则表达式从最后一封电子邮件到指定地址提取匹配项和子匹配项的数组。它搜索电子邮件的完整原始文本:标题、主题行和正文。返回值类似于 preg_match()
返回的数组。
示例
<?php
$matchs = $I->grabMatchesFromLastEmailTo('user@example.com', '@<strong>(.*)</strong>@');
?>
- 参数 $email
- 参数 $regex
grabFromLastEmailTo
根据正则表达式从最后一封电子邮件到指定地址提取字符串。它搜索电子邮件的完整原始文本:标题、主题行和正文。
示例
<?php
$match = $I->grabFromLastEmailTo('user@example.com', '@<strong>(.*)</strong>@');
?>
- 参数 $email
- 参数 $regex
seeEmailCount
断言自上次调用 resetEmails()
以来已发送一定数量的电子邮件。
示例
<?php
$match = $I->seeEmailCount(2);
?>
- 参数 $count
许可证
在与 Codeception 相同的许可证下发布:[MIT](https://github.com/captbaritone/codeception-mailcatcher-module/blob/master/LICENSE)(外部链接)