koehnlein / codeception-email-mailpit
为Codeception提供测试辅助工具,用于测试Mailpit的电子邮件功能
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0
- ext-json: *
- codeception/codeception: ^5.0
- guzzlehttp/guzzle: ^6.1 || ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- http-interop/http-factory-guzzle: ^1.2
- phpunit/phpunit: ^9.5
- roave/security-advisories: dev-latest
Suggests
- ext-iconv: Will be used as a mime decoder (iconv_mime_decode)
- ext-mbstring: Will be used as a mime decoder if ext-iconv is not installed (mb_decode_mimeheader)
This package is auto-updated.
Last update: 2024-09-08 16:44:54 UTC
README
此codeception模块可用于针对您的Mailpit实例运行测试。
Mailpit受到了MailHog的启发,但MailHog已经不再开发。因为它并不是1:1的替代品,API已经更改,现有的MailHog codeception模块不能再使用。
此codeception模块基于oqq/codeception-email-mailhog(它是ericmartel/codeception-email-mailhog的分支)构建,并为Mailpit提供了与提到的模块为MailHog提供的几乎相同的功能。
安装
通过composer,需要此包
composer req koehnlein/codeception-email-mailpit --dev
然后在您的Codeception suite yaml文件中打开它
class_name: FunctionalTester modules: enabled: - Mailpit config: Mailpit: url: 'http://mailpit.dev' port: '8025'
可以使用guzzleRequestOptions
变量直接将附加参数传递给Guzzle连接。
deleteEmailsAfterScenario
变量可以设置为true以确保在每次场景结束时删除所有电子邮件,但默认情况下它是关闭的。
添加的方法
此模块为用户添加了一些公共方法,例如
deleteAllEmails()
删除Mailpit中的所有电子邮件
fetchEmails()
从Mialpit获取所有电子邮件头,按时间戳排序并将它们分配给当前和未读收件箱
accessInboxFor($address)
筛选电子邮件,仅保留由提供的地址接收的电子邮件
openNextUnreadEmail()
弹出最新未读电子邮件并将其分配为测试电子邮件
openNextAttachmentInOpenedEmail()
弹出下一个附件并将其分配为测试附件
示例测试
以下是一个简单的场景,我们测试电子邮件的内容。有关所有可用测试方法的详细列表,请参阅[Codeception Email Testing Framework][CodeceptionEmailTestingFramework]。
<?php $I = new FunctionalTester($scenario); $I->am('a member'); $I->wantTo('request a reset password link'); // First, remove all existing emails in the Mailpit inbox $I->deleteAllEmails(); // Implementation is up to the user, use this as an example $I->requestAPasswordResetLink(); // Query Mailpit and fetch all available emails $I->fetchEmails(); // This is optional, but will filter the emails in case you're sending multiple emails or use the BCC field $I->accessInboxFor('[email protected]'); // A new email should be available and it should be unread $I->haveEmails(); $I->haveUnreadEmails(); // Set the next unread email as the email to perform operations on $I->openNextUnreadEmail(); // After opening the only available email, the unread inbox should be empty $I->dontHaveUnreadEmails(); // Validate the content of the opened email, all of these operations are performed on the same email $I->seeInOpenedEmailSubject('Your Password Reset Link'); $I->seeInOpenedEmailTextBody('Follow this link to reset your password'); $I->seeInOpenedEmailHtmlBody('<a href="https://www.example.org/">Follow this link to reset your password</a>'); $I->seeInOpenedEmailRecipients('[email protected]'); // Validate if email has attachments $I->haveAttachmentsInOpenedEmail(); // Open next attachment $I->openNextAttachmentInOpenedEmail(); // Validate metadata of the attachment $I->seeInFilenameOfOpenedAttachment(); $I->grabFilenameFromOpenedAttachment(); $I->grabContentTypeFromOpenedAttachment(); $I->grabSizeFromOpenedAttachment();
从MailHog Codeception模块迁移
如果您想从codeception-email-mailhog
切换到此模块,您需要遵循以下步骤
删除旧的MailHog模块
根据您安装的codeception-email-mailhog
的分支,您可以使用以下方式卸载它
composer remove oqq/codeception-email-mailhog --dev
或
composer remove ericmartal/codeception-email-mailhog --dev
或您使用的其他分支的任何包名。
添加新的Mailpit模块
composer req koehnlein/codeception-email-mailpit --dev
更新Codeception配置
在Codeception配置文件中更改模块名称,从MailHog
更改为Mailpit
。
重构您的Cests
- 搜索所有
$I->...EmailBody(...)
出现,并重构为$I->...EmailTextBody(...)
和/或$I->...EmailHtmlBody(...)
- 现在
$I->canSeeInOpenedEmailSender
中的名称被双引号包围。因此,如果您以前使用过$I->canSeeInOpenedEmailSender('My Name <[email protected]>')
,请将其替换为$I->canSeeInOpenedEmailSender('"My Name" <[email protected]>');