在我的测试中,我调用了一些发送电子邮件的命令。我可以使用以下命令显示发送的电子邮件数量:
$output->writeln(
$spool->flushQueue(
$this->getContainer()->get('swiftmailer.transport.real')
)
);
Symfony2 文档解释了如何在 Web 测试期间使用分析器获取电子邮件内容(在 Stack Overflow 上也有解释),但我不知道在没有 Web 请求时如何做同样的事情。
我使用了这些链接中提供的代码:
<?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\Response 的实例,给定 null,在 .../vendor/symfony/symfony/src/Symfony/ 中调用Bundle/FrameworkBundle/Client.php 在第 72 行并定义 .../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');
这似乎是合乎逻辑的,因为没有请求,所以没有响应。
我使用 Symfony 2.8.7。
这是我的 Swiftmailer 配置app/config_test.yml
:
swiftmailer:
disable_delivery: true
delivery_address: %swiftmailer.delivery_address%