Skip to content

Incomplete Mail testing section in 5.3, no "how to" on email testing #15922

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
AntonioCS opened this issue Oct 13, 2021 · 11 comments
Closed

Incomplete Mail testing section in 5.3, no "how to" on email testing #15922

AntonioCS opened this issue Oct 13, 2021 · 11 comments
Labels
hasPR A Pull Request has already been submitted for this issue. Mailer Testing

Comments

@AntonioCS
Copy link

Hello,

I googled for email testing in symfony and got this page: https://symfony.com/doc/4.1/email/testing.html
This is for 4.1 but I am using symfony 5.3 so I clicked the link (which is on the page) the updated version of this page
I was taken to this page: https://symfony.com/doc/current/mailer.html

This page is a "How to" page and it's missing a testing section. The reader is left to guess how to test emails in symfony 5.3.
I have asked this in the symfony slack #support channel and got a bit of help but nothing conclusive.

I did a bit more googling and found: symfony/symfony#39511
Where is seems the user that posted the issue has a similar issue and when @xabbuh suggested enabling the profiler all the issues went away.
So I've done:

#config/packages/test/framework.yaml
framework:
    profiler: ~

I've also done:

       $this->client = static::createClient();

      .....
            $this->client->enableProfiler();
       ....

        return $this->client->request(....

But when I do this:

self::assertEmailCount(1);

I still get an error saying I have sent zero emails.

When I run code locally I can see an email in mailhog. So I know an email is being sent.
I think email testing is crucial in any application. I would do PR on how to test emails, if I actually new all the steps to test email, but I don't :(

Can someone please update the docs regarding this issue?

@isarastov
Copy link

assertEmailCount works for me, here's a simple project where the test passes: https://github.com/isarastov/symfony-test-send-mail

@AntonioCS
Copy link
Author

It's still missing in the documentation page I linked.
And I have a similar use case, but it's in a DataPersister of Api Platform (I assume it should still register the email being sent)

@OskarStark
Copy link
Contributor

Did you try https://github.com/zenstruck/mailer-test

cc @kbond

@AntonioCS
Copy link
Author

@OskarStark I did not, was hoping that what symfony provides would be enough.
I must have something off somewhere as @isarastov repo is basically the code I have.

@kbond
Copy link
Member

kbond commented Oct 13, 2021

The native Symfony email assertions don't use the profiler (but maybe I'm wrong about that)?

zenstruck/mailer-test is mostly an opinionated, alternative to the native email assertions. One feature it does provide is persisting the sent emails between kernel reboots. Could this be your issue?

@AntonioCS
Copy link
Author

@kbond the email assertions are done in a method called after the test is run, it's not on another test (I assume that would be where the kernel is rebooted? )

@kbond
Copy link
Member

kbond commented Oct 13, 2021

I meant if you make multiple requests within the same test the kernel will be rebooted.

$client = static::createClient();

$client->request('GET', '/sends-email');
$client->request('GET', '/something-else');

self::assertEmailCount(1); // fails because the kernel was rebooted and the sent messages lost

@kbond
Copy link
Member

kbond commented Oct 13, 2021

Another possibility: does your request redirect after sending the email? I think this reboots the kernel so you'd lose the sent emails.

@AntonioCS
Copy link
Author

It's a post request sent to and endpoint generated by API Platform, with a custom DataPersister, so I'm not 100% sure but I think there are no redirects.
I am making multiple requests (testing the endpoint with many payloads) but the code that contains the email assertion is called immediately after a request (so in theory there is no reboot).

@xabbuh xabbuh added hasPR A Pull Request has already been submitted for this issue. Mailer Testing labels Nov 9, 2021
@myselfhimself
Copy link

Did you try https://github.com/zenstruck/mailer-test

cc @kbond

This package works well with me (I had two test cases where the first succeeded finding one sent email, while the one right after would fail detecting a second mail sent). Thank you

@wouterj
Copy link
Member

wouterj commented Oct 17, 2022

Closing, as this was fixed by #16195

@wouterj wouterj closed this as completed Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hasPR A Pull Request has already been submitted for this issue. Mailer Testing
Projects
None yet
Development

No branches or pull requests

7 participants