Codeception / module-symfony

Codeception module for testing apps using Symfony framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Send an email in a Cest file in an acceptance test (Symfony service)

ThomasLandauer opened this issue · comments

What are you trying to achieve?

Send an email from a private method in a Cest file in an acceptance test. I'm using Symfony's Mailer service to set up the message and send it when the test is run:

public function tryWhatever (AcceptanceTester $I)
{
    // ...
    $mailer = $I->grabService('mailer');
    $message = new \Swift_Message();
    $message
        ->setTo(...)
        ->setSubject('My subject')
        ->setBody('Testmail', 'text/plain')
    ;
    $mailer->send($message);
}

What do you get instead?

No email :-(
But the mailer returns 1 (meaning 1 message has been sent).
The same code does work in a controller.

So I'm suspecting that emails are being "caught" internally (as in functional tests) and not actually being sent - is this the case? Can I prevent the catching somehow?

Details

  • Codeception version: 2.3.3
class_name: AcceptanceTester
modules:
    enabled:
        - Symfony:
            part: services

Did you enable email delivering at swiftmailer configuration? It is disabled in config_test.yml by default:
https://github.com/symfony/symfony-standard/blob/3.3/app/config/config_test.yml#L16

@Tekill Thanks, sounded good, but it doesn't make a difference :-(

Probably you are using the configuration where real delivery is disabled.
Not a Symfony expert so can't provide you with more ideas.
Can you debug the execution to see what happens when email is sent?

@ThomasLandauer have you solved this problem? thanks for answer!

Since this issue describes problems in unsupported versions of Codeception (2.3) and most likely too old version of Swift Mailer, I am closing this issue.

If it turns out that this problem is also present in Symfony Mailer or the latest supported version of Swiftmailer 6.2.4 on at least Symfony v3.4.47 , and affects Codeception 4 and at least version 1.6of Codeception Symfony module, please open another issue with an updated description.

In that case, please check if it really is an acceptance test where this code is located, as well as that the $mailer object is really an instance of the expected class, for example with assertInstanceOf.