Skip to content

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

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

Merged
merged 1 commit into from
Aug 5, 2019

Conversation

fabpot
Copy link
Member

@fabpot fabpot commented Aug 4, 2019

Q A
Branch? 4.4
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
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

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);
    }
}

@fabpot
Copy link
Member Author

fabpot commented Aug 4, 2019

image

@fabpot
Copy link
Member Author

fabpot commented Aug 4, 2019

Now with some functional tests.

@fabpot fabpot force-pushed the mailer-test-assertions branch 3 times, most recently from 03ea45e to e1645f0 Compare August 5, 2019 05:57
@fabpot fabpot force-pushed the mailer-test-assertions branch 2 times, most recently from 0259074 to a1ab4d5 Compare August 5, 2019 13:55
@javiereguiluz
Copy link
Member

The proposed asserts are nice! Thanks.

I wonder if we need asserts for addresses. The mailer component allows to define email addresses in multiple ways (see https://symfony.com/doc/current/mailer.html#email-addresses). If I define some address as Jane Smith <jane@example.com> ... which of the following would work?

$this->assertEmailHeaderSame($email, 'From', 'jane@example.com');
$this->assertEmailHeaderSame($email, 'From', 'Jane Smith');
$this->assertEmailHeaderSame($email, 'From', 'Jane Smith <jane@example.com>');

If others agree that this is confusing and also a common thing to assert, then we could add some assert for addresses which accept only a plain email address no matter how you defined the address:

$this->assertEmailFromAddress($email, 'jane@example.com');

Related to this, what happens with multiple addresses?

$this->assertEmailToAddressContains($email, 'john@example.com');

@fabpot fabpot force-pushed the mailer-test-assertions branch from a1ab4d5 to c263f21 Compare August 5, 2019 15:46
@fabpot fabpot force-pushed the mailer-test-assertions branch 2 times, most recently from 2ed6076 to 9ea3b0e Compare August 5, 2019 16:15
@fabpot
Copy link
Member Author

fabpot commented Aug 5, 2019

@javiereguiluz Good idea. Implemented:

$this->assertEmailAddressContains($email, 'To', 'fabien@symfony.com');
$this->assertEmailAddressContains($email, 'To', 'thomas@symfony.com');
$this->assertEmailAddressContains($email, 'Reply-To', 'me@symfony.com');

Works with headers that can contain only 1 or several addresses (also with named addresses).

@fabpot fabpot force-pushed the mailer-test-assertions branch from 9ea3b0e to 23f237b Compare August 5, 2019 16:16
@fabpot fabpot merged commit 23f237b into symfony:4.4 Aug 5, 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
@fabpot fabpot deleted the mailer-test-assertions branch August 5, 2019 16:41
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.4 Oct 27, 2019
This was referenced Nov 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants