Skip to content

Commit 36b09b7

Browse files
authored
make the Mailable class tappable. (#53788)
1 parent a62a062 commit 36b09b7

File tree

2 files changed

+40
-85
lines changed

2 files changed

+40
-85
lines changed

src/Illuminate/Mail/Mailable.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Illuminate\Support\Traits\ForwardsCalls;
2020
use Illuminate\Support\Traits\Localizable;
2121
use Illuminate\Support\Traits\Macroable;
22+
use Illuminate\Support\Traits\Tappable;
2223
use Illuminate\Testing\Constraints\SeeInOrder;
2324
use PHPUnit\Framework\Assert as PHPUnit;
2425
use ReflectionClass;
@@ -29,7 +30,7 @@
2930

3031
class Mailable implements MailableContract, Renderable
3132
{
32-
use Conditionable, ForwardsCalls, Localizable, Macroable {
33+
use Conditionable, ForwardsCalls, Localizable, Tappable, Macroable {
3334
__call as macroCall;
3435
}
3536

tests/Mail/MailMailableTest.php

+38-84
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,7 @@ protected function tearDown(): void
2424

2525
public function testMailableSetsRecipientsCorrectly()
2626
{
27-
Container::getInstance()->instance('mailer', new class
28-
{
29-
public function render()
30-
{
31-
//
32-
}
33-
});
27+
$this->stubMailer();
3428

3529
$mailable = new WelcomeMailableStub;
3630
$mailable->to('[email protected]');
@@ -114,13 +108,7 @@ public function render()
114108

115109
public function testMailableSetsCcRecipientsCorrectly()
116110
{
117-
Container::getInstance()->instance('mailer', new class
118-
{
119-
public function render()
120-
{
121-
//
122-
}
123-
});
111+
$this->stubMailer();
124112

125113
$mailable = new WelcomeMailableStub;
126114
$mailable->cc('[email protected]');
@@ -211,13 +199,7 @@ public function render()
211199

212200
public function testMailableSetsBccRecipientsCorrectly()
213201
{
214-
Container::getInstance()->instance('mailer', new class
215-
{
216-
public function render()
217-
{
218-
//
219-
}
220-
});
202+
$this->stubMailer();
221203

222204
$mailable = new WelcomeMailableStub;
223205
$mailable->bcc('[email protected]');
@@ -308,13 +290,7 @@ public function render()
308290

309291
public function testMailableSetsReplyToCorrectly()
310292
{
311-
Container::getInstance()->instance('mailer', new class
312-
{
313-
public function render()
314-
{
315-
//
316-
}
317-
});
293+
$this->stubMailer();
318294

319295
$mailable = new WelcomeMailableStub;
320296
$mailable->replyTo('[email protected]');
@@ -394,13 +370,7 @@ public function render()
394370

395371
public function testMailableSetsFromCorrectly()
396372
{
397-
Container::getInstance()->instance('mailer', new class
398-
{
399-
public function render()
400-
{
401-
//
402-
}
403-
});
373+
$this->stubMailer();
404374

405375
$mailable = new WelcomeMailableStub;
406376
$mailable->from('[email protected]');
@@ -630,13 +600,7 @@ public function testMailablePriorityGetsSent()
630600

631601
public function testMailableMetadataGetsSent()
632602
{
633-
Container::getInstance()->instance('mailer', new class
634-
{
635-
public function render()
636-
{
637-
//
638-
}
639-
});
603+
$this->stubMailer();
640604

641605
$view = m::mock(Factory::class);
642606

@@ -671,13 +635,7 @@ public function render()
671635

672636
public function testMailableTagGetsSent()
673637
{
674-
Container::getInstance()->instance('mailer', new class
675-
{
676-
public function render()
677-
{
678-
//
679-
}
680-
});
638+
$this->stubMailer();
681639

682640
$view = m::mock(Factory::class);
683641

@@ -829,13 +787,7 @@ public function toMailAttachment()
829787

830788
public function testHasAttachmentWithEnvelopeAttachments()
831789
{
832-
Container::getInstance()->instance('mailer', new class
833-
{
834-
public function render()
835-
{
836-
//
837-
}
838-
});
790+
$this->stubMailer();
839791
$mailable = new class extends Mailable
840792
{
841793
public function envelope()
@@ -1034,13 +986,7 @@ public function testItCanCheckForStorageBasedAttachments()
1034986

1035987
public function testAssertHasAttachment()
1036988
{
1037-
Container::getInstance()->instance('mailer', new class
1038-
{
1039-
public function render()
1040-
{
1041-
//
1042-
}
1043-
});
989+
$this->stubMailer();
1044990

1045991
$mailable = new class() extends Mailable
1046992
{
@@ -1070,13 +1016,7 @@ public function build()
10701016

10711017
public function testAssertHasAttachedData()
10721018
{
1073-
Container::getInstance()->instance('mailer', new class
1074-
{
1075-
public function render()
1076-
{
1077-
//
1078-
}
1079-
});
1019+
$this->stubMailer();
10801020

10811021
$mailable = new class() extends Mailable
10821022
{
@@ -1134,13 +1074,7 @@ public function build()
11341074

11351075
public function testAssertHasSubject()
11361076
{
1137-
Container::getInstance()->instance('mailer', new class
1138-
{
1139-
public function render()
1140-
{
1141-
//
1142-
}
1143-
});
1077+
$this->stubMailer();
11441078

11451079
$mailable = new class() extends Mailable
11461080
{
@@ -1194,13 +1128,7 @@ public function testMailableHeadersGetSent()
11941128

11951129
public function testMailableAttributesInBuild()
11961130
{
1197-
Container::getInstance()->instance('mailer', new class
1198-
{
1199-
public function render()
1200-
{
1201-
//
1202-
}
1203-
});
1131+
$this->stubMailer();
12041132

12051133
$mailable = new class() extends Mailable
12061134
{
@@ -1227,6 +1155,32 @@ public function build()
12271155
$mailable->assertHasMetadata('user_id', 1);
12281156
$mailable->assertHasSubject('test subject');
12291157
}
1158+
1159+
public function testMailablesCanBeTapped()
1160+
{
1161+
$this->stubMailer();
1162+
1163+
$mail = new WelcomeMailableStub;
1164+
1165+
$mail->tap(fn ($mailable) => $mailable->to('[email protected]', 'Taylor Otwell'));
1166+
$mail->tap(fn ($mailable) => $mailable->subject('Test Subject!'));
1167+
1168+
$mail->tap(function ($mailable) {
1169+
$mailable->assertTo('[email protected]')
1170+
->assertHasSubject('Test Subject!');
1171+
});
1172+
}
1173+
1174+
protected function stubMailer()
1175+
{
1176+
Container::getInstance()->instance('mailer', new class
1177+
{
1178+
public function render()
1179+
{
1180+
//
1181+
}
1182+
});
1183+
}
12301184
}
12311185

12321186
class MailableHeadersStub extends Mailable

0 commit comments

Comments
 (0)