Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Mailer] Adds assertEmailSubjectContains and assertEmailSubjectNotContains methods #50200

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 1 commit into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', '[email protected]');
$this->assertEmailAddressContains($email, 'To', '[email protected]');
$this->assertEmailAddressContains($email, 'Reply-To', '[email protected]');
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 <[email protected]>
*
* 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;
}
}