Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Mailer][Profiler] 6.2-dev Email context is empty message collected form Profiler #47993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
silverbackdan opened this issue Oct 25, 2022 · 9 comments

Comments

@silverbackdan
Copy link

silverbackdan commented Oct 25, 2022

Symfony version(s) affected

6.2-dev

Description

I test in my project that the context of the email has got the correct variables being set. But in 6.2 the message collected by the profiler now has an empty context.

How to reproduce

In my case website_name is being set.

I create the email like so:

$email = (new TemplatedEmail())
            ->to($toEmailAddress)
            ->subject($subject)
            ->htmlTemplate('@SilverbackApiComponents/emails/' . $this->getTemplate())
            ->context($context);

$context is:

array:2 [
  "user" => 'xxx'
  "website_name" => "New Website"
]

I collect the email and context from the profiler in my test:

** @var MessageDataCollector $collector */
        $collector = $this->getProfile()->getCollector('mailer');
        /** @var TemplatedEmail[] $messages */
        $messages = $collector->getEvents()->getMessages();

        Assert::assertCount(1, $messages);
        Assert::assertInstanceOf(TemplatedEmail::class, $email = $messages[0]);

        /** @var TemplatedEmail $email */
        $context = $email->getContext();

And in 6.2 beta, the context is an empty array.

Possible Solution

No response

Additional Context

No response

@silverbackdan
Copy link
Author

silverbackdan commented Nov 23, 2022

I've found the issue. It's this line here:
https://github.com/symfony/symfony/blob/6.2/src/Symfony/Bridge/Twig/Mime/BodyRenderer.php#L70

This empties the message context. By this commit here by @fabpot
cc3b871

So the message is the same in the logger, and therefore in 6.2 profiler, the message context is empty.

Do we need to empty the context?

The email is fine, but in my tests I'm collecting the email from the profiler and checking that the context was set properly by my bundle. So this causes a failing test.

@silverbackdan
Copy link
Author

It looks to me as though this line could be removed as the checks to see whether the email has already been rendered do not check the context, but instead just look to see if the templates are now null.
https://github.com/symfony/symfony/blob/6.2/src/Symfony/Bridge/Twig/Mime/BodyRenderer.php#L45

@silverbackdan
Copy link
Author

PR raised. Hopefully a nice easy fix and no tests failing when 6.2 is released.

@fabpot
Copy link
Member

fabpot commented Nov 24, 2022

See #47075 (comment) for the explanation of why we need to empty the context.

@silverbackdan
Copy link
Author

Thanks @fabpot - I commented in the PR so we can discuss there whether this is an issue and could be a problem not having this information collected for the profiler or I need to adjust how I run tests.

@stephanvierkant
Copy link
Contributor

Thanks! I've got exactly the same problem. I'm using it in my tests. Isn't this a breaking change?

@silverbackdan
Copy link
Author

I'd have thought so too @stephanvierkant but seems clearing the context is a good way to ensure that a TemplatedEmail can be serialized as many issues were coming up from users who had unserializable items in their context including Doctrine entities.

I've re-worked the PR so it should hopefully satisfy both use cases by clearing the context before sending to the bus instead of after rendering.

Busy times for the team as the 6.2 RC1 has been tagged today but hopefully we can work to getting this bug fix in place before the official release.

@silverbackdan
Copy link
Author

Closed as will not be "fixed". See PR for comments.
Solution for 6.2 will be to adjust tests accordingly.
Either add an event listener earlier to test the context, test the resulting rendered email, or unit test the context is being set alongside your functional tests.

@silverbackdan
Copy link
Author

silverbackdan commented Nov 26, 2022

This was my little solution to keep tests as they were and get the context if it's helpful for anyone. It's not an ideal approach but with limited time it gets the job done and restores the context into the email message object as we read the events again.
components-web-app/api-components-bundle@fe2f17b

For me, the main purposes of my tests were to check specific context variables had been set, there were quite a lot of checks and this was the quickest way to ensure the tests would still run as they were without rethinking too much how and where to test. My application is mainly functional tests right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants