Skip to content

Commit

Permalink
Merge pull request #50 from stackkit/feature/new-mailable
Browse files Browse the repository at this point in the history
Add support for new mailables
  • Loading branch information
marickvantuil authored Apr 9, 2023
2 parents 433e747 + 4e8ec2b commit 9e05380
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 16 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@
"scripts": {
"l10": [
"composer require laravel/framework:10.* orchestra/testbench:8.* --no-interaction --no-update",
"composer update --prefer-stable --prefer-dist --no-interaction --no-suggest"
"composer update --prefer-stable --prefer-dist --no-interaction"
],
"l9": [
"composer require laravel/framework:9.* orchestra/testbench:7.* --no-interaction --no-update",
"composer update --prefer-stable --prefer-dist --no-interaction --no-suggest"
"composer update --prefer-stable --prefer-dist --no-interaction"
],
"l8": [
"composer require laravel/framework:8.* orchestra/testbench:6.* --no-interaction --no-update",
"composer update --prefer-stable --prefer-dist --no-interaction --no-suggest"
"composer update --prefer-stable --prefer-dist --no-interaction"
],
"l7": [
"composer require laravel/framework:8.* orchestra/testbench:6.* --no-interaction --no-update",
"composer update --prefer-stable --prefer-dist --no-interaction --no-suggest"
"composer update --prefer-stable --prefer-dist --no-interaction"
],
"l6": [
"composer require laravel/framework:8.* orchestra/testbench:6.* --no-interaction --no-update",
"composer update --prefer-stable --prefer-dist --no-interaction --no-suggest"
"composer update --prefer-stable --prefer-dist --no-interaction"
]
}
}
10 changes: 9 additions & 1 deletion src/MailableReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Exception;
use Illuminate\Container\Container;
use ReflectionObject;

class MailableReader
{
Expand All @@ -16,7 +17,14 @@ class MailableReader
*/
public function read(EmailComposer $composer): void
{
Container::getInstance()->call([$composer->getData('mailable'), 'build']);
if (method_exists($composer->getData('mailable'), 'prepareMailableForDelivery')) {
$reflected = (new ReflectionObject($composer->getData('mailable')));
$method = $reflected->getMethod('prepareMailableForDelivery');
$method->setAccessible(true);
$method->invoke($composer->getData('mailable'));
} else {
Container::getInstance()->call([$composer->getData('mailable'), 'build']);
}

$this->readRecipient($composer);

Expand Down
71 changes: 61 additions & 10 deletions tests/MailableReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,34 @@
namespace Tests;

use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Stackkit\LaravelDatabaseEmails\Email;

class MailableReaderTest extends TestCase
{
private function mailable(): Mailable
{
if (version_compare(app()->version(), '10.0.0', '>=')) {
return new Laravel10TestMailable();
}

return new TestMailable();
}

/** @test */
public function it_extracts_the_recipient()
{
$composer = Email::compose()
->mailable(new TestMailable());
->mailable($this->mailable());

$this->assertEquals(['john@doe.com'], $composer->getData('recipient'));

$composer = Email::compose()
->mailable(
(new TestMailable())->to(['jane@doe.com'])
$this->mailable()->to(['jane@doe.com'])
);

$this->assertCount(2, $composer->getData('recipient'));
Expand All @@ -28,39 +41,39 @@ public function it_extracts_the_recipient()
/** @test */
public function it_extracts_cc_addresses()
{
$composer = Email::compose()->mailable(new TestMailable());
$composer = Email::compose()->mailable($this->mailable());

$this->assertEquals(['john+cc@doe.com', 'john+cc2@doe.com'], $composer->getData('cc'));
}

/** @test */
public function it_extracts_bcc_addresses()
{
$composer = Email::compose()->mailable(new TestMailable());
$composer = Email::compose()->mailable($this->mailable());

$this->assertEquals(['john+bcc@doe.com', 'john+bcc2@doe.com'], $composer->getData('bcc'));
}

/** @test */
public function it_extracts_the_subject()
{
$composer = Email::compose()->mailable(new TestMailable());
$composer = Email::compose()->mailable($this->mailable());

$this->assertEquals('Your order has shipped!', $composer->getData('subject'));
}

/** @test */
public function it_extracts_the_body()
{
$composer = Email::compose()->mailable(new TestMailable());
$composer = Email::compose()->mailable($this->mailable());

$this->assertEquals("Name: John Doe\n", $composer->getData('body'));
}

/** @test */
public function it_extracts_attachments()
{
$email = Email::compose()->mailable(new TestMailable())->send();
$email = Email::compose()->mailable($this->mailable())->send();

$attachments = $email->getAttachments();

Expand All @@ -78,7 +91,7 @@ public function it_extracts_attachments()
public function it_extracts_the_from_address_and_or_name()
{
$email = Email::compose()->mailable(
(new TestMailable())
($this->mailable())
->from('marick@dolphiq.nl', 'Marick')
)->send();

Expand All @@ -87,7 +100,7 @@ public function it_extracts_the_from_address_and_or_name()
$this->assertEquals('Marick', $email->getFromName());

$email = Email::compose()->mailable(
(new TestMailable())
($this->mailable())
->from('marick@dolphiq.nl')
)->send();

Expand All @@ -96,7 +109,7 @@ public function it_extracts_the_from_address_and_or_name()
$this->assertEquals(config('mail.from.name'), $email->getFromName());

$email = Email::compose()->mailable(
(new TestMailable())
($this->mailable())
->from(null, 'Marick')
)->send();

Expand Down Expand Up @@ -132,3 +145,41 @@ public function build()
->view('tests::dummy', ['name' => 'John Doe']);
}
}

class Laravel10TestMailable extends Mailable
{
public function content(): Content
{
$content = new Content(
'tests::dummy'
);

$content->with('name', 'John Doe');

return $content;
}

public function envelope(): Envelope
{
return new Envelope(
null,
[
new Address('john@doe.com', 'John Doe')
],
['john+cc@doe.com', 'john+cc2@doe.com'],
['john+bcc@doe.com', 'john+bcc2@doe.com'],
[],
'Your order has shipped!'
);
}

public function attachments(): array
{
return [
Attachment::fromPath(__DIR__ . '/files/pdf-sample.pdf')->withMime('application/pdf'),
Attachment::fromData(function () {
return '<p>Thanks for your oder</p>';
}, 'order.html')
];
}
}

0 comments on commit 9e05380

Please sign in to comment.