Skip to content

Commit

Permalink
make the Mailable class tappable. (#53788)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinb1989 authored Dec 8, 2024
1 parent a62a062 commit 36b09b7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 85 deletions.
3 changes: 2 additions & 1 deletion src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Illuminate\Support\Traits\ForwardsCalls;
use Illuminate\Support\Traits\Localizable;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\Traits\Tappable;
use Illuminate\Testing\Constraints\SeeInOrder;
use PHPUnit\Framework\Assert as PHPUnit;
use ReflectionClass;
Expand All @@ -29,7 +30,7 @@

class Mailable implements MailableContract, Renderable
{
use Conditionable, ForwardsCalls, Localizable, Macroable {
use Conditionable, ForwardsCalls, Localizable, Tappable, Macroable {
__call as macroCall;
}

Expand Down
122 changes: 38 additions & 84 deletions tests/Mail/MailMailableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ protected function tearDown(): void

public function testMailableSetsRecipientsCorrectly()
{
Container::getInstance()->instance('mailer', new class
{
public function render()
{
//
}
});
$this->stubMailer();

$mailable = new WelcomeMailableStub;
$mailable->to('taylor@laravel.com');
Expand Down Expand Up @@ -114,13 +108,7 @@ public function render()

public function testMailableSetsCcRecipientsCorrectly()
{
Container::getInstance()->instance('mailer', new class
{
public function render()
{
//
}
});
$this->stubMailer();

$mailable = new WelcomeMailableStub;
$mailable->cc('taylor@laravel.com');
Expand Down Expand Up @@ -211,13 +199,7 @@ public function render()

public function testMailableSetsBccRecipientsCorrectly()
{
Container::getInstance()->instance('mailer', new class
{
public function render()
{
//
}
});
$this->stubMailer();

$mailable = new WelcomeMailableStub;
$mailable->bcc('taylor@laravel.com');
Expand Down Expand Up @@ -308,13 +290,7 @@ public function render()

public function testMailableSetsReplyToCorrectly()
{
Container::getInstance()->instance('mailer', new class
{
public function render()
{
//
}
});
$this->stubMailer();

$mailable = new WelcomeMailableStub;
$mailable->replyTo('taylor@laravel.com');
Expand Down Expand Up @@ -394,13 +370,7 @@ public function render()

public function testMailableSetsFromCorrectly()
{
Container::getInstance()->instance('mailer', new class
{
public function render()
{
//
}
});
$this->stubMailer();

$mailable = new WelcomeMailableStub;
$mailable->from('taylor@laravel.com');
Expand Down Expand Up @@ -630,13 +600,7 @@ public function testMailablePriorityGetsSent()

public function testMailableMetadataGetsSent()
{
Container::getInstance()->instance('mailer', new class
{
public function render()
{
//
}
});
$this->stubMailer();

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

Expand Down Expand Up @@ -671,13 +635,7 @@ public function render()

public function testMailableTagGetsSent()
{
Container::getInstance()->instance('mailer', new class
{
public function render()
{
//
}
});
$this->stubMailer();

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

Expand Down Expand Up @@ -829,13 +787,7 @@ public function toMailAttachment()

public function testHasAttachmentWithEnvelopeAttachments()
{
Container::getInstance()->instance('mailer', new class
{
public function render()
{
//
}
});
$this->stubMailer();
$mailable = new class extends Mailable
{
public function envelope()
Expand Down Expand Up @@ -1034,13 +986,7 @@ public function testItCanCheckForStorageBasedAttachments()

public function testAssertHasAttachment()
{
Container::getInstance()->instance('mailer', new class
{
public function render()
{
//
}
});
$this->stubMailer();

$mailable = new class() extends Mailable
{
Expand Down Expand Up @@ -1070,13 +1016,7 @@ public function build()

public function testAssertHasAttachedData()
{
Container::getInstance()->instance('mailer', new class
{
public function render()
{
//
}
});
$this->stubMailer();

$mailable = new class() extends Mailable
{
Expand Down Expand Up @@ -1134,13 +1074,7 @@ public function build()

public function testAssertHasSubject()
{
Container::getInstance()->instance('mailer', new class
{
public function render()
{
//
}
});
$this->stubMailer();

$mailable = new class() extends Mailable
{
Expand Down Expand Up @@ -1194,13 +1128,7 @@ public function testMailableHeadersGetSent()

public function testMailableAttributesInBuild()
{
Container::getInstance()->instance('mailer', new class
{
public function render()
{
//
}
});
$this->stubMailer();

$mailable = new class() extends Mailable
{
Expand All @@ -1227,6 +1155,32 @@ public function build()
$mailable->assertHasMetadata('user_id', 1);
$mailable->assertHasSubject('test subject');
}

public function testMailablesCanBeTapped()
{
$this->stubMailer();

$mail = new WelcomeMailableStub;

$mail->tap(fn ($mailable) => $mailable->to('taylor@laravel.com', 'Taylor Otwell'));
$mail->tap(fn ($mailable) => $mailable->subject('Test Subject!'));

$mail->tap(function ($mailable) {
$mailable->assertTo('taylor@laravel.com')
->assertHasSubject('Test Subject!');
});
}

protected function stubMailer()
{
Container::getInstance()->instance('mailer', new class
{
public function render()
{
//
}
});
}
}

class MailableHeadersStub extends Mailable
Expand Down

0 comments on commit 36b09b7

Please sign in to comment.