pryzmatpl/mailcatcher-codeception-module

在您的Codeception验收测试中测试电子邮件 PHP8 CP5

dev-master 2023-02-23 12:25 UTC

This package is not auto-updated.

Last update: 2024-09-20 17:53:00 UTC


README

Build Status

此模块将允许您在Codeception验收测试期间测试发送的电子邮件。它依赖于您在开发服务器上安装了 MailCatcher

它受到Codeception博客文章的启发: 在PHP中测试电子邮件。它目前非常简单。如果您有更多功能的想法,请发送拉取请求或提交问题。

安装

  1. 将包添加到您的 composer.json

    composer require --dev captbaritone/mailcatcher-codeception-module

  2. 配置您的项目,在测试环境中通过 smtp://127.0.0.1:1025 真实发送电子邮件

  3. 在您的 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