Skip to content

Commit

Permalink
Add queueMailsByDefault method
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Jan 20, 2025
1 parent fffdd53 commit 2782566
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/Illuminate/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion src/Illuminate/Support/Testing/Fakes/MailFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/Support/SupportTestingMailFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,22 @@ public function testSendQueuesAMailableThatShouldBeQueued()
}
}

public function testSendQueuesAMailableThatShouldBeQueuedByDefault()
{
$this->fake->queueMailsByDefault();

$this->fake->to('[email protected]')->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();
Expand Down

0 comments on commit 2782566

Please sign in to comment.