module-symfony
module-symfony copied to clipboard
`seeEmailIsSent()` not working when email is sent by a Symfony/Doctrine entity listener
When using an Entity Listener to send an email, seeEmailIsSent()
doesn't notice it. When I do the same email command in the controller, it works.
The actual email delivery works in both cases (as shown by mailcatcher).
Details
- Codeception version: 2.5.1
- PHP Version: 7.2
It seems like something went wrong when GitHub tried to transfer this issue. I am attaching an image of Google's cached version of the most relevant comment.
The Mail Logger for Symfony Mailer also has this problem.
I tried to permanently persist the services as @picoss suggested for Swift Mailer, however it didn't work. So unfortunately this issue will have to be open a little longer.
I have the same issue when sending e-mail this way using Swiftmailer. What more, after update from Symfony 4.4 and older Codeception to Symfony 5.2 and latest Codeception, I get Emails can't be tested without Symfony Mailer service.
. If I install symfony/mailer
, error disappears but it says that 0 emails were sent.
You should not be sending emails in the entity listeners. Nor in doctrine lifecycle. Why? Her is why https://youtu.be/WW2qPKukoZY?t=1290
@Legion112 Thank you very much for the video. If this is 'the way it is', what would really close this issue would be a link to the official documentation where the same thing that is mentioned in the video is mentioned or where alternatives that someone could use instead of Doctrine Entity Listeners are mentioned.
do you think it's ok @ThomasLandauer ?
In short: I don't agree ;-)
In long: Codeception is a testing framework, not a best-practice guide. So besides the fact that for any best-practice there will always be some (strange) situations where breaking them is a good idea. My main argument is: It's not upon us to judge! If somebody does want to send an email from a listener - well, why not - it's their app, not ours ;-)
And besides not agreeing in general, I don't even agree with the video sequence itself: What's wrong with sending out an email on the "persist" event (i.e. when the record is created)? So the essence of the serialize/unserialize example is for me:
Watch out, cause if you don't do it right, you can really mess it up!
Which is a warning you can apply to anything in programming... ;-)
Finally: Following this same argument, we'd also have to remove typeInPopup, cause using window.prompt
is certainly an anti-pattern too...