Skip to content
Open
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
11 changes: 9 additions & 2 deletions src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ class Mailable implements MailableContract, Renderable
*/
public static $viewDataCallback;

/**
* The name of the default queue that should be used when queued.
*
* @var string|null
*/
public static $defaultQueue;

/**
* Send the message using the given mailer.
*
Expand Down Expand Up @@ -230,7 +237,7 @@ public function queue(Queue $queue)

$connection = property_exists($this, 'connection') ? $this->connection : null;

$queueName = property_exists($this, 'queue') ? $this->queue : null;
$queueName = (property_exists($this, 'queue') ? $this->queue : null) ?? static::$defaultQueue;

return $queue->connection($connection)->pushOn(
$queueName ?: null, $this->newQueuedJob()
Expand All @@ -248,7 +255,7 @@ public function later($delay, Queue $queue)
{
$connection = property_exists($this, 'connection') ? $this->connection : null;

$queueName = property_exists($this, 'queue') ? $this->queue : null;
$queueName = (property_exists($this, 'queue') ? $this->queue : null) ?? static::$defaultQueue;

return $queue->connection($connection)->laterOn(
$queueName ?: null, $delay, $this->newQueuedJob()
Expand Down
7 changes: 7 additions & 0 deletions src/Illuminate/Notifications/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class Notification
*/
public $locale;

/**
* The name of the default queue that should be used when queued.
*
* @var string|null
*/
public static $defaultQueue;

/**
* Get the channels the event should broadcast on.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Notifications/NotificationSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ protected function queueNotification($notifiables, $notification)
$connection = $notification->viaConnections()[$channel] ?? null;
}

$queue = $notification->queue;
$queue = $notification->queue ?? Notification::$defaultQueue;

if (method_exists($notification, 'viaQueues')) {
$queue = $notification->viaQueues()[$channel] ?? null;
Expand Down
40 changes: 29 additions & 11 deletions tests/Mail/MailableQueuedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Illuminate\Support\Testing\Fakes\QueueFake;
use Laravel\SerializableClosure\SerializableClosure;
use Mockery as m;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Transport\TransportInterface;

Expand All @@ -25,18 +26,35 @@ protected function tearDown(): void
m::close();
}

public function testQueuedMailableSent(): void
public static function defaultQueueDataProvider()
{
$queueFake = new QueueFake(new Application);
$mailer = $this->getMockBuilder(Mailer::class)
->setConstructorArgs($this->getMocks())
->onlyMethods(['createMessage', 'to'])
->getMock();
$mailer->setQueue($queueFake);
$mailable = new MailableQueueableStub;
$queueFake->assertNothingPushed();
$mailer->send($mailable);
$queueFake->assertPushedOn(null, SendQueuedMailable::class);
return [
['some-queue'],
[null],
];
}

#[DataProvider('defaultQueueDataProvider')]
public function testDefaultQueue($queue): void
{
try {
if ($queue) {
Mailable::$defaultQueue = $queue;
}

$queueFake = new QueueFake(new Application);
$mailer = $this->getMockBuilder(Mailer::class)
->setConstructorArgs($this->getMocks())
->onlyMethods(['createMessage', 'to'])
->getMock();
$mailer->setQueue($queueFake);
$mailable = new MailableQueueableStub;
$queueFake->assertNothingPushed();
$mailer->send($mailable);
$queueFake->assertPushedOn($queue, SendQueuedMailable::class);
} finally {
Mailable::$defaultQueue = null;
}
}

public function testQueuedMailableWithAttachmentSent(): void
Expand Down
46 changes: 46 additions & 0 deletions tests/Notifications/NotificationSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\NotificationSender;
use Mockery as m;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mailer\Exception\TransportException;
Expand Down Expand Up @@ -166,6 +167,51 @@ public function testNotificationFailedSentWithoutHttpTransportException()

$sender->sendNow($notifiable, new DummyNotificationWithViaConnections(), ['mail']);
}

public static function defaultNotificationQueueDataProvider()
{
return [
['some-queue'],
[null],
];
}

#[DataProvider('defaultNotificationQueueDataProvider')]
public function testDefaultNotificationQueue($queue)
{
try {
if ($queue) {
Notification::$defaultQueue = $queue;
}

$notifiable = new NotifiableUser;
$manager = m::mock(ChannelManager::class);
$manager->shouldReceive('getContainer')->andReturn(app());

$dispatchedJob = null;
$bus = m::mock(BusDispatcher::class);
$bus->shouldReceive('dispatch')
->once()
->withArgs(function ($job) use ($queue, &$dispatchedJob) {
$dispatchedJob = $job;

return $job->queue === $queue;
});

$events = m::mock(EventDispatcher::class);
$events->shouldReceive('listen')->once();

$sender = new NotificationSender($manager, $bus, $events);

$notification = new DummyNotificationWithMiddleware;

$sender->send($notifiable, $notification);

$this->assertSame($queue, $dispatchedJob->queue);
} finally {
Notification::$defaultQueue = null;
}
}
}

class DummyQueuedNotificationWithStringVia extends Notification implements ShouldQueue
Expand Down