English spoken conference

Symfony 5: The Fast Track

A new book to learn about developing modern Symfony 5 applications.

Support this project

New in Symfony 4.4: PHPUnit Assertions for Email Messages

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', '[email protected]');
        $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()

New in Symfony 4.4: PHPUnit Assertions for Email Messages symfony.com/blog/new-in-symfony-4-4-phpunit-assertions-for-email-messages

Tweet this

Comments

So great! So useful! Thanks!
Login with SymfonyConnect to post a comment