【问题标题】:Symfony PHPUnit mock SwiftMailerSymfony PHPUnit 模拟 SwiftMailer
【发布时间】:2016-04-20 08:18:08
【问题描述】:

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

如何模拟 swiftMailer 和发送邮件前检查参数?

这是我的 config_test.yml

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 函数 'send' 发送电子邮件

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);
}

我像食谱一样尝试

// 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];

但有错误

PHP Fatal error:  Call to a member function getCollector() on a non-object

更新

在配置中我没有启用配置文件

framework:
    test: ~
    session:
        storage_id: session.storage.mock_file
        cookie_httponly: true
        cookie_secure: true
    profiler:
        collect: false

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

$client->enableProfiler();

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

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

【问题讨论】:

标签: php unit-testing symfony mocking swiftmailer


【解决方案1】:

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

# app/config_test.yml

framework:
    profiler:
        enabled: true
        collect: false

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

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

// 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.6.11 并且在配置中我没有 enabled: true 但之前有请求 $client->enableProfiler(); 并且可以工作。我尝试添加配置enabled: true 并测试它。
  • 我没有解释,但似乎enabled: true 是必要的,即使$this->enableProfiler(); 应该做同样的事情。
猜你喜欢
  • 2013-02-09
  • 1970-01-01
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
  • 2020-10-25
  • 2014-11-07
  • 2015-11-01
  • 2017-01-13
相关资源
最近更新 更多