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 thetest 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.phpnamespaceApp\Tests\Controller;useSymfony\Bundle\FrameworkBundle\Test\WebTestCase;classDefaultControllerTestextendsWebTestCase{publicfunctiontestSomething(){$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()