From 54569105e3754cf84be92ba27a9cd3777d92865a Mon Sep 17 00:00:00 2001 From: Hugo Alliaume Date: Thu, 4 Jun 2020 13:17:32 +0200 Subject: [PATCH] feat(behat): add new assertions if has attachment with name is same or matches, close #12 (#14) --- README.md | 2 + src/Bridge/Behat/MailerContextTrait.php | 16 ++++++++ src/Test/MailerAssertions.php | 51 +++++++++++++++++++++++-- tests/Bridge/Behat/email.feature | 2 + tests/MailerAssertionsTest.php | 36 +++++++++++++++++ 5 files changed, 103 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d32695c..2ce7d92 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,8 @@ With additional assertions: - `assertEmailTextBodyNotMatches` - `assertEmailHtmlBodyMatches` - `assertEmailHtmlBodyNotMatches` +- `assertEmailAttachmentNameSame` +- `assertEmailAttachmentNameMatches` ## Installation diff --git a/src/Bridge/Behat/MailerContextTrait.php b/src/Bridge/Behat/MailerContextTrait.php index 1fa2003..32ff5c4 100644 --- a/src/Bridge/Behat/MailerContextTrait.php +++ b/src/Bridge/Behat/MailerContextTrait.php @@ -117,4 +117,20 @@ public function thisEmailHtmlBodyNotMatches(string $regex): void { $this->mailerAssertions->assertEmailHtmlBodyNotMatches($this->getSelectedMessageEvent()->getMessage(), $regex); } + + /** + * @Then this email has attachment named :name + */ + public function thisEmailHasAttachmentNamed(string $name): void + { + $this->mailerAssertions->assertEmailAttachmentNameSame($this->getSelectedMessageEvent()->getMessage(), $name); + } + + /** + * @Then this email has attachment name matching :regex + */ + public function thisEmailHasAttachmentNameMatching(string $regex): void + { + $this->mailerAssertions->assertEmailAttachmentNameMatches($this->getSelectedMessageEvent()->getMessage(), $regex); + } } diff --git a/src/Test/MailerAssertions.php b/src/Test/MailerAssertions.php index 3069740..54fb124 100644 --- a/src/Test/MailerAssertions.php +++ b/src/Test/MailerAssertions.php @@ -6,6 +6,7 @@ use Kocal\SymfonyMailerTesting\MailerLogger; use Symfony\Component\Mime\Email; +use Symfony\Component\Mime\Part\DataPart; use Symfony\Component\Mime\RawMessage; use Webmozart\Assert\Assert; @@ -42,7 +43,7 @@ public function assertEmailSubjectMatches(RawMessage $email, string $regex, ?str )); } - public function assertEmailTextBodyMatches(RawMessage $email, string $regex, ?string $message=null): void + public function assertEmailTextBodyMatches(RawMessage $email, string $regex, ?string $message = null): void { Assert::isInstanceOf($email, Email::class); Assert::nullOrString($email->getTextBody()); @@ -53,7 +54,7 @@ public function assertEmailTextBodyMatches(RawMessage $email, string $regex, ?st )); } - public function assertEmailTextBodyNotMatches(RawMessage $email, string $regex, ?string $message=null): void + public function assertEmailTextBodyNotMatches(RawMessage $email, string $regex, ?string $message = null): void { Assert::isInstanceOf($email, Email::class); Assert::nullOrString($email->getTextBody()); @@ -64,7 +65,7 @@ public function assertEmailTextBodyNotMatches(RawMessage $email, string $regex, )); } - public function assertEmailHtmlBodyMatches(RawMessage $email, string $regex, ?string $message=null): void + public function assertEmailHtmlBodyMatches(RawMessage $email, string $regex, ?string $message = null): void { Assert::isInstanceOf($email, Email::class); Assert::nullOrString($email->getHtmlBody()); @@ -75,7 +76,7 @@ public function assertEmailHtmlBodyMatches(RawMessage $email, string $regex, ?st )); } - public function assertEmailHtmlBodyNotMatches(RawMessage $email, string $regex, ?string $message=null): void + public function assertEmailHtmlBodyNotMatches(RawMessage $email, string $regex, ?string $message = null): void { Assert::isInstanceOf($email, Email::class); Assert::nullOrString($email->getHtmlBody()); @@ -85,4 +86,46 @@ public function assertEmailHtmlBodyNotMatches(RawMessage $email, string $regex, $email->getHtmlBody() )); } + + public function assertEmailAttachmentNameSame(RawMessage $email, string $attachmentName, ?string $message = null): void + { + Assert::isInstanceOf($email, Email::class); + + $matches = (function () use ($email, $attachmentName): bool { + /** @var DataPart $attachment */ + foreach ($email->getAttachments() as $attachment) { + if ($attachmentName === $attachment->getPreparedHeaders()->getHeaderParameter('Content-Disposition', 'filename')) { + return true; + } + } + + return false; + })(); + + Assert::true($matches, sprintf( + $message ?? 'Failed asserting that the Email has an attachment with name "%s".', + $attachmentName, + )); + } + + public function assertEmailAttachmentNameMatches(RawMessage $email, string $attachmentNamePattern, ?string $message = null): void + { + Assert::isInstanceOf($email, Email::class); + + $matches = (function () use ($email, $attachmentNamePattern): bool { + /** @var DataPart $attachment */ + foreach ($email->getAttachments() as $attachment) { + if (1 === preg_match($attachmentNamePattern, $attachment->getPreparedHeaders()->getHeaderParameter('Content-Disposition', 'filename') ?? '')) { + return true; + } + } + + return false; + })(); + + Assert::true($matches, sprintf( + $message ?? 'Failed asserting that the Email has an attachment with name matching pattern "%s".', + $attachmentNamePattern, + )); + } } diff --git a/tests/Bridge/Behat/email.feature b/tests/Bridge/Behat/email.feature index 5aaf9ea..db44016 100644 --- a/tests/Bridge/Behat/email.feature +++ b/tests/Bridge/Behat/email.feature @@ -32,6 +32,8 @@ Feature: Testing emails Then I select email #0 And this email has 1 attachment + And this email has attachment named "attachment.txt" + And this email has attachment name matching "#^attachment#" Scenario: I can test if emails text body contains When I send an email: diff --git a/tests/MailerAssertionsTest.php b/tests/MailerAssertionsTest.php index 40c9b74..09c8960 100644 --- a/tests/MailerAssertionsTest.php +++ b/tests/MailerAssertionsTest.php @@ -529,6 +529,42 @@ public function testAssertEmailSubjectMatchesFailing(): void $this->mailerAssertions->assertEmailSubjectMatches($email, '/^[A-Z]oodbye/'); } + public function testAssertEmailAttachmentNameSame(): void + { + $email = $this->createEmail()->attach('Hello world!', 'message.txt'); + + $this->mailerAssertions->assertEmailAttachmentNameSame($email, 'message.txt'); + + $this->addToAssertionCount(1); + } + + public function testAssertEmailAttachmentNameSameFailing(): void + { + $this->expectDeprecationMessage('Failed asserting that the Email has an attachment with name "not message.txt".'); + + $email = $this->createEmail()->attach('Hello world!', 'message.txt'); + + $this->mailerAssertions->assertEmailAttachmentNameSame($email, 'not message.txt'); + } + + public function testAssertEmailAttachmentNameMatches(): void + { + $email = $this->createEmail()->attach('Hello world!', 'message.txt'); + + $this->mailerAssertions->assertEmailAttachmentNameMatches($email, '/^message/'); + + $this->addToAssertionCount(1); + } + + public function testAssertEmailAttachmentNameMatchesFailing(): void + { + $this->expectDeprecationMessage('Failed asserting that the Email has an attachment with name matching pattern "/^not message/".'); + + $email = $this->createEmail()->attach('Hello world!', 'message.txt'); + + $this->mailerAssertions->assertEmailAttachmentNameMatches($email, '/^not message/'); + } + protected function createMessageEvent(RawMessage $message, string $transport = 'null://'): MessageEvent { return new MessageEvent($message, Envelope::create($message), $transport);