Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to test mail attachments #115

Merged
merged 5 commits into from
May 14, 2024
Merged
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
28 changes: 25 additions & 3 deletions src/Traits/MailsMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Mail\Mailable;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Mail;
use Symfony\Component\HttpFoundation\Response;

trait MailsMockTrait
{
Expand All @@ -23,11 +22,13 @@ trait MailsMockTrait
* 'emails' => string|array, email addresses to which the letter is expected to be sent on the step 1
* 'fixture' => 'expected_rendered_fixture.html', fixture name to which send email expected to be equal on the step 1
* 'subject' => string|null, expected email subject from the step 1
* 'from' => string|null, expected email sender address the step 1
* 'attachments' => array, expected attachments
* ]
*
* or be a function call:
*
* $this->mockedMail($emails, $fixture, $subject, $from),
* $this->mockedMail($emails, $fixture, $subject, $from, $attachments),
*
* or be an array, if sent more than 1 email:
*
Expand All @@ -36,12 +37,14 @@ trait MailsMockTrait
* 'emails' => string|array, email addresses to which the letter is expected to be sent on the step 1
* 'fixture' => 'expected_rendered_fixture.html', fixture name to which send email expected to be equal on the step 1
* 'subject' => string|null, expected email subject from the step 1
* 'from' => string|null, expected email sender address the step 1
* ],
* ...
* [
* 'emails' => string|array, email addresses to which the letter is expected to be sent on the step N
* 'fixture' => 'expected_rendered_fixture.html', fixture name to which send email expected to be equal on the step N
* 'subject' => string|null, expected email subject from the step N
* 'attachments' => array, expected attachments
* ]
* ]
*
Expand Down Expand Up @@ -75,6 +78,7 @@ protected function assertSentCallback(array $emailChain, int &$index, bool $expo
$this->assertEmailsList($expectedMailData, $mail, $index);
$this->assertFixture($expectedMailData, $mail, $exportMode);
$this->assertEmailFrom($expectedMailData, $mail);
$this->assertAttachments($expectedMailData, $mail, $index);

$index++;

Expand Down Expand Up @@ -200,13 +204,31 @@ protected function prepareEmailChain($emailChain): array
return (is_multidimensional($emailChain)) ? $emailChain : [$emailChain];
}

protected function mockedMail($emails, string $fixture, string $subject = '', $from = ''): array
protected function mockedMail($emails, string $fixture, string $subject = '', $from = '', $attachments = []): array
{
return [
'emails' => $emails,
'fixture' => $fixture,
'subject' => $subject,
'from' => $from,
'attachments' => $attachments,
];
}

protected function assertAttachments(array $currentMail, Mailable $mail, int $index): void
{
$attachments = Arr::get($currentMail, 'attachments', []);
$className = get_class($mail);

if (count($attachments)) {
$this->assertTrue(
method_exists($mail, 'assertHasAttachment'),
"Class {$className} doesn't have method `assertHasAttachment` to check an attachment.",
);

foreach ($attachments as $attachment) {
$mail->assertHasAttachment($attachment);
}
}
}
}
39 changes: 39 additions & 0 deletions tests/MailsMockTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use PHPUnit\Framework\ExpectationFailedException;
use RonasIT\Support\Tests\Support\Mock\TestMail;
use RonasIT\Support\Tests\Support\Mock\TestMailHasSubject;
use RonasIT\Support\Tests\Support\Mock\TestMailLegacy;
use RonasIT\Support\Tests\Support\Mock\TestMailWithAttachments;

class MailsMockTraitTest extends HelpersTestCase
{
Expand Down Expand Up @@ -129,4 +131,41 @@ public function testMailWithoutRequiredParameters()
],
]);
}

public function testMailWithAttachmentForLegacyVersion()
{
$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessage(
"Class RonasIT\Support\Tests\Support\Mock\TestMailLegacy doesn't have method "
. "`assertHasAttachment` to check an attachment."
);

Mail::to('test@mail.com')->queue(new TestMailLegacy(
['name' => 'John Smith'],
'subject',
'emails.test'
));

$this->assertMailEquals(TestMailLegacy::class, [
$this->mockedMail('test@mail.com', 'test_mail.html', 'subject', '', [
'attachment1',
]),
]);
}

public function testMailWithAttachment()
{
Mail::to('test@mail.com')->queue(new TestMailWithAttachments(
['name' => 'John Smith'],
'subject',
'emails.test'
));

$this->assertMailEquals(TestMailWithAttachments::class, [
$this->mockedMail('test@mail.com', 'test_mail.html', 'subject', '', [
'attachment1',
['file' => new \stdClass(), 'options' => ['some_options']]
]),
]);
}
}
31 changes: 31 additions & 0 deletions tests/support/Mock/TestMailLegacy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace RonasIT\Support\Tests\Support\Mock;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;

class TestMailLegacy extends Mailable implements ShouldQueue
{
public function __construct(array $viewData, $subject, $view)
{
$this->viewData = $viewData;
$this->subject = $subject;
$this->view = $view;
$this->queue = 'mails';

$this->setAddress('noreply@mail.net', null, 'from');
}

public function build()
{
return $this
->view($this->view)
->subject($this->subject)
->with($this->viewData)
->attach('/path/to/file', [
'as' => 'name.pdf',
'mime' => 'application/pdf',
]);
}
}
28 changes: 28 additions & 0 deletions tests/support/Mock/TestMailWithAttachments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace RonasIT\Support\Tests\Support\Mock;

use RonasIT\Support\Mail\BaseMail;

class TestMailWithAttachments extends BaseMail
{
public function __construct(array $viewData, $subject, $view)
{
parent::__construct($viewData, $subject, $view);

$this->setAddress('noreply@mail.net', null, 'from');
}

public function attachments(): array
{
return [
['file' => 'attachment1', 'options' => []],
['file' => new \stdClass(), 'options' => ['some_options']],
];
}

public function assertHasAttachment($file, array $options = []): bool
{
return true;
}
}
Loading