diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index c7305bb0..c5e3baeb 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -16,6 +16,7 @@ use Codeception\Module\Symfony\EventsAssertionsTrait; use Codeception\Module\Symfony\FormAssertionsTrait; use Codeception\Module\Symfony\MailerAssertionsTrait; +use Codeception\Module\Symfony\MimeAssertionsTrait; use Codeception\Module\Symfony\ParameterAssertionsTrait; use Codeception\Module\Symfony\RouterAssertionsTrait; use Codeception\Module\Symfony\SecurityAssertionsTrait; @@ -134,6 +135,7 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule use EventsAssertionsTrait; use FormAssertionsTrait; use MailerAssertionsTrait; + use MimeAssertionsTrait; use ParameterAssertionsTrait; use RouterAssertionsTrait; use SecurityAssertionsTrait; diff --git a/src/Codeception/Module/Symfony/MimeAssertionsTrait.php b/src/Codeception/Module/Symfony/MimeAssertionsTrait.php new file mode 100644 index 00000000..9228602a --- /dev/null +++ b/src/Codeception/Module/Symfony/MimeAssertionsTrait.php @@ -0,0 +1,177 @@ +assertEmailAddressContains('To', 'jane_doe@example.com'); + * ``` + */ + public function assertEmailAddressContains(string $headerName, string $expectedValue, Email $email = null): void + { + $email = $this->verifyEmailObject($email, __FUNCTION__); + $this->assertThat($email, new MimeConstraint\EmailAddressContains($headerName, $expectedValue)); + } + + /** + * Verify that an email has sent the specified number `$count` of attachments. + * If the Email object is not specified, the last email sent is used instead. + * + * ```php + * assertEmailAttachmentCount(1); + * ``` + */ + public function assertEmailAttachmentCount(int $count, Email $email = null): void + { + $email = $this->verifyEmailObject($email, __FUNCTION__); + $this->assertThat($email, new MimeConstraint\EmailAttachmentCount($count)); + } + + /** + * Verify that an email has a [header](https://datatracker.ietf.org/doc/html/rfc4021) `$headerName`. + * If the Email object is not specified, the last email sent is used instead. + * + * ```php + * assertEmailHasHeader('Bcc'); + * ``` + */ + public function assertEmailHasHeader(string $headerName, Email $email = null): void + { + $email = $this->verifyEmailObject($email, __FUNCTION__); + $this->assertThat($email, new MimeConstraint\EmailHasHeader($headerName)); + } + + /** + * Verify that the [header](https://datatracker.ietf.org/doc/html/rfc4021) + * `$headerName` of an email is not the expected one `$expectedValue`. + * If the Email object is not specified, the last email sent is used instead. + * + * ```php + * assertEmailHeaderNotSame('To', 'john_doe@gmail.com'); + * ``` + */ + public function assertEmailHeaderNotSame(string $headerName, string $expectedValue, Email $email = null): void + { + $email = $this->verifyEmailObject($email, __FUNCTION__); + $this->assertThat($email, new LogicalNot(new MimeConstraint\EmailHeaderSame($headerName, $expectedValue))); + } + + /** + * Verify that the [header](https://datatracker.ietf.org/doc/html/rfc4021) + * `$headerName` of an email is the same as expected `$expectedValue`. + * If the Email object is not specified, the last email sent is used instead. + * + * ```php + * assertEmailHeaderSame('To', 'jane_doe@gmail.com'); + * ``` + */ + public function assertEmailHeaderSame(string $headerName, string $expectedValue, Email $email = null): void + { + $email = $this->verifyEmailObject($email, __FUNCTION__); + $this->assertThat($email, new MimeConstraint\EmailHeaderSame($headerName, $expectedValue)); + } + + /** + * Verify that the HTML body of an email contains `$text`. + * If the Email object is not specified, the last email sent is used instead. + * + * ```php + * assertEmailHtmlBodyContains('Successful registration'); + * ``` + */ + public function assertEmailHtmlBodyContains(string $text, Email $email = null): void + { + $email = $this->verifyEmailObject($email, __FUNCTION__); + $this->assertThat($email, new MimeConstraint\EmailHtmlBodyContains($text)); + } + + /** + * Verify that the HTML body of an email does not contain a text `$text`. + * If the Email object is not specified, the last email sent is used instead. + * + * ```php + * assertEmailHtmlBodyNotContains('userpassword'); + * ``` + */ + public function assertEmailHtmlBodyNotContains(string $text, Email $email = null): void + { + $email = $this->verifyEmailObject($email, __FUNCTION__); + $this->assertThat($email, new LogicalNot(new MimeConstraint\EmailHtmlBodyContains($text))); + } + + /** + * Verify that an email does not have a [header](https://datatracker.ietf.org/doc/html/rfc4021) `$headerName`. + * If the Email object is not specified, the last email sent is used instead. + * + * ```php + * assertEmailNotHasHeader('Bcc'); + * ``` + */ + public function assertEmailNotHasHeader(string $headerName, Email $email = null): void + { + $email = $this->verifyEmailObject($email, __FUNCTION__); + $this->assertThat($email, new LogicalNot(new MimeConstraint\EmailHasHeader($headerName))); + } + + /** + * Verify the text body of an email contains a `$text`. + * If the Email object is not specified, the last email sent is used instead. + * + * ```php + * assertEmailTextBodyContains('Example text body'); + * ``` + */ + public function assertEmailTextBodyContains(string $text, Email $email = null): void + { + $email = $this->verifyEmailObject($email, __FUNCTION__); + $this->assertThat($email, new MimeConstraint\EmailTextBodyContains($text)); + } + + /** + * Verify that the text body of an email does not contain a `$text`. + * If the Email object is not specified, the last email sent is used instead. + * + * ```php + * assertEmailTextBodyNotContains('My secret text body'); + * ``` + */ + public function assertEmailTextBodyNotContains(string $text, Email $email = null): void + { + $email = $this->verifyEmailObject($email, __FUNCTION__); + $this->assertThat($email, new LogicalNot(new MimeConstraint\EmailTextBodyContains($text))); + } + + /** + * 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 + { + $email = $email ?: $this->grabLastSentEmail(); + $errorMsgFormat = "There is no email to verify. An Email object was not specified when invoking '%s' and the application has not sent one."; + return $email ?: $this->fail( + sprintf($errorMsgFormat, $function) + ); + } +} \ No newline at end of file