8000 [Mailer] since 5.2.0, testing that an email has been sent with mailer, fails · Issue #39511 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

[Mailer] since 5.2.0, testing that an email has been sent with mailer, fails #39511

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
epitre opened this issue Dec 15, 2020 · 12 comments · Fixed by #42771
Closed

[Mailer] since 5.2.0, testing that an email has been sent with mailer, fails #39511

epitre opened this issue Dec 15, 2020 · 12 comments · Fixed by #42771

Comments

@epitre
Copy link
Contributor
epitre commented Dec 15, 2020

Symfony version(s) affected: 5.2.0

Description
In a functionnal test, I get an error when I try to test that an email has been sent with mailer

How to reproduce

<?php

namespace App\Tests\Unit\Manager;

use App\Manager\EmailManager;
use Psr\Log\LoggerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Bundle\FrameworkBundle\Test\MailerAssertionsTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;

class EmailManagerTest extends KernelTestCase
{
    use MailerAssertionsTrait;

    private $mailer;

    protected function setUp(): void
    {
        self::bootKernel();

        $this->mailer = self::$container->get('mailer.mailer');
    }

    public function testSendEmail()
    {
        $emailManager = new EmailManager(
            $this->createMock(LoggerInterface::class),
            $this->mailer
        );

        $fromEmail = 'from@dev.local';
        $fromName = 'dev from';
        $from = new Address($fromEmail, $fromName);
        $to = 'to@dev.local';
        $subject = 'test subject';
        $body = 'My own test body';

        $emailManager->sendEmail(
            $from,
            $to,
            $subject,
            $body
        );

        $this->assertEmailCount(1); // error
    }
}

Possible Solution
no idea

Additional context
the error I get is :

A client must have Mailer enabled to make email assertions. Did you forget to require symfony/mailer?

/srv/app/vendor/symfony/framework-bundle/Test/MailerAssertionsTrait.php:129
/srv/app/vendor/symfony/framework-bundle/Test/MailerAssertionsTrait.php:25
@epitre epitre added the Bug label Dec 15, 2020
@epitre epitre changed the title since 5.2.0, testing that an email has been sent with mailer, fails [Mailer] since 5.2.0, testing that an email has been sent with mailer, fails Dec 15, 2020
@epitre
Copy link
Contributor Author
epitre commented Dec 15, 2020

If I downgrade framework-bundle to 5.1, it works ^^

@jarekkowol
Copy link

I have the same issue. Already have installed symfony/mailer

@xabbuh
Copy link
Member
xabbuh commented Feb 3, 2021

Can you create a small example application that allows to reproduce your issue?

@jarekkowol
Copy link
jarekkowol commented Feb 3, 2021

A very simplified version of functional test:

class RegistrationFTest extends WebTestCase
{
    protected KernelBrowser $client;

    public function setUp(): void
    {
        $this->client = static::createClient([], ['CONTENT_TYPE' => 'application/json']);
    }

    public function testRegister()
    {
        $payload = [
            'email' => 'newuser@test.com',
            'password' => 'password'
        ];

        $this->client->request('POST', '/register', [], [], [], json_encode($payload));
        $this->assertResponseIsSuccessful();
       
        $this->assertEmailCount(1);
        $this->assertEmailIsQueued($this->getMailerEvent(0));

        $email = $this->getMailerMessage(0);
        $this->assertEmailHeaderSame($email, 'To', $payload['email']);
        $this->assertEmailTextBodyContains($email, 'XXX');
        $this->assertEmailHtmlBodyContains($email, 'XXX');
    }
}

@xabbuh
Copy link
Member
xabbuh commented Feb 3, 2021

Please create a small application that we can clone to investigate what's happening.

@jarekkowol
Copy link

Here you are: https://github.com/jarekkowol/debug_sf52
And thank you for your involvement :)

@xabbuh
Copy link
Member
xabbuh commented Feb 6, 2021

Enabling the profiler in the test environment will solve your issue:

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

I am not sure though if that is desired.

@jarekkowol
Copy link

That's right, it solved the problem

@epitre
Copy link
Contributor Author
epitre commented May 26, 2021

@xabbuh Should we close this issue ?
your "solution" solves the problem.
Is there a PR that should be created ?

@xabbuh
Copy link
Member
xabbuh commented May 26, 2021

I believe it would still be worth to investigate why it worked before and if couldn't come back to that state easily. Would you be up to give that a try?

@epitre
Copy link
Contributor Author
epitre commented May 26, 2021

Before submitting this issue, I did try to understand what was going on but failed :'(
I guess my symfony "expertise" has not reach that level yet. Sorry :(

@AEdV
Copy link
AEdV commented Jun 14, 2021

Hello,
I have the same problem and the above solution did not work for me ; I'm working with Symfony 5.3.
Does anyone have another idea?

nicolas-grekas added a commit that referenced this issue Sep 7, 2021
…wizardz)

This PR was submitted for the 5.4 branch but it was merged into the 5.3 branch instead.

Discussion
----------

[FrameworkBundle] Match 5.3 and 5.1 mailer configuration

| Q             | A
| ------------- | ---
| Branch?       | 5.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #39511
| License       | MIT
| Doc PR        |

Looks like the listener has been in the main config file in 5.1 branch but has not been moved to 5.2 branch during the refactoring.

Commits
-------

6214f3a Match 5.1 mailer configuration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
5283
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants
0