首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony PHPUnit模拟SwiftMailer

Symfony PHPUnit模拟SwiftMailer
EN

Stack Overflow用户
提问于 2016-04-20 08:18:08
回答 1查看 2.1K关注 0票数 1

我对我的端点(api动作) postLeadAction进行了功能测试,在刷新新实体之后,我发送电子邮件表示祝贺。我发送电子邮件与帮助SwiftMailer与运输sendGrid。以及如何在发送电子邮件之前检查sebject、fromName、fromEmail、toEmail。现在,我使用-env= test运行测试,并在配置快速邮件程序中为测试环境添加用于目录的发送电子邮件文件的假脱机参数,而不是发送电子邮件。

如何模拟swiftMailer并在发送电子邮件之前检查参数?

这是我的config_test.yml

代码语言:javascript
复制
swiftmailer:
default_mailer: default
mailers:
    default:
        transport: %mailer_transport%
        host: '%mailer_host%'
        port: 587
        encryption: ~
        username: '%mailer_user%'
        password: '%mailer_password%'
        spool:
            type: file
            path: '%kernel.root_dir%/spool'

这个MailerWrapper类,电子邮件发送与SwiftMailer函数‘发送’

代码语言:javascript
复制
class MailerWrapper
{
protected $mailer;
/**
 * @var \Swift_Message
 */

  //some parameters

public function __construct(\Swift_Mailer $mailer)
{
    $this->mailer = $mailer;
}

public function newMessage()
{
    //some parameters

    return $this;
}

public function send()
{
   //some logic with message
    return $this->mailer->send($this->message);
}

我试着喜欢烹饪书

代码语言:javascript
复制
// Enable the profiler for the next request (it does nothing if the profiler is not available)
$this->client->enableProfiler();

$mailCollector = $this->client->getProfile()->getCollector('swiftmailer.mailer.default');

// Check that an email was sent
$this->assertEquals(1, $mailCollector->getMessageCount());

$collectedMessages = $mailCollector->getMessages();
$message = $collectedMessages[0];

但有错误

代码语言:javascript
复制
PHP Fatal error:  Call to a member function getCollector() on a non-object

更新

在配置中,我不启用配置文件

代码语言:javascript
复制
framework:
    test: ~
    session:
        storage_id: session.storage.mock_file
        cookie_httponly: true
        cookie_secure: true
    profiler:
        collect: false

但是我有错误,因为我在http请求之后启用了配置文件,当我启用以前-一切正常。

代码语言:javascript
复制
$client->enableProfiler();

$this->request(
    'post',
    $this->generateUrl('post_lead', [], UrlGeneratorInterface::RELATIVE_PATH),
    [],
    [
     // some parameters
    ]
);

$mailCollector = $client->getProfile()->getCollector('swiftmailer');
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-20 11:59:15

我用Symfony 2.8进行了测试,并且必须在配置中启用分析器:

代码语言:javascript
复制
# app/config_test.yml

framework:
    profiler:
        enabled: true
        collect: false

在定义了enabled: true之后,您的测试应该可以工作。

为了避免PHP在我的测试中出现致命错误,我在测试电子邮件之前添加了一个小的检查:

代码语言:javascript
复制
// Enable the profiler for the next request (it does nothing if the profiler is not available)
$this->client->enableProfiler();

// Check that the profiler is available.
if ($profile = $this->client->getProfile()) {
    $mailCollector = $profile->getCollector('swiftmailer');

    // Check that an e-mail was sent
    $this->assertEquals(1, $mailCollector->getMessageCount());

    // …
}
else {
    $this->markTestIncomplete(
        'Profiler is disabled.'
    );
}

使用此检查,测试将被PHPUnit标记为不完整,而不是返回错误和破坏测试套件。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36737791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档