Good morning.
I would like to test sending email in my Symfony Command.
My email is send with \Swift_Mailer by
$this->mailer->send($message);
I try to use this:
http://symfony.com/doc/current/cookbook/email/testing.html
But $this->client->getProfile() and Symfony Response is not available in Symfony Command.
Argument 1 passed to Symfony\Component\HttpKernel\Profiler\Profiler::loadProfileFromResponse() must be an instance of Symfony\Component\HttpFoundation\Response, null given,
Now how can i verify that my email is send correctly?
When testing commands you should have $kernel variable. From that you get container with $kernel->getContainer(). With container you have access to message logger: $container->get('swiftmailer.mailer.default.plugin.messagelogger'). That's is. Now you have an instance of Swift_Plugins_MessageLogger which have #getMessages which returns sent messages.
You can put the spool type of swiftmailer to file and then count the files in that path.
In symfony4 it could be:
test/swiftmailer.yaml
swiftmailer:
spool:
type: file
path: 'path/to/emailTestFolder'
Test file
$mails = new \FilesystemIterator('path/to/emailTestFolder', \FilesystemIterator::SKIP_DOTS);
$this->assertSame(1, iterator_count($mails));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With