symfony icon indicating copy to clipboard operation
symfony copied to clipboard

[Mailer] Testing transport is broken using 7.2

Open Fabrn opened this issue 8 months ago • 4 comments

Symfony version(s) affected

7.2.4

Description

Hello !

I've been trying to upgrade our project from 7.1.8 to 7.2.4, however, I'm getting an issue while running our test suites. We have a test that ensures that transport is defined using the X-Transport header. Even though the documentation says that the header is removed after sending the email, we could test the header like so :

$email = $this->getMailerMessage();

self::assertEmailHeaderSame($email, 'X-Transport', 'mailjet');

Now that I upgraded to 7.2.4, the header is removed. Then, I tried to test that the message was successfully sent using the mailjet transport using the transport parameter of the getMailerMessage method, but my message will not be returned.

The transport seems lost in test environment. Is it a bug ? Am I missing something ? Is there a better way to test the transport ?

How to reproduce

Setup a test multi-transport mailer configuration :

framework:
    mailer:
        transports:
            transport_a: '%env(MAILER_DSN_TRANSPORT_A)%'
            transport_b: '%env(MAILER_DSN_TRANSPORT_B)%'

Send a mail using a specific transport :

$message->getHeaders()->addTextHeader('X-Transport', 'transport_b');

$this->mailer->send($message);

Write a functional test checking for transport :

class MyTest extends KernelTestCase
{
    use MailerAssertionsTrait;

    public function test(): void
    {
        // send mail

        $email = $this->getMailerMessage();

        self::assertEmailHeaderSame($email, 'X-Transport', 'transport_b');
    }
}

Possible Solution

No response

Additional Context

No response

Fabrn avatar Mar 26 '25 12:03 Fabrn

Can you create a small example application that allows to reproduce your issue?

xabbuh avatar Mar 26 '25 12:03 xabbuh

Hello. Not for now but I can try my best to try it this evening

Fabrn avatar Mar 26 '25 12:03 Fabrn

Hello there, sorry for being so late.

I've built my POC this morning, however I am not able to reproduce the issue yet. This might be something with our project. I will try to look at this more closely and debug everything on Monday and I'm coming back to you with some news.

Have a nice day

Fabrn avatar Mar 29 '25 09:03 Fabrn

Hello !

After investigating the issue, I found out that using Symfony 7.1, the Mailer::$transport property is initialized using the NullTransport in test environment, which is fine since DSN is null. NullTransport does not manipulate headers since AbstractTransport does not. However, simply by upgrading to 7.2, I found out that the Symfony\Component\Mailer\Transport\Transports replaces the NullTransport, which is a shame since it removes the header.

I think it is strange since our dsn are null://null, which is perfectly fine for 7.1 but not for 7.2. Is it something with DSN resolving ? Has anything changed for transport configuration ?

I am still investigating but I am already posting if anyone can help me here 😄

Thanks in advance

Fabrn avatar Mar 31 '25 08:03 Fabrn

Hi @Fabrn any new insights?

OskarStark avatar Jun 30 '25 11:06 OskarStark

Hello ! Sorry for leaving this issue dead, I failed to make a reproducer and completly forgot about it. I'm going to try again this week, coming back to you real quick.

Fabrn avatar Jun 30 '25 20:06 Fabrn

Well I've tried to look into it and everything works fine on a fresh project. I guess there's something wrong with the project at my work somehow. Let's just close this issue since no one else seem to encounter this issue and I am not able to reproduce it.

Thank you for your time !

Fabrn avatar Jul 01 '25 20:07 Fabrn

Thanks for the feedback, much appreciated 🙏

OskarStark avatar Jul 01 '25 20:07 OskarStark