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

Support alternative mail syntax #23

Merged
merged 4 commits into from
Oct 25, 2022
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
8 changes: 8 additions & 0 deletions src/MailableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class MailableResolver

private ?string $content = null;

private bool $hasBuild = false;

public function __construct(public string|Closure|Mailable $mailable)
{
}
Expand Down Expand Up @@ -92,6 +94,12 @@ public function content(): string

private function build(Mailable $instance): Mailable
{
if ($this->hasBuild) {
return $instance;
}

$this->hasBuild = true;

$locale = MailbookFacade::getLocale();

if ($locale) {
Expand Down
22 changes: 22 additions & 0 deletions tests/AlternativeMailTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Xammie\Mailbook\Facades\Mailbook;
use Xammie\Mailbook\Tests\Mails\AlternativeMail;
use Xammie\Mailbook\Tests\Mails\ClassicMail;

it('can get mail subject and content', function ($mailable) {
$item = Mailbook::add($mailable);

expect($item->subject())->toBe('Invoice Paid')
->and($item->content())->toBe('Test mail content')
->and($item->from())->toBe(['Example <hello@example.com>'])
->and($item->replyTo())->toBe([])
->and($item->to())->toBe([])
->and($item->cc())->toBe(['Example Name <foo@example.com>'])
->and($item->bcc())->toBe([]);
})
->with([
new ClassicMail(),
new AlternativeMail(),
])
->skip(! class_exists('Illuminate\Mail\Mailables\Envelope'), 'Alternative mailables are not available in this version of Laravel');
28 changes: 28 additions & 0 deletions tests/Mails/AlternativeMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Xammie\Mailbook\Tests\Mails;

use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;

final class AlternativeMail extends Mailable
{
public function envelope(): Envelope
{
return new Envelope(
cc: [new Address('foo@example.com', 'Example Name')],
subject: 'Invoice Paid',
);
}

public function content(): Content
{
return new Content(
htmlString: 'Test mail content',
);
}
}
18 changes: 18 additions & 0 deletions tests/Mails/ClassicMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Xammie\Mailbook\Tests\Mails;

use Illuminate\Mail\Mailable;

final class ClassicMail extends Mailable
{
public function build(): self
{
return $this
->subject('Invoice Paid')
->cc('foo@example.com', 'Example Name')
->html('Test mail content');
}
}