-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Closed
Description
Description
In a WebTestCase
I would like to be able to test the subject on emails as well as the body:
self::assertEmailSubjectContains($email, 'This is the subject');
Here's my own version of the constraint right now:
<?php
declare(strict_types=1);
namespace App\Tests\Constraint;
use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\Mime\Email;
final class EmailSubjectContains extends Constraint
{
private string $expectedText;
public function __construct(string $expectedText)
{
$this->expectedText = $expectedText;
}
public function toString(): string
{
return sprintf('contains "%s"', $this->expectedText);
}
protected function matches($other): bool
{
if (!$other instanceof Email) {
throw new \LogicException('Can only test a message subject on an Email instance');
}
return str_contains((string) $other->getSubject(), $this->expectedText);
}
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;
}
}
If this is good enough I can make a PR with it :)
Example
No response