-
-
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
src/Symfony/Bundle/FrameworkBundle/Test/MailerAssertionsTrait.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Test; | ||
|
||
use PHPUnit\Framework\Constraint\LogicalNot; | ||
use Symfony\Component\Mailer\Event\MessageEvent; | ||
use Symfony\Component\Mailer\Event\MessageEvents; | ||
use Symfony\Component\Mailer\Test\Constraint as MailerConstraint; | ||
use Symfony\Component\Mime\RawMessage; | ||
use Symfony\Component\Mime\Test\Constraint as MimeConstraint; | ||
|
||
trait MailerAssertionsTrait | ||
{ | ||
public static function assertEmailCount(int $count, string $transport = null, string $message = ''): void | ||
{ | ||
self::assertThat(self::getMessageMailerEvents(), new MailerConstraint\EmailCount($count, $transport), $message); | ||
} | ||
|
||
public static function assertEmailIsQueued(MessageEvent $event, string $message = ''): void | ||
{ | ||
self::assertThat($event, new MailerConstraint\EmailIsQueued(), $message); | ||
} | ||
|
||
public static function assertEmailIsNotQueued(MessageEvent $event, string $message = ''): void | ||
{ | ||
self::assertThat($event, new LogicalNot(new MailerConstraint\EmailIsQueued()), $message); | ||
} | ||
|
||
public static function assertEmailAttachementCount(RawMessage $email, int $count, string $message = ''): void | ||
{ | ||
self::assertThat($email, new MimeConstraint\EmailAttachmentCount($count), $message); | ||
} | ||
|
||
public static function assertEmailTextBodyContains(RawMessage $email, string $text, string $message = ''): void | ||
{ | ||
self::assertThat($email, new MimeConstraint\EmailTextBodyContains($text), $message); | ||
} | ||
|
||
public static function assertEmailTextBodyNotContains(RawMessage $email, string $text, string $message = ''): void | ||
{ | ||
self::assertThat($email, new LogicalNot(new MimeConstraint\EmailTextBodyContains($text)), $message); | ||
} | ||
|
||
public static function assertEmailHtmlBodyContains(RawMessage $email, string $text, string $message = ''): void | ||
{ | ||
self::assertThat($email, new MimeConstraint\EmailHtmlBodyContains($text), $message); | ||
} | ||
|
||
public static function assertEmailHtmlBodyNotContains(RawMessage $email, string $text, string $message = ''): void | ||
{ | ||
self::assertThat($email, new LogicalNot(new MimeConstraint\EmailHtmlBodyContains($text)), $message); | ||
} | ||
|
||
public static function assertEmailHasHeader(RawMessage $email, string $headerName, string $message = ''): void | ||
{ | ||
self::assertThat($email, new MimeConstraint\EmailHasHeader($headerName), $message); | ||
} | ||
|
||
public static function assertEmailNotHasHeader(RawMessage $email, string $headerName, string $message = ''): void | ||
{ | ||
self::assertThat($email, new LogicalNot(new MimeConstraint\EmailHasHeader($headerName)), $message); | ||
} | ||
|
||
public static function assertEmailHeaderSame(RawMessage $email, string $headerName, string $expectedValue, string $message = ''): void | ||
{ | ||
self::assertThat($email, new MimeConstraint\EmailHeaderSame($headerName, $expectedValue), $message); | ||
} | ||
|
||
public static function assertEmailHeaderNotSame(RawMessage $email, string $headerName, string $expectedValue, string $message = ''): void | ||
{ | ||
self::assertThat($email, new LogicalNot(new MimeConstraint\EmailHeaderSame($headerName, $expectedValue)), $message); | ||
} | ||
|
||
public static function assertEmailAddressContains(RawMessage $email, string $headerName, string $expectedValue, string $message = ''): void | ||
{ | ||
self::assertThat($email, new MimeConstraint\EmailAddressContains($headerName, $expectedValue), $message); | ||
} | ||
|
||
/** | ||
* @return MessageEvents[] | ||
*/ | ||
public static function getMailerEvents(string $transport = null): array | ||
{ | ||
return self::getMessageMailerEvents()->getEvents($transport); | ||
} | ||
|
||
public static function getMailerEvent(int $index = 0, string $transport = null): ?MessageEvent | ||
{ | ||
return self::getMailerEvents($transport)[$index] ?? null; | ||
} | ||
< 67E6 td id="diff-79c512e0538642241b951e1141ecb1f9e98639d24d3aa1b4a8ea658c9c11d7efR100" data-line-number="100" class="blob-num blob-num-addition js-linkable-line-number js-blob-rnum"> |
|
|
/** | ||
* @return RawMessage[] | ||
*/ | ||
public static function getMailerMessages(string $transport = null): array | ||
{ | ||
return self::getMessageMailerEvents()->getMessages($transport); | ||
} | ||
|
||
public static function getMailerMessage(int $index = 0, string $transport = null): ?RawMessage | ||
{ | ||
return self::getMailerMessages($transport)[$index] ?? null; | ||
} | ||
|
||
private static function getMessageMailerEvents(): MessageEvents | ||
{ | ||
if (!self::getClient()->getRequest()) { | ||
static::fail('Unable to make email assertions. Did you forget to make an HTTP request?'); | ||
} | ||
|
||
if (!$logger = self::$container->get('mailer.logger_message_listener')) { | ||
static::fail('A client must have Mailer enabled to make email assertions. Did you forget to require symfony/mailer?'); | ||
} | ||
|
||
return $logger->getEvents(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A3E2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
.../Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/EmailController.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Mailer\MailerInterface; | ||
use Symfony\Component\Mime\Email; | ||
use Symfony\Component\Mime\NamedAddress; | ||
|
||
class EmailController | ||
{ | ||
public function indexAction(MailerInterface $mailer) | ||
{ | ||
$mailer->send((new Email())->to('fabien@symfony.com')->from('fabien@symfony.com')->subject('Foo') | ||
->addReplyTo('me@symfony.com') | ||
->addCc('cc@symfony.com') | ||
->text('Bar!') | ||
->html('<p>Foo</p>') | ||
->attach(file_get_contents(__FILE__), 'foobar.php') | ||
); | ||
|
||
$mailer->send((new Email())->to('fabien@symfony.com', 'thomas@symfony.com')->from('fabien@symfony.com')->subject('Foo') | ||
->addReplyTo(new NamedAddress('me@symfony.com', 'Fabien Potencier')) | ||
->addCc('cc@symfony.com') | ||
->text('Bar!') | ||
->html('<p>Foo</p>') | ||
->attach(file_get_contents(__FILE__), 'foobar.php') | ||
); | ||
|
||
return new Response(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Mailer/config.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Mailer/routing.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
_emailtest_bundle: | ||
resource: '@TestBundle/Resources/config/routing.yml' |
6 changes: 6 additions & 0 deletions
6
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Mailer/services.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
services: | ||
_defaults: | ||
public: true | ||
|
||
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\EmailController: | ||
tags: ['controller.service_arguments'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/Symfony/Component/Mailer/Test/Constraint/EmailCount.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Mailer\Test\Constraint; | ||
|
||
use PHPUnit\Framework\Constraint\Constraint; | ||
use Symfony\Component\Mailer\Event\MessageEvents; | ||
|
||
final class EmailCount extends Constraint | ||
{ | ||
private $expectedValue; | ||
private $transport; | ||
|
||
public function __construct(int $expectedValue, string $transport = null) | ||
{ | ||
$this->expectedValue = $expectedValue; | ||
$this->transport = $transport; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function toString(): string | ||
{ | ||
return sprintf('%shas sent "%d" emails', $this->transport ? $this->transport.' ' : '', $this->expectedValue); | ||
} | ||
|
||
/** | ||
* @param MessageEvents $events | ||
* | ||
* {@inheritdoc} | ||
*/ | ||
protected function matches($events): bool | ||
{ | ||
return $this->expectedValue === \count($events->getEvents($this->transport)); | ||
} | ||
|
||
/** | ||
* @param MessageEvents $events | ||
* | ||
* {@inheritdoc} | ||
*/ | ||
protected function failureDescription($events): string | ||
{ | ||
return sprintf('the Transport %s (%d sent)', $this->toString(), \count($events->getEvents($this->transport))); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.