From 55ba7b8f22cacfdafdd292ddcf3b9679da181c1e Mon Sep 17 00:00:00 2001 From: johan Vlaar Date: Mon, 12 Jun 2023 11:20:03 +0200 Subject: [PATCH] [Mailer] Add EmailSubjectContains constraint (#49939) Adds assertEmailSubjectContains and assertEmailSubjectNotContains --- .../Test/MailerAssertionsTrait.php | 10 ++++ .../Tests/Functional/MailerTest.php | 2 + .../Test/Constraint/EmailSubjectContains.php | 47 +++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 src/Symfony/Component/Mime/Test/Constraint/EmailSubjectContains.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Test/MailerAssertionsTrait.php b/src/Symfony/Bundle/FrameworkBundle/Test/MailerAssertionsTrait.php index d6b29d2b5a0c6..83643421ef880 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Test/MailerAssertionsTrait.php +++ b/src/Symfony/Bundle/FrameworkBundle/Test/MailerAssertionsTrait.php @@ -90,6 +90,16 @@ public static function assertEmailAddressContains(RawMessage $email, string $hea self::assertThat($email, new MimeConstraint\EmailAddressContains($headerName, $expectedValue), $message); } + public static function assertEmailSubjectContains(RawMessage $email, string $expectedValue, string $message = ''): void + { + self::assertThat($email, new MimeConstraint\EmailSubjectContains($expectedValue), $message); + } + + public static function assertEmailSubjectNotContains(RawMessage $email, string $expectedValue, string $message = ''): void + { + self::assertThat($email, new LogicalNot(new MimeConstraint\EmailSubjectContains($expectedValue)), $message); + } + /** * @return MessageEvent[] */ diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php index 942dd05178b24..0ca437c1e8d9c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php @@ -104,6 +104,8 @@ public function testMailerAssertions() $this->assertEmailAttachmentCount($email, 1); $email = $this->getMailerMessage($second); + $this->assertEmailSubjectContains($email, 'Foo'); + $this->assertEmailSubjectNotContains($email, 'Bar'); $this->assertEmailAddressContains($email, 'To', 'fabien@symfony.com'); $this->assertEmailAddressContains($email, 'To', 'thomas@symfony.com'); $this->assertEmailAddressContains($email, 'Reply-To', 'me@symfony.com'); diff --git a/src/Symfony/Component/Mime/Test/Constraint/EmailSubjectContains.php b/src/Symfony/Component/Mime/Test/Constraint/EmailSubjectContains.php new file mode 100644 index 0000000000000..64be30cd2f319 --- /dev/null +++ b/src/Symfony/Component/Mime/Test/Constraint/EmailSubjectContains.php @@ -0,0 +1,47 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Mime\Test\Constraint; + +use PHPUnit\Framework\Constraint\Constraint; +use Symfony\Component\Mime\Email; + +final class EmailSubjectContains extends Constraint +{ + public function __construct( + private readonly string $expectedSubjectValue, + ) { + } + + public function toString(): string + { + return sprintf('contains subject with value "%s"', $this->expectedSubjectValue); + } + + protected function matches($other): bool + { + if (!$other instanceof Email) { + throw new \LogicException('Can only test a message subject on a Email instance.'); + } + + return str_contains((string) $other->getSubject(), $this->expectedSubjectValue); + } + + protected function failureDescription($other): string + { + $message = 'The email subject '.$this->toString(); + if ($other instanceof Email) { + $message .= sprintf('. The subject was: "%s"', $other->getSubject() ?? ''); + } + + return $message; + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy