From 27825662a49e67f17ad62ebb5359b21b4b765dd6 Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Mon, 20 Jan 2025 15:35:45 +0100 Subject: [PATCH] Add queueMailsByDefault method --- src/Illuminate/Mail/Mailer.php | 20 ++++++++++++++++++- src/Illuminate/Support/Facades/Mail.php | 1 + .../Support/Testing/Fakes/MailFake.php | 20 ++++++++++++++++++- tests/Support/SupportTestingMailFakeTest.php | 16 +++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Mail/Mailer.php b/src/Illuminate/Mail/Mailer.php index bd6484f3ea67..9ab04e68b1d5 100755 --- a/src/Illuminate/Mail/Mailer.php +++ b/src/Illuminate/Mail/Mailer.php @@ -88,6 +88,13 @@ class Mailer implements MailerContract, MailQueueContract */ protected $queue; + /** + * Indicates if mails should be queued by default. + * + * @var bool + */ + protected $queueMailsByDefault = false; + /** * Create a new Mailer instance. * @@ -349,7 +356,7 @@ public function send($view, array $data = [], $callback = null) */ protected function sendMailable(MailableContract $mailable) { - return $mailable instanceof ShouldQueue + return ($mailable instanceof ShouldQueue || $this->queueMailsByDefault) ? $mailable->mailer($this->name)->queue($this->queue) : $mailable->mailer($this->name)->send($this); } @@ -461,6 +468,17 @@ protected function setGlobalToAndRemoveCcAndBcc($message) $message->forgetBcc(); } + /** + * Indicate that all mailables should be queued by default. + * + * @param bool $queue + * @return void + */ + public function queueMailsByDefault(bool $shouldQueue = true) + { + $this->queueMailsByDefault = $shouldQueue; + } + /** * Queue a new mail message for sending. * diff --git a/src/Illuminate/Support/Facades/Mail.php b/src/Illuminate/Support/Facades/Mail.php index fb4019b25aae..65083a94306c 100755 --- a/src/Illuminate/Support/Facades/Mail.php +++ b/src/Illuminate/Support/Facades/Mail.php @@ -57,6 +57,7 @@ * @method static bool hasSent(string $mailable) * @method static \Illuminate\Support\Collection queued(string|\Closure $mailable, callable|null $callback = null) * @method static bool hasQueued(string $mailable) + * @method static void queueMailsByDefault(bool $shouldQueue = true) * * @see \Illuminate\Mail\MailManager * @see \Illuminate\Support\Testing\Fakes\MailFake diff --git a/src/Illuminate/Support/Testing/Fakes/MailFake.php b/src/Illuminate/Support/Testing/Fakes/MailFake.php index 7d4f16b334d0..94bce865cc85 100644 --- a/src/Illuminate/Support/Testing/Fakes/MailFake.php +++ b/src/Illuminate/Support/Testing/Fakes/MailFake.php @@ -47,6 +47,13 @@ class MailFake implements Factory, Fake, Mailer, MailQueue */ protected $queuedMailables = []; + /** + * Indicates if mails should be queued by default. + * + * @var bool + */ + protected $queueMailsByDefault = false; + /** * Create a new mail fake. * @@ -476,7 +483,7 @@ public function raw($text, $callback) */ public function send($view, array $data = [], $callback = null) { - return $this->sendMail($view, $view instanceof ShouldQueue); + return $this->sendMail($view, ($view instanceof ShouldQueue || $this->queueMailsByDefault)); } /** @@ -516,6 +523,17 @@ protected function sendMail($view, $shouldQueue = false) $this->mailables[] = $view; } + /** + * Indicate that all mailables should be queued by default. + * + * @param bool $queue + * @return void + */ + public function queueMailsByDefault(bool $shouldQueue = true) + { + $this->queueMailsByDefault = $shouldQueue; + } + /** * Queue a new message for sending. * diff --git a/tests/Support/SupportTestingMailFakeTest.php b/tests/Support/SupportTestingMailFakeTest.php index 07189bb92efc..f94ec12d1d31 100644 --- a/tests/Support/SupportTestingMailFakeTest.php +++ b/tests/Support/SupportTestingMailFakeTest.php @@ -310,6 +310,22 @@ public function testSendQueuesAMailableThatShouldBeQueued() } } + public function testSendQueuesAMailableThatShouldBeQueuedByDefault() + { + $this->fake->queueMailsByDefault(); + + $this->fake->to('taylor@laravel.com')->send($this->mailable); + + $this->fake->assertQueued(MailableStub::class); + + try { + $this->fake->assertSent(MailableStub::class); + $this->fail(); + } catch (ExpectationFailedException $e) { + $this->assertStringContainsString('The expected [Illuminate\Tests\Support\MailableStub] mailable was not sent.', $e->getMessage()); + } + } + public function testAssertNothingSent() { $this->fake->assertNothingSent();