-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Conversation
fbee460
to
05c1ae4
Compare
05c1ae4
to
c96a967
Compare
Now with some functional tests. |
03ea45e
to
e1645f0
Compare
src/Symfony/Component/Mime/Test/Constraint/EmailAttachmentCount.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Mime/Test/Constraint/EmailHtmlBodyContains.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Mime/Test/Constraint/EmailTextBodyContains.php
Outdated
Show resolved
Hide resolved
src/Symfony/Bundle/FrameworkBundle/Test/MailerAssertionsTrait.php
Outdated
Show resolved
Hide resolved
...mfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml
Outdated
Show resolved
Hide resolved
0259074
to
a1ab4d5
Compare
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 $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'); |
a1ab4d5
to
c263f21
Compare
2ed6076
to
9ea3b0e
Compare
@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). |
9ea3b0e
to
23f237b
Compare
…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
This PR introduces PHPUnit constraints and assertions to ease testing emails in functional tests: