Skip to content

Commit 2ba8e5c

Browse files
committed
Add ability to default mailables and notifcations
1 parent d158526 commit 2ba8e5c

File tree

5 files changed

+92
-14
lines changed

5 files changed

+92
-14
lines changed

src/Illuminate/Mail/Mailable.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ class Mailable implements MailableContract, Renderable
189189
*/
190190
public static $viewDataCallback;
191191

192+
/**
193+
* The name of the default queue that should be used when queued.
194+
*
195+
* @var string|null
196+
*/
197+
public static $defaultQueue;
198+
192199
/**
193200
* Send the message using the given mailer.
194201
*
@@ -230,7 +237,7 @@ public function queue(Queue $queue)
230237

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

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

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

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

253260
return $queue->connection($connection)->laterOn(
254261
$queueName ?: null, $delay, $this->newQueuedJob()

src/Illuminate/Notifications/Notification.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ class Notification
2222
*/
2323
public $locale;
2424

25+
/**
26+
* The name of the default queue that should be used when queued.
27+
*
28+
* @var string|null
29+
*/
30+
public static $defaultQueue;
31+
2532
/**
2633
* Get the channels the event should broadcast on.
2734
*

src/Illuminate/Notifications/NotificationSender.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ protected function queueNotification($notifiables, $notification)
235235
$connection = $notification->viaConnections()[$channel] ?? null;
236236
}
237237

238-
$queue = $notification->queue;
238+
$queue = $notification->queue ?? Notification::$defaultQueue;
239239

240240
if (method_exists($notification, 'viaQueues')) {
241241
$queue = $notification->viaQueues()[$channel] ?? null;

tests/Mail/MailableQueuedTest.php

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Illuminate\Support\Testing\Fakes\QueueFake;
1616
use Laravel\SerializableClosure\SerializableClosure;
1717
use Mockery as m;
18+
use PHPUnit\Framework\Attributes\DataProvider;
1819
use PHPUnit\Framework\TestCase;
1920
use Symfony\Component\Mailer\Transport\TransportInterface;
2021

@@ -25,18 +26,35 @@ protected function tearDown(): void
2526
m::close();
2627
}
2728

28-
public function testQueuedMailableSent(): void
29+
public static function defaultQueueDataProvider()
2930
{
30-
$queueFake = new QueueFake(new Application);
31-
$mailer = $this->getMockBuilder(Mailer::class)
32-
->setConstructorArgs($this->getMocks())
33-
->onlyMethods(['createMessage', 'to'])
34-
->getMock();
35-
$mailer->setQueue($queueFake);
36-
$mailable = new MailableQueueableStub;
37-
$queueFake->assertNothingPushed();
38-
$mailer->send($mailable);
39-
$queueFake->assertPushedOn(null, SendQueuedMailable::class);
31+
return [
32+
['some-queue'],
33+
[null],
34+
];
35+
}
36+
37+
#[DataProvider('defaultQueueDataProvider')]
38+
public function testDefaultQueue($queue): void
39+
{
40+
try {
41+
if ($queue) {
42+
Mailable::$defaultQueue = $queue;
43+
}
44+
45+
$queueFake = new QueueFake(new Application);
46+
$mailer = $this->getMockBuilder(Mailer::class)
47+
->setConstructorArgs($this->getMocks())
48+
->onlyMethods(['createMessage', 'to'])
49+
->getMock();
50+
$mailer->setQueue($queueFake);
51+
$mailable = new MailableQueueableStub;
52+
$queueFake->assertNothingPushed();
53+
$mailer->send($mailable);
54+
$queueFake->assertPushedOn($queue, SendQueuedMailable::class);
55+
} finally {
56+
Mailable::$defaultQueue = null;
57+
}
4058
}
4159

4260
public function testQueuedMailableWithAttachmentSent(): void

tests/Notifications/NotificationSenderTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Illuminate\Notifications\Notification;
1515
use Illuminate\Notifications\NotificationSender;
1616
use Mockery as m;
17+
use PHPUnit\Framework\Attributes\DataProvider;
1718
use PHPUnit\Framework\TestCase;
1819
use Symfony\Component\Mailer\Exception\HttpTransportException;
1920
use Symfony\Component\Mailer\Exception\TransportException;
@@ -166,6 +167,51 @@ public function testNotificationFailedSentWithoutHttpTransportException()
166167

167168
$sender->sendNow($notifiable, new DummyNotificationWithViaConnections(), ['mail']);
168169
}
170+
171+
public static function defaultNotificationQueueDataProvider()
172+
{
173+
return [
174+
['some-queue'],
175+
[null],
176+
];
177+
}
178+
179+
#[DataProvider('defaultNotificationQueueDataProvider')]
180+
public function testDefaultNotificationQueue($queue)
181+
{
182+
try {
183+
if ($queue) {
184+
Notification::$defaultQueue = $queue;
185+
}
186+
187+
$notifiable = new NotifiableUser;
188+
$manager = m::mock(ChannelManager::class);
189+
$manager->shouldReceive('getContainer')->andReturn(app());
190+
191+
$dispatchedJob = null;
192+
$bus = m::mock(BusDispatcher::class);
193+
$bus->shouldReceive('dispatch')
194+
->once()
195+
->withArgs(function ($job) use ($queue, &$dispatchedJob) {
196+
$dispatchedJob = $job;
197+
198+
return $job->queue === $queue;
199+
});
200+
201+
$events = m::mock(EventDispatcher::class);
202+
$events->shouldReceive('listen')->once();
203+
204+
$sender = new NotificationSender($manager, $bus, $events);
205+
206+
$notification = new DummyNotificationWithMiddleware;
207+
208+
$sender->send($notifiable, $notification);
209+
210+
$this->assertSame($queue, $dispatchedJob->queue);
211+
} finally {
212+
Notification::$defaultQueue = null;
213+
}
214+
}
169215
}
170216

171217
class DummyQueuedNotificationWithStringVia extends Notification implements ShouldQueue

0 commit comments

Comments
 (0)