Skip to content

Commit

Permalink
[Mailer] Add EmailSubjectContains constraint (#49939)
Browse files Browse the repository at this point in the history
Adds assertEmailSubjectContains and assertEmailSubjectNotContains
  • Loading branch information
johanadivare committed Jun 12, 2023
1 parent 52a9292 commit 55ba7b8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Test/MailerAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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\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() ?? '<empty>');
}

return $message;
}
}

0 comments on commit 55ba7b8

Please sign in to comment.