Skip to content

Commit 9e05380

Browse files
Merge pull request #50 from stackkit/feature/new-mailable
Add support for new mailables
2 parents 433e747 + 4e8ec2b commit 9e05380

File tree

3 files changed

+75
-16
lines changed

3 files changed

+75
-16
lines changed

composer.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,23 @@
3737
"scripts": {
3838
"l10": [
3939
"composer require laravel/framework:10.* orchestra/testbench:8.* --no-interaction --no-update",
40-
"composer update --prefer-stable --prefer-dist --no-interaction --no-suggest"
40+
"composer update --prefer-stable --prefer-dist --no-interaction"
4141
],
4242
"l9": [
4343
"composer require laravel/framework:9.* orchestra/testbench:7.* --no-interaction --no-update",
44-
"composer update --prefer-stable --prefer-dist --no-interaction --no-suggest"
44+
"composer update --prefer-stable --prefer-dist --no-interaction"
4545
],
4646
"l8": [
4747
"composer require laravel/framework:8.* orchestra/testbench:6.* --no-interaction --no-update",
48-
"composer update --prefer-stable --prefer-dist --no-interaction --no-suggest"
48+
"composer update --prefer-stable --prefer-dist --no-interaction"
4949
],
5050
"l7": [
5151
"composer require laravel/framework:8.* orchestra/testbench:6.* --no-interaction --no-update",
52-
"composer update --prefer-stable --prefer-dist --no-interaction --no-suggest"
52+
"composer update --prefer-stable --prefer-dist --no-interaction"
5353
],
5454
"l6": [
5555
"composer require laravel/framework:8.* orchestra/testbench:6.* --no-interaction --no-update",
56-
"composer update --prefer-stable --prefer-dist --no-interaction --no-suggest"
56+
"composer update --prefer-stable --prefer-dist --no-interaction"
5757
]
5858
}
5959
}

src/MailableReader.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Exception;
88
use Illuminate\Container\Container;
9+
use ReflectionObject;
910

1011
class MailableReader
1112
{
@@ -16,7 +17,14 @@ class MailableReader
1617
*/
1718
public function read(EmailComposer $composer): void
1819
{
19-
Container::getInstance()->call([$composer->getData('mailable'), 'build']);
20+
if (method_exists($composer->getData('mailable'), 'prepareMailableForDelivery')) {
21+
$reflected = (new ReflectionObject($composer->getData('mailable')));
22+
$method = $reflected->getMethod('prepareMailableForDelivery');
23+
$method->setAccessible(true);
24+
$method->invoke($composer->getData('mailable'));
25+
} else {
26+
Container::getInstance()->call([$composer->getData('mailable'), 'build']);
27+
}
2028

2129
$this->readRecipient($composer);
2230

tests/MailableReaderTest.php

+61-10
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,34 @@
33
namespace Tests;
44

55
use Illuminate\Mail\Mailable;
6+
use Illuminate\Mail\Mailables\Address;
7+
use Illuminate\Mail\Mailables\Attachment;
8+
use Illuminate\Mail\Mailables\Content;
9+
use Illuminate\Mail\Mailables\Envelope;
610
use Stackkit\LaravelDatabaseEmails\Email;
711

812
class MailableReaderTest extends TestCase
913
{
14+
private function mailable(): Mailable
15+
{
16+
if (version_compare(app()->version(), '10.0.0', '>=')) {
17+
return new Laravel10TestMailable();
18+
}
19+
20+
return new TestMailable();
21+
}
22+
1023
/** @test */
1124
public function it_extracts_the_recipient()
1225
{
1326
$composer = Email::compose()
14-
->mailable(new TestMailable());
27+
->mailable($this->mailable());
1528

1629
$this->assertEquals(['[email protected]'], $composer->getData('recipient'));
1730

1831
$composer = Email::compose()
1932
->mailable(
20-
(new TestMailable())->to(['[email protected]'])
33+
$this->mailable()->to(['[email protected]'])
2134
);
2235

2336
$this->assertCount(2, $composer->getData('recipient'));
@@ -28,39 +41,39 @@ public function it_extracts_the_recipient()
2841
/** @test */
2942
public function it_extracts_cc_addresses()
3043
{
31-
$composer = Email::compose()->mailable(new TestMailable());
44+
$composer = Email::compose()->mailable($this->mailable());
3245

3346
$this->assertEquals(['[email protected]', '[email protected]'], $composer->getData('cc'));
3447
}
3548

3649
/** @test */
3750
public function it_extracts_bcc_addresses()
3851
{
39-
$composer = Email::compose()->mailable(new TestMailable());
52+
$composer = Email::compose()->mailable($this->mailable());
4053

4154
$this->assertEquals(['[email protected]', '[email protected]'], $composer->getData('bcc'));
4255
}
4356

4457
/** @test */
4558
public function it_extracts_the_subject()
4659
{
47-
$composer = Email::compose()->mailable(new TestMailable());
60+
$composer = Email::compose()->mailable($this->mailable());
4861

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

5265
/** @test */
5366
public function it_extracts_the_body()
5467
{
55-
$composer = Email::compose()->mailable(new TestMailable());
68+
$composer = Email::compose()->mailable($this->mailable());
5669

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

6073
/** @test */
6174
public function it_extracts_attachments()
6275
{
63-
$email = Email::compose()->mailable(new TestMailable())->send();
76+
$email = Email::compose()->mailable($this->mailable())->send();
6477

6578
$attachments = $email->getAttachments();
6679

@@ -78,7 +91,7 @@ public function it_extracts_attachments()
7891
public function it_extracts_the_from_address_and_or_name()
7992
{
8093
$email = Email::compose()->mailable(
81-
(new TestMailable())
94+
($this->mailable())
8295
->from('[email protected]', 'Marick')
8396
)->send();
8497

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

89102
$email = Email::compose()->mailable(
90-
(new TestMailable())
103+
($this->mailable())
91104
92105
)->send();
93106

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

98111
$email = Email::compose()->mailable(
99-
(new TestMailable())
112+
($this->mailable())
100113
->from(null, 'Marick')
101114
)->send();
102115

@@ -132,3 +145,41 @@ public function build()
132145
->view('tests::dummy', ['name' => 'John Doe']);
133146
}
134147
}
148+
149+
class Laravel10TestMailable extends Mailable
150+
{
151+
public function content(): Content
152+
{
153+
$content = new Content(
154+
'tests::dummy'
155+
);
156+
157+
$content->with('name', 'John Doe');
158+
159+
return $content;
160+
}
161+
162+
public function envelope(): Envelope
163+
{
164+
return new Envelope(
165+
null,
166+
[
167+
new Address('[email protected]', 'John Doe')
168+
],
169+
170+
171+
[],
172+
'Your order has shipped!'
173+
);
174+
}
175+
176+
public function attachments(): array
177+
{
178+
return [
179+
Attachment::fromPath(__DIR__ . '/files/pdf-sample.pdf')->withMime('application/pdf'),
180+
Attachment::fromData(function () {
181+
return '<p>Thanks for your oder</p>';
182+
}, 'order.html')
183+
];
184+
}
185+
}

0 commit comments

Comments
 (0)