0

I want to test email delivery in Behat tests in Symfony 5. Apparently, there is a MailerAssertionsTrait in the PHPUnit package for Symfony which uses the MessageLoggerListener to check the sent emails. Following this tutorial https://swag.industries/test-emails-with-symfony/, I adapted that trait for my Behat context.

Unfortunately the service mailer.message_logger_listener does not seem to be available in the Symfony container. No matter if I set the MAILER_DSN to null://null or to an actually working transport.

I saw that this listener is being registered in the file vendor/symfony/framework-bundle/Resources/config/mailer_debug.php, which leads me to believe that I have to enable debug mode for the symfony/mailer package in some way. But I did not yet find out how exactly that is done.

How do I enable the MessageLoggerListener for the symfony/mailer package in my test environment?

Subsurf
  • 1,256
  • 1
  • 17
  • 28
  • You can try to copy service definition from `vendor/symfony/framework-bundle/Resources/config/mailer_debug.php` and add it to `config/services_test.yaml`. – vstelmakh Nov 01 '21 at 11:04
  • @vstelmakh thanks for your reply. I didn't think of configuring the package in my own app config, but it makes sense. It didn't load the file at all at first until I figured out that as of Symfony 5.1, `*.php` config files are not loaded anymore by default, so I had to change my `Kernel.php` to load `*.php` config files too. The file is now definitely being loaded, but the `MessageLoggerListener` still can't be retrieved. Calling `$container->has('mailer.message_logger_listener')` still returns false. – Subsurf Nov 12 '21 at 14:23
  • I now found out that the `mailer.message_logger_listener` had already been registered correctly, but it is set to private, as any other service in the service container that is not explicitly set to being public, hence I could not fetch it from the container using `has()` and `get()`. I could inject it to my Behat test case using constructor injection. However, the logger seems to be meant to be used only with the `mailer.data_logger` because it does register the sent email, but when I try to get the recorded events, they have been reset... – Subsurf Nov 20 '21 at 17:07

0 Answers0