テスト中に、メールを送信するいくつかのコマンドを呼び出します。次のコマンドを使用して、送信された電子メールの数を表示できます。
$output->writeln(
$spool->flushQueue(
$this->getContainer()->get('swiftmailer.transport.real')
)
);
Symfony2 のドキュメントには、Web テスト中にプロファイラーを使用してメール コンテンツを取得する方法が説明されています(こちらのスタック オーバーフローでも説明されています) が、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/ で呼び出されます。 72 行目の Bundle/FrameworkBundle/Client.php および定義済み .../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%