During my tests, I invoke several commands that send emails. I can display the number of emails sent with the following command:
$output->writeln( $spool->flushQueue( $this->getContainer()->get('swiftmailer.transport.real') ) );
The Symfony2 documentation explains how to get email content using the profiler during a web test (also explained here in the Stack Overflow section), but I don't know how to do the same 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() ); } }
It returns this error:
Argument 1 passed to Symfony \ Component \ HttpKernel \ Profiler \ Profiler :: loadProfileFromResponse () must be an instance of Symfony \ Component \ HttpFoundation \ Response, null, 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
The error occurs from this line:
$mailCollector = $client->getProfile()->getCollector('swiftmailer');
It seems logical, because there is no answer, since there is no request.
I am using Symfony 2.8.7.
Here is my Swiftmailer configuration in app/config_test.yml
:
swiftmailer: disable_delivery: true delivery_address: %swiftmailer.delivery_address%