Skip to content

[Mailer] Introduces an InMemoryTransport to save messages in memory. #32409

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
wants to merge 1 commit into from

Conversation

jschaedl
Copy link
Contributor

@jschaedl jschaedl commented Jul 6, 2019

Q A
Branch? 4.4
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #31747
License MIT
Doc PR N/A

This PR provides an in-memory transport to save sent messages in memory.

To use this transport you need to configure your mailer with this DSN: in-memory://

# .env.local .env.test

MAILER_DSN=in-memory://

Now you can retrieve the mailer.default_transport which is of type InMemoryTransport and can get the sent messages via calling the get() method.

class DefaultControllerTest extends WebTestCase
{
    public function testSomething()
    {
        $client = static::createClient();
        // ...

        $this->assertSame(200, $client->getResponse()->getStatusCode());

        /* @var InMemoryTransport $transport */
        $transport = self::$container->get('mailer.default_transport');
        $this->assertCount(1, $transport->get());
    }
}

@jschaedl jschaedl force-pushed the mailer-inmemory_transport branch from 787a563 to 839af20 Compare July 6, 2019 14:46
@jschaedl jschaedl marked this pull request as ready for review July 6, 2019 15:43
@jschaedl jschaedl force-pushed the mailer-inmemory_transport branch from 839af20 to 1d5c7ab Compare July 6, 2019 16:26
@nicolas-grekas nicolas-grekas added this to the next milestone Jul 8, 2019
@fabpot fabpot added the Mailer label Jul 8, 2019
@Koc
Copy link
Contributor

Koc commented Jul 21, 2019

Please update your PR according to changes introduced in #31946

fabpot added a commit that referenced this pull request Aug 4, 2019
This PR was merged into the 4.4 branch.

Discussion
----------

[Mailer] Add message events logger

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | refs #31592, refs #32409, refs #31947, refs #31747
| License       | MIT
| Doc PR        | n/a

To allow testing emails and for the web profiler, we need a way to store all sent/queued messages.

Commits
-------

7642178 [Mailer] added message events logger
@fabpot
Copy link
Member

fabpot commented Aug 4, 2019

I think that testing emails is orthogonal to the transport. See #32930 for an implementation that allows to receive the email AND test it at the same time in functional tests.

@fabpot fabpot closed this Aug 4, 2019
fabpot added a commit that referenced this pull request Aug 5, 2019
…for the Mailer (fabpot)

This PR was merged into the 4.4 branch.

Discussion
----------

[Mailer][Mime] Add PHPUnit constraints and assertions for the Mailer

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | refs #31592, closes #32409, closes #31947, closes #31747, closes #30850
| License       | MIT
| Doc PR        | n/a

This PR introduces PHPUnit constraints and assertions to ease testing emails in functional tests:

```php
<?php

namespace App\Tests;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MailerAssertionsTraitsTest extends WebTestCase
{
    public function testSomething()
    {
        $client = static::createClient();
        $client->request('GET', '/');

        $this->assertEmailCount(2);
        $this->assertEmailIsQueued($this->getMailerEvent(0));

        $email = $this->getMailerMessage(0);
        $this->assertEmailHasHeader($email, 'To');
        $this->assertEmailHeaderSame($email, 'To', 'fabien@symfony.com');
        $this->assertEmailTextBodyContains($email, 'Bar');
        $this->assertEmailHtmlBodyContains($email, 'Foo');
        $this->assertEmailAttachementCount($email, 1);
    }
}
```

Commits
-------

23f237b added PHPUnit constraints and assertions for the Mailer
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.4 Oct 27, 2019
@jschaedl jschaedl deleted the mailer-inmemory_transport branch February 23, 2020 08:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants