Can we actually send out mails during semi-automatic testing?

Viewed 29

We are using unit / integration tests during Shopware 6 development.

One technique we use is to disable database transaction behaviour to see the results for example of fixtures in the admin panel, for an easier debugging / understanding:

trait IntegrationTestBehaviour
{
    use KernelTestBehaviour;
//   use DatabaseTransactionBehaviour;
    use FilesystemBehaviour;
    use CacheTestBehaviour;
    use BasicTestDataBehaviour;
    use SessionTestBehaviour;
    use RequestStackTestBehaviour;
}

Similar to this it would be helpful to send out actual emails during some tests (only for development, not in the CI and so on).

It is already possible to automatically test emails like this:

 $eventDidRun = false;
 $listenerClosure = function (MailSentEvent $event) use (&$eventDidRun): void {
     $eventDidRun = true;
 };
 $this->addEventListener($dispatcher, MailSentEvent::class, $listenerClosure);

 // do something that sends an email

 static::assertTrue($eventDidRun, 'The mail.sent Event did not run');

But sometimes we want to manually see the actual email.

The .env.test already contains a valid mailer URL:

MAILER_URL=smtp://x:y@smtp.mailtrap.io:2525?encryption=tls&auth_mode=login

But still no mails get send during the test.

While I guess that this is fully intentional, is there some method to workaround the blockage of getting mails sent during testing?

1 Answers

The reason is the MAILER_URL variable is pre-set to null://localhost in the phpunit.xml.dist of the platform repository:

<server name="MAILER_URL" value="null://localhost"/>

You could set the MAILER_URL environment variable yourself before the tests of the class are executed:

/**
 * @beforeClass
 */
public static function setMailerUrl(): void
{
    $_SERVER['MAILER_URL'] = 'smtp://x:y@smtp.mailtrap.io:2525?encryption=tls&auth_mode=login';
}
Related