Symfony News

New in Symfony 4.4: PHPUnit Assertions for Email Messages

Fabien Potencier

Contributed by
Fabien Potencier
in #32930 and #33203.

The new Mailer and Mime components were introduced in Symfony 4.3 to replace the previous solution based on SwiftMailer. In Symfony 4.4 we've improved them with new PHPUnit assertions to test emails.

The new assertions follow the same philosophy of the test assertions introduced in Symfony 4.3, with long but expressive method names:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// tests/Controller/DefaultControllerTest.php
namespace App\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

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

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

        $email = $this->getMailerMessage(0);
        $this->assertEmailHeaderSame($email, 'To', 'fabien@symfony.com');
        $this->assertEmailTextBodyContains($email, 'Welcome to Symfony!');
        $this->assertEmailAttachementCount($email, 1);
    }
}

This is the full list of mailer assertions:

  • assertEmailCount()
  • assertQueuedEmailCount()
  • assertEmailIsQueued()
  • assertEmailIsNotQueued()
  • assertEmailAttachementCount()
  • assertEmailTextBodyContains()
  • assertEmailTextBodyNotContains()
  • assertEmailHtmlBodyContains()
  • assertEmailHtmlBodyNotContains()
  • assertEmailHasHeader()
  • assertEmailNotHasHeader()
  • assertEmailHeaderSame()
  • assertEmailHeaderNotSame()
  • assertEmailAddressContains()

Be trained by Symfony experts - 2019-10-21 Lyon - 2019-10-28 Berlin - 2019-10-28 Berlin


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook