Skip to content

Commit

Permalink
Attachment assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonferens committed Oct 18, 2024
1 parent 425a181 commit 00fa385
Show file tree
Hide file tree
Showing 7 changed files with 642 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ This should be called after `Mail` has been sent, but before your assertions, ot
| `$intercepted->assertPriorityIsLowest();` | |
| `$intercepted->assertPriorityIsLowest();` | |

| Attachment Assertions | Parameters |
|:-------------------------------------------------------|:-------------------|
| `$intercepted->assertHasAttachment($filename);` | `$filename` string |
| `$intercepted->assertHasAttachments();` | |
| `$intercepted->assertMissingAttachment($filename);` | `$filename` string |
| `$intercepted->assertMissingAttachments();` | |
| `$intercepted->assertHasEmbeddedImage($filename);` | `$filename` string |
| `$intercepted->assertHasEmbeddedImages();` | |
| `$intercepted->assertMissingEmbeddedImage($filename);` | `$filename` string |
| `$intercepted->assertMissingEmbeddedImages();` | |

#### Assertion Methods

| Assertions | Parameters |
Expand Down Expand Up @@ -211,6 +222,17 @@ This should be called after `Mail` has been sent, but before your assertions, ot
| `$this->assertMailPriorityIsLowest($mail);` | `$mail` AssertableMessage, Email |
| `$this->assertMailPriorityIsLowest($mail);` | `$mail` AssertableMessage, Email |

| Attachment Assertions | Parameters |
|:---------------------------------------------------|:--------------------------------------------------------|
| `this->assertMailHasAttachment($filename);` | `$filename` string<br/>`$mail` AssertableMessage, Email |
| `this->assertMailHasAttachments();` | `$mail` AssertableMessage, Email |
| `this->assertMailMissingAttachment($filename);` | `$filename` string<br/>`$mail` AssertableMessage, Email |
| `this->assertMailMissingAttachments();` | `$mail` AssertableMessage, Email |
| `this->assertMailHasEmbeddedImage($filename);` | `$filename` string<br/>`$mail` AssertableMessage, Email |
| `this->assertMailHasEmbeddedImages();` | `$mail` AssertableMessage, Email |
| `this->assertMailMissingEmbeddedImage($filename);` | `$filename` string<br/>`$mail` AssertableMessage, Email |
| `this->assertMailMissingEmbeddedImages();` | `$mail` AssertableMessage, Email |

You should use each item of the `interceptedMail()` collection as the mail object for all assertions.

If you are injecting your own headers or need access to other headers in the email, use this assertion to verify they exist and are set properly. These assertions require the header name and the compiled email.
Expand Down
8 changes: 8 additions & 0 deletions src/AssertableMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@
* @method assertMissingHeader(string $expected)
* @method assertHeaderIs(string $expected, string $expectedValue)
* @method assertHeaderIsNot(string $expected, string $expectedValue)
* @method assertHasAttachment(string $filename)
* @method assertHasAttachments()
* @method assertMissingAttachment(string $filename)
* @method assertMissingAttachments()
* @method assertHasEmbeddedImage(string $filename)
* @method assertHasEmbeddedImages()
* @method assertMissingEmbeddedImage(string $filename)
* @method assertMissingEmbeddedImages()
*/
class AssertableMessage extends Assert
{
Expand Down
147 changes: 147 additions & 0 deletions src/Assertions/AttachmentAssertions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

namespace KirschbaumDevelopment\MailIntercept\Assertions;

use KirschbaumDevelopment\MailIntercept\AssertableMessage;
use PHPUnit\Framework\ExpectationFailedException;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\DataPart;

trait AttachmentAssertions
{
/*
|--------------------------------------------------------------------------
| File Attachments
|--------------------------------------------------------------------------
*/

/**
* Assert mail has attachments.
*/
public function assertMailHasAttachments(AssertableMessage|Email $mail): void
{
$this->assertNotEmpty(
$mail->getAttachments(),
'Mail missing expected attachments.'
);
}

/**
* Assert mail missing attachments.
*/
public function assertMailMissingAttachments(AssertableMessage|Email $mail): void
{
$this->assertEmpty(
$mail->getAttachments(),
'Mail has expected attachments.'
);
}

/**
* Assert mail has attachment.
*/
public function assertMailHasAttachment(string $expected, AssertableMessage|Email $mail): void
{
$hasAttachment = collect($mail->getAttachments())
->contains(fn (DataPart $attachment) => $expected === $attachment->getFilename());

$this->assertTrue(
$hasAttachment,
'Mail missing expected attachment.'
);
}

/**
* Assert mail missing attachment.
*/
public function assertMailMissingAttachment(string $expected, AssertableMessage|Email $mail): void
{
$missingAttachment = ! collect($mail->getAttachments())
->contains(fn (DataPart $attachment) => $expected === $attachment->getFilename());

$this->assertTrue(
$missingAttachment,
'Mail has expected attachment.'
);
}

/*
|--------------------------------------------------------------------------
| Embedded Images
|--------------------------------------------------------------------------
*/

/**
* Assert mail has attachments.
*/
public function assertMailHasEmbeddedImages(AssertableMessage|Email $mail): void
{
$hasEmbedded = collect($mail->getAttachments())
->contains(fn (DataPart $attachment) => $attachment->getDisposition() === 'inline');

$this->assertTrue(
$hasEmbedded,
'Mail missing embedded images.'
);
}

/**
* Assert mail missing attachments.
*/
public function assertMailMissingEmbeddedImages(AssertableMessage|Email $mail): void
{
$missingEmbedded = ! collect($mail->getAttachments())
->contains(fn (DataPart $attachment) => $attachment->getDisposition() === 'inline');

$this->assertTrue(
$missingEmbedded,
'Mail has embedded images.'
);
}

/**
* Assert mail has attachment.
*/
public function assertMailHasEmbeddedImage(string $expected, AssertableMessage|Email $mail): void
{
/**
* @var DataPart|null $embed
*/
$embed = collect($mail->getAttachments())
->firstWhere(fn (DataPart $attachment) => $expected === $attachment->getFilename());

if ($embed) {
$this->assertTrue(
$embed->getDisposition() === 'inline',
'Mail has expected attachment but is not embedded.'
);

return;
}

throw new ExpectationFailedException('Mail missing expected embedded image.');
}

/**
* Assert mail missing attachment.
*/
public function assertMailMissingEmbeddedImage(string $expected, AssertableMessage|Email $mail): void
{
/**
* @var DataPart|null $embed
*/
$embed = collect($mail->getAttachments())
->firstWhere(fn (DataPart $attachment) => $expected === $attachment->getFilename());

if (! $embed) {
$this->assertNull($embed);

return;
}

$this->assertTrue(
$embed->getDisposition() !== 'inline',
'Mail has expected embedded image.'
);
}
}
2 changes: 2 additions & 0 deletions src/WithMailInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;
use KirschbaumDevelopment\MailIntercept\Assertions\AttachmentAssertions;
use KirschbaumDevelopment\MailIntercept\Assertions\BccAssertions;
use KirschbaumDevelopment\MailIntercept\Assertions\CcAssertions;
use KirschbaumDevelopment\MailIntercept\Assertions\ContentAssertions;
Expand All @@ -21,6 +22,7 @@

trait WithMailInterceptor
{
use AttachmentAssertions;
use BccAssertions;
use CcAssertions;
use ContentAssertions;
Expand Down
Loading

0 comments on commit 00fa385

Please sign in to comment.