Skip to content
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

seeEmailIsSent() not working when email is sent by a Symfony/Doctrine entity listener #5252

Closed
ThomasLandauer opened this issue Nov 1, 2018 · 5 comments

Comments

@ThomasLandauer
Copy link
Member

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
@picoss
Copy link

picoss commented Jan 14, 2019

I've had the same issue trying to test emails sent from a doctrine entity listener.

The only way I've found to get sent emails from profiler is to persist the Swift_Plugins_MessageLogger plugin.

<?php
namespace App\Tests\Helper;

class Symfony extends \Codeception\Module\Symfony
{
    /**
     * {@inheritdoc}
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->persistService('swiftmailer.mailer.default.plugin.messagelogger', true);
    }
}

As far as I understand, Symfony and Doctrine doesn't share the same container when using both Symfony and Doctrine2 modules in codeception.
Persisting Swift_Plugins_MessageLogger service will share this service between Symfony and Doctrine2.

Not sure it's the right solution, but it works for me.

@ThomasLandauer
Copy link
Member Author

@TavoNiievez Do you have an idea for Symfony Mailer?

@ghost
Copy link

ghost commented Sep 12, 2020

@ThomasLandauer Can you confirm if after sending the email you do some kind of redirection?

@ThomasLandauer
Copy link
Member Author

More details: The controller is an api, it just persists the incoming data, then:

return new Response('OK'); // This is not packed in an HTML skeleton to prevent the Symfony toolbar from loading - but this shouldn't make a difference in TEST environment

The entity listener kicks in postPersist; it sends the email with Symfony Mailer, writes this fact to the entity and persists it again.

In Codeception I'm doing:

$I->sendPOST('/api/...');
$I->seeEmailIsSent();

And the error message I'm getting is:

Failed asserting that 0 is greater than 0.

The workaround I'm using right now: With $I->grabEntityFromRepository(...) I check if the "emailed" flag (from the listener) is set. This works fine.

Contrary to what I said above, I cannot get it to work anymore if I move the email sending code from the listener to the controller! So maybe the cause of the problem is that I'm using sendPOST() from REST module, and seeEmailIsSent() from Symfony module can't "access" that? If this is the case => Any ideas for a solution?

@ThomasLandauer
Copy link
Member Author

Closing this, as it was transferred to Codeception/module-symfony#90

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

No branches or pull requests

2 participants