Skip to content

[11.x] Make mailables tappable #53788

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

Merged
merged 1 commit into from
Dec 8, 2024
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
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('[email protected]');
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('[email protected]');
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('[email protected]');
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('[email protected]');
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('[email protected]');
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('[email protected]', 'Taylor Otwell'));
$mail->tap(fn ($mailable) => $mailable->subject('Test Subject!'));

$mail->tap(function ($mailable) {
$mailable->assertTo('[email protected]')
->assertHasSubject('Test Subject!');
});
}

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

class MailableHeadersStub extends Mailable
Expand Down
Loading