whatdafox/codeception-mailtrap

Codeception 的 Mailtrap 模块

1.4.1 2018-12-31 20:16 UTC

This package is auto-updated.

Last update: 2024-09-09 14:00:10 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License Get help on Codementor

Codeception Mailtrap 模块

本软件包为 Codeception 提供了一个 Mailtrap 模块。

安装

您需要将仓库添加到您的 composer.json 文件中

    composer require --dev whatdafox/codeception-mailtrap

配置

您可以通过在 Codeception 套件配置中将 'Mailtrap' 添加到启用模块中,将此模块用作其他任何 Codeception 模块。

将 Mailtrap 添加到模块列表中

modules:
    enabled: [Filesystem, FunctionalHelper, Db, Mailtrap]

设置配置变量

modules:
    enabled: [Filesystem, FunctionalHelper, Db, Mailtrap]
    config:
        Mailtrap:
            client_id: ADD_YOUR_TOKEN_HERE
            inbox_id: ADD_YOUR_INBOX_NAME_HERE

更新 Codeception 构建

codecept build

您已设置完毕!

用法示例

等待邮件到达

如果您的测试依赖于应用程序发送电子邮件,您可以让测试在继续之前等待配置的 Mailtrap 邮箱中到达某些内容。这些方法都有可选的超时时间,可以传递为额外的参数。

// wait for any email to arrive in your inbox...
$I->waitForEmail();

// ...or wait with an optional 10 second timeout (default is 5 seconds)
$I->waitForEmail(10);

您还可以等待满足某些条件的电子邮件。

// wait for an email with the given subject
$I->waitForEmailWithSubject("Subscription Confirmation");

// wait for an email to arrive with the given string in the HTML part of the body
$I->waitForEmailWithTextInHtmlBody("Thanks for joining!");

// wait for an email to arrive with the given string in the text part of the body
$I->waitForEmailWithTextInTextBody("Thanks for joining!");

确认邮件内容

要确认电子邮件的特定部分,您可以指示测试获取收到的最后一封电子邮件,并运行电子邮件详细信息的等价断言。

可用于检查的参数是 Mailtrap API /api/v1/inboxes/inbox_id/messages 操作提供的参数。

// returns true if all these parameters are exact matches
$I->receiveEmail([
    'subject' => 'Great Savings On Hamburgers',
    'to_email' => 'k.bacon@example.com',
    'to_name' => 'Kevin Bacon',
    'from_email' => 'noreply@astro-burger.com',
    'from_name' => 'Kate From Astronomical Burgers',
]);

有一些方法可以检查消息的常见参数,这可能使您的测试更容易阅读。这些方法将寻找精确的等价性。

// check last email was sent from the correct address
$I->receiveAnEmailFromEmail('noreply@astro-burger.com');

// check that the sender name is correct
$I->receiveAnEmailFromName('Kate From Astronomical Burgers');

// check email recipient email is correct
$I->receiveAnEmailToEmail('k.bacon@example.com');

// check email recipient name is correct
$I->receiveAnEmailToName('Kevin Bacon');

// check email has correct subject
$I->receiveAnEmailWithSubject('Great Savings On Hamburgers');

// will check to see that the email's text body matches the provided string
$I->receiveAnEmailWithTextBody("the email's complete text body");

// will check to see that the email's html body matches the provided string
$I->receiveAnEmailWithHtmlBody("<strong>the email's complete html body</strong>");

您还可以检查收到的最后一封电子邮件的局部匹配。例如,在“Great Savings on Hamburgers”中查找“Great Savings”的出现。

// check the provided string is somewhere in the subject
$I->seeInEmailSubject('Great Savings');

// check the provided string is somewhere in the email's text body
$I->seeInEmailTextBody("subset of text body");

// check the provided string is somewhere in the email's text body
$I->seeInEmailHtmlBody("<strong>subset of html body</strong>");

检查附件

您可以检查是否有附件,或特定数量的附件。如果您想对附件内容进行进一步检查,您将必须获取消息,然后自己与 Mailtrap API 交互。

// check that there are three attachments on the last message
$I->seeAttachments(3);

// check that there is at least 1 attachment on the last message
$I->seeAnAttachment();

直接获取电子邮件

如果您需要执行更复杂的测试,您可以直接获取收到的电子邮件。消息以 Codeception\Module\MailtrapMessage 实例的形式返回。

// returns the contents of your Mailtrap inbox
$lastMessages = $I->fetchMessages();

// return teh most recent message received
$lastMessage = $I->fetchLastMessage();

// return the five most recent messages received
$last5Messages = $I->fetchLastMessages(5);