Skip to content

Commit

Permalink
feat(behat): add new assertions if has attachment with name is same o…
Browse files Browse the repository at this point in the history
…r matches, close #12 (#14)
  • Loading branch information
Kocal authored Jun 4, 2020
1 parent 0fd01f5 commit 5456910
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ With additional assertions:
- `assertEmailTextBodyNotMatches`
- `assertEmailHtmlBodyMatches`
- `assertEmailHtmlBodyNotMatches`
- `assertEmailAttachmentNameSame`
- `assertEmailAttachmentNameMatches`

## Installation

Expand Down
16 changes: 16 additions & 0 deletions src/Bridge/Behat/MailerContextTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
51 changes: 47 additions & 4 deletions src/Test/MailerAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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,
));
}
}
2 changes: 2 additions & 0 deletions tests/Bridge/Behat/email.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
36 changes: 36 additions & 0 deletions tests/MailerAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5456910

Please sign in to comment.