8000 Impossible to test sending mail from a command with the MailerAssertionsTrait · Issue #38035 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

Impossible to test sending mail from a command with the MailerAssertionsTrait #38035

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
bdelesalle opened this issue Sep 2, 2020 · 2 comments

Comments

@bdelesalle
Copy link

Symfony version(s) affected: 4.4.*

Description

When you want to test the sending of mail from a command, you cannot use the MailerAssertionsTrait since it expects an HTTP request.

private static function getMessageMailerEvents(): MessageEvents
{
    if (!self::getClient()->getRequest()) {
        static::fail('Unable to make email assertions. Did you forget to make an HTTP request?');
    }

    if (!$logger = self::$container->get('mailer.logger_message_listener')) {
        static::fail('A client must have Mailer enabled to make email assertions. Did you forget to require symfony/mailer?');
    }

    return $logger->getEvents();
}

How to reproduce

<?php

namespace App\Tests;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\MailerAssertionsTrait;
use Symfony\Component\Console\Tester\CommandTester;

class MyCommandTest extends WebTestCase
{
    use MailerAssertionsTrait;

    public function testCommand(): void
    {
        $application = new Application(self::$kernel);
        $command = $application->find('app:my-command');
        $commandTester = new CommandTester($command);

        $commandTester->execute(['command' => $command->getName()]);

        self::assertEmailCount(1);
    }
}

Possible Solution

Remove the HTTP request check.

private static function getMessageMailerEvents(): MessageEvents
{
    if (!$logger = self::$container->get('mailer.logger_message_listener')) {
        static::fail('A client must have Mailer enabled to make email assertions. Did you forget to require symfony/mailer?');
    }

    return $logger->getEvents();
}
@jschaedl
Copy link
8000 Contributor
jschaedl commented Sep 7, 2020

The check for an existing $request has already been removed in 5.1. See 5.0 vs 5.1

I think we can do the same in 4.4.

@fabpot
Copy link
Member
fabpot commented Sep 7, 2020

This was done in #35534

fabpot added a commit that referenced this issue Sep 7, 2020
…chaedl)

This PR was merged into the 4.4 branch.

Discussion
----------

[Mailer] Remove unnecessary check for  existing request

| Q             | A
| ------------- | ---
| Branch?       | 4.4 <!-- see below -->
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #38035 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT
| Doc PR        | - <!-- required for new features -->
<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/releases):
 - Always add tests and ensure they pass.
 - Never break backward compatibility (see https://symfony.com/bc).
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too.)
 - Features and deprecations must be submitted against branch master.
-->

Commits
-------

3feff72 remove unnecessary check for  existing request
@fabpot fabpot closed this as completed Sep 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants
0