Skip to content

Commit 488cbf5

Browse files
[12.x] Add hasMailer method to the mailable class (#56340)
* Add the hasMailer class to the Mailable object. * Set the $currentMailer of MailFake to be the default driver when initialising. * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 9499a6c commit 488cbf5

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

src/Illuminate/Mail/Mailable.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,17 @@ private function ensureAttachmentsAreHydrated()
18111811
});
18121812
}
18131813

1814+
/**
1815+
* Determine if the mailable will be sent by the given mailer.
1816+
*
1817+
* @param string $mailer
1818+
* @return bool
1819+
*/
1820+
public function usesMailer($mailer)
1821+
{
1822+
return $this->mailer === $mailer;
1823+
}
1824+
18141825
/**
18151826
* Set the name of the mailer that should send the message.
18161827
*

src/Illuminate/Support/Testing/Fakes/MailFake.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class MailFake implements Factory, Fake, Mailer, MailQueue
5555
public function __construct(MailManager $manager)
5656
{
5757
$this->manager = $manager;
58+
$this->currentMailer = $manager->getDefaultDriver();
5859
}
5960

6061
/**

tests/Mail/MailMailableTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,11 @@ public function testMailerMayBeSet()
573573
$mailable = new WelcomeMailableStub;
574574

575575
$mailable->mailer('array');
576+
$this->assertTrue($mailable->usesMailer('array'));
576577

577-
$mailable = unserialize(serialize($mailable));
578-
579-
$this->assertSame('array', $mailable->mailer);
578+
$mailable->mailer('smtp');
579+
$this->assertTrue($mailable->usesMailer('smtp'));
580+
$this->assertFalse($mailable->usesMailer('ses'));
580581
}
581582

582583
public function testMailablePriorityGetsSent()

tests/Support/SupportTestingMailFakeTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ class SupportTestingMailFakeTest extends TestCase
3131
protected function setUp(): void
3232
{
3333
parent::setUp();
34-
$this->mailManager = m::mock(MailManager::class);
34+
$this->mailManager = m::mock(MailManager::class, function ($mock) {
35+
$mock->shouldReceive('getDefaultDriver')
36+
->andReturn('smtp');
37+
});
3538
$this->fake = new MailFake($this->mailManager);
3639
$this->mailable = new MailableStub;
3740
}
@@ -380,6 +383,33 @@ public function testMissingMethodsAreForwarded()
380383

381384
$this->assertEquals('bar', $this->fake->foo());
382385
}
386+
387+
public function testAssertMailer()
388+
{
389+
$this->fake->to('[email protected]')->send($this->mailable);
390+
391+
$this->fake->assertSent(MailableStub::class, function ($mail) {
392+
return $mail->usesMailer('smtp');
393+
});
394+
395+
$this->fake->mailer('ses')->to('[email protected]')->send($this->mailable);
396+
397+
$this->fake->assertSent(MailableStub::class, function ($mail) {
398+
return $mail->usesMailer('ses');
399+
});
400+
401+
$this->fake->mailer('sendgrid')->to('[email protected]')->queue($this->mailable);
402+
403+
$this->fake->assertQueued(MailableStub::class, function ($mail) {
404+
return $mail->usesMailer('sendgrid');
405+
});
406+
407+
$this->fake->mailer('mailjet')->to('[email protected]')->queue($this->mailable);
408+
409+
$this->fake->assertQueued(MailableStub::class, function ($mail) {
410+
return $mail->usesMailer('mailjet');
411+
});
412+
}
383413
}
384414

385415
class MailableStub extends Mailable

0 commit comments

Comments
 (0)