Skip to content
Open
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
7 changes: 3 additions & 4 deletions src/Codeception/Module/Symfony/MailerAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Symfony\Component\Mailer\Event\MessageEvents;
use Symfony\Component\Mailer\EventListener\MessageLoggerListener;
use Symfony\Component\Mailer\Test\Constraint as MailerConstraint;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\RawMessage;

use function array_key_last;

Expand Down Expand Up @@ -100,9 +100,8 @@ public function dontSeeEmailIsSent(): void
* $I->assertSame('john_doe@example.com', $address->getAddress());
* ```
*/
public function grabLastSentEmail(): ?Email
public function grabLastSentEmail(): ?RawMessage
{
/** @var Email[] $emails */
$emails = $this->getMessageMailerEvents()->getMessages();

return $emails ? $emails[array_key_last($emails)] : null;
Expand All @@ -119,7 +118,7 @@ public function grabLastSentEmail(): ?Email
* $emails = $I->grabSentEmails();
* ```
*
* @return \Symfony\Component\Mime\RawMessage[]
* @return RawMessage[]
*/
public function grabSentEmails(): array
{
Expand Down
14 changes: 8 additions & 6 deletions src/Codeception/Module/Symfony/MimeAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Constraint\LogicalNot;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Message;
use Symfony\Component\Mime\RawMessage;
use Symfony\Component\Mime\Test\Constraint as MimeConstraint;

use function sprintf;
Expand All @@ -23,7 +25,7 @@ trait MimeAssertionsTrait
* $I->assertEmailAddressContains('To', 'jane_doe@example.com');
* ```
*/
public function assertEmailAddressContains(string $headerName, string $expectedValue, ?Email $email = null): void
public function assertEmailAddressContains(string $headerName, string $expectedValue, ?Message $email = null): void
{
$email = $this->verifyEmailObject($email, __FUNCTION__);
$this->assertThat($email, new MimeConstraint\EmailAddressContains($headerName, $expectedValue));
Expand Down Expand Up @@ -53,7 +55,7 @@ public function assertEmailAttachmentCount(int $count, ?Email $email = null): vo
* $I->assertEmailHasHeader('Bcc');
* ```
*/
public function assertEmailHasHeader(string $headerName, ?Email $email = null): void
public function assertEmailHasHeader(string $headerName, ?Message $email = null): void
{
$email = $this->verifyEmailObject($email, __FUNCTION__);
$this->assertThat($email, new MimeConstraint\EmailHasHeader($headerName));
Expand All @@ -69,7 +71,7 @@ public function assertEmailHasHeader(string $headerName, ?Email $email = null):
* $I->assertEmailHeaderNotSame('To', 'john_doe@gmail.com');
* ```
*/
public function assertEmailHeaderNotSame(string $headerName, string $expectedValue, ?Email $email = null): void
public function assertEmailHeaderNotSame(string $headerName, string $expectedValue, ?Message $email = null): void
{
$email = $this->verifyEmailObject($email, __FUNCTION__);
$this->assertThat($email, new LogicalNot(new MimeConstraint\EmailHeaderSame($headerName, $expectedValue)));
Expand All @@ -85,7 +87,7 @@ public function assertEmailHeaderNotSame(string $headerName, string $expectedVal
* $I->assertEmailHeaderSame('To', 'jane_doe@gmail.com');
* ```
*/
public function assertEmailHeaderSame(string $headerName, string $expectedValue, ?Email $email = null): void
public function assertEmailHeaderSame(string $headerName, string $expectedValue, ?Message $email = null): void
Copy link
Copy Markdown
Author

@krrico krrico May 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether it's better to use RawMessage as type hint everywhere and let the Symfony Mime Constraints used here run into the RuntimeException when they don't support RawMessage or Message.

{
$email = $this->verifyEmailObject($email, __FUNCTION__);
$this->assertThat($email, new MimeConstraint\EmailHeaderSame($headerName, $expectedValue));
Expand Down Expand Up @@ -130,7 +132,7 @@ public function assertEmailHtmlBodyNotContains(string $text, ?Email $email = nul
* $I->assertEmailNotHasHeader('Bcc');
* ```
*/
public function assertEmailNotHasHeader(string $headerName, ?Email $email = null): void
public function assertEmailNotHasHeader(string $headerName, ?Message $email = null): void
{
$email = $this->verifyEmailObject($email, __FUNCTION__);
$this->assertThat($email, new LogicalNot(new MimeConstraint\EmailHasHeader($headerName)));
Expand Down Expand Up @@ -169,7 +171,7 @@ public function assertEmailTextBodyNotContains(string $text, ?Email $email = nul
/**
* Returns the last email sent if $email is null. If no email has been sent it fails.
*/
private function verifyEmailObject(?Email $email, string $function): Email
private function verifyEmailObject(?RawMessage $email, string $function): RawMessage
{
$email = $email ?: $this->grabLastSentEmail();
$errorMsgTemplate = "There is no email to verify. An Email object was not specified when invoking '%s' and the application has not sent one.";
Expand Down