8000 [Mailer] Add EmailSubjectContains constraint (#49939) · symfony/symfony@a4d30fb · GitHub
[go: up one dir, main page]

Skip to content

Commit a4d30fb

Browse files
committed
[Mailer] Add EmailSubjectContains constraint (#49939)
Adds assertEmailSubjectContains and assertEmailSubjectNotContains
1 parent 707245c commit a4d30fb

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/Test/MailerAssertionsTrait.php

+10
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ public static function assertEmailAddressContains(RawMessage $email, string $hea
9090
self::assertThat($email, new MimeConstraint\EmailAddressContains($headerName, $expectedValue), $message);
9191
}
9292

93+
public static function assertEmailSubjectContains(RawMessage $email, string $expectedValue, string $message = ''): void
94+
{
95+
self::assertThat($email, new MimeConstraint\EmailSubjectContains($expectedValue), $message);
96+
}
97+
98+
public static function assertEmailSubjectNotContains(RawMessage $email, string $expectedValue, string $message = ''): void
99+
{
100+
self::assertThat($email, new LogicalNot(new MimeConstraint\EmailSubjectContains($expectedValue)), $message);
101+
}
102+
93103
/**
94104
* @return MessageEvent[]
95105
*/

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ public function testMailerAssertions()
104104
$this->assertEmailAttachmentCount($email, 1);
105105

106106
$email = $this->getMailerMessage($second);
107+
$this->assertEmailSubjectContains($email, 'Foo');
108+
$this->assertEmailSubjectNotContains($email, 'Bar');
107109
$this->assertEmailAddressContains($email, 'To', 'fabien@symfony.com');
108110
$this->assertEmailAddressContains($email, 'To', 'thomas@symfony.com');
109111
$this->assertEmailAddressContains($email, 'Reply-To', 'me@symfony.com');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mime\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\Mime\Email;
16+
17+
final class EmailSubjectContains extends Constraint
18+
{
19+
private $expectedSubjectValue;
20+
21+
public function __construct(string $expectedSubjectValue)
22+
{
23+
$this->expectedSubjectValue = $expectedSubjectValue;
24+
}
25+
26+
public function toString(): string
27+
{
28+
return sprintf('contains subject with value "%s"', $this->expectedSubjectValue);
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
protected function matches($other): bool
35+
{
36+
if (!$other instanceof Email) {
37+
throw new \LogicException('Can only test a message subject on a Email instance.');
38+
}
39+
40+
return str_contains((string) $other->getSubject(), $this->expectedSubjectValue);
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
protected function failureDescription($other): string
47+
{
48+
$message = 'The email subject ' . $this->toString();
49+
if ($other instanceof Email) {
50+
$message .= sprintf('. The subject was: "%s"', $other->getSubject() ?? '<empty>');
51+
}
52+
53+
return $message;
54+
}
55+
}

0 commit comments

Comments
 (0)
0