webcanyon/codeception-mailtrap

Codeception 的 Mailtrap 模块

v0.0.6 2024-04-15 13:03 UTC

This package is auto-updated.

Last update: 2024-09-15 13:51:45 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);