获取命令测试期间发送的电子邮件的内容 [英] Get content of email sent during command tests

查看:115
本文介绍了获取命令测试期间发送的电子邮件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在测试过程中,我调用了一些发送电子邮件的命令。我可以显示通过以下命令发送的电子邮件数量:

During my tests I call some commands which send emails. I can display the number of emails sent with the following command:

$output->writeln(
    $spool->flushQueue(
        $this->getContainer()->get('swiftmailer.transport.real')
    )
);

Symfony2文档说明了如何在网络测试过程中使用探查器获取电子邮件内容(也在Stack Overflow上对此进行了说明),但我不知道该怎么做

The Symfony2 documentation explains how to get email content by using the profiler during a Web test (also explained here on Stack Overflow), but I don't know how to do the same thing when there is no Web request.

我使用了这些链接中提供的代码:

I used the code provided in these links:

<?php

namespace ACME\MyBundle\Tests\Command;

use Liip\FunctionalTestBundle\Test\WebTestCase;

class EmailTest extends WebTestCase
{
    public function tesEmailCommand()
    {
        // load data fixtures

        // http://symfony.com/doc/current/cookbook/email/testing.html
        $client = static::createClient();
        // Enable the profiler for the next request (it does nothing if the profiler is not available)
        $client->enableProfiler();

        /** @var \Symfony\Bundle\FrameworkBundle\Console\Application $application */
        // inherit from the parent class
        $application = clone $this->application;

        $application->add(new EmailCommand());
        $command = $application->find('acme:emails');
        $commandTester = new CommandTester($command);

        $commandTester->execute(array(
            'command' => 'acme:emails'
        ));

        $display = $commandTester->getDisplay();

        $this->assertContains('foo', $display);

        // http://symfony.com/doc/current/cookbook/email/testing.html
        $mailCollector = $client->getProfile()->getCollector('swiftmailer');

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

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

        // Asserting email data
        $this->assertInstanceOf('Swift_Message', $message);
        $this->assertEquals(
            'You should see me from the profiler!',
            $message->getBody()
        );
    }
}

它返回此错误:


传递给Symfony\Component\HttpKernel\Profiler\Profiler :: loadProfileFromResponse()的参数1必须是Symfony\Component\HttpFoundation的实例。 \响应,给定为空,在第72行的... / vendor / symfony / symfony / src / Symfony / Bundle / FrameworkBundle / Client.php中调用并定义
... / vendor / symfony / symfony / src /Symfony/Component/HttpKernel/Profiler/Profiler.php:81
... /供应商/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Client.php:72
... / src /ACME/MyBundle/Tests/Command/EmailTest.php:94

Argument 1 passed to Symfony\Component\HttpKernel\Profiler\Profiler::loadProfileFromResponse() must be an instance of Symfony\Component\HttpFoundation\Response, null given, called in .../vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Client.php on line 72 and defined .../vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Profiler/Profiler.php:81 .../vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Client.php:72 .../src/ACME/MyBundle/Tests/Command/EmailTest.php:94

错误来自此行:

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

似乎合乎逻辑,因为没有请求,因此没有响应。

It seems logical because there is no response since there's no request.

我使用Symfony 2.8.7。

I use Symfony 2.8.7.

这是我在<$ c $中的Swiftmailer配置c> app / config_test.yml :

swiftmailer:
    disable_delivery: true
    delivery_address: %swiftmailer.delivery_address%


推荐答案

I'



// kernel
$kernel = $this->createKernel();
$kernel->boot();

// container
$container = $kernel->getContainer();

// register swiftmailer logger
$mailer = $container->get('mailer');
$logger = new \Swift_Plugins_MessageLogger();
$mailer->registerPlugin($logger);

然后您可以通过以下方式获取消息内容:

And then you can get the message contents with:



foreach ($logger->getMessages() as $message) {
    $subject       = $message->getSubject();
    $plaintextBody = $message->getBody();
    $htmlBody      = $message->getChildren()[0]->getBody();
}

这篇关于获取命令测试期间发送的电子邮件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆