Skip to content

Commit 1e320eb

Browse files
committed
mailable
1 parent d397e57 commit 1e320eb

File tree

4 files changed

+111
-18
lines changed

4 files changed

+111
-18
lines changed

src/Illuminate/Foundation/Bus/PendingDispatch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ protected function setQueueAndConnectionFromAttributesIfNotSet(): void
209209

210210
$reflectionClass = new \ReflectionClass($this->job);
211211
if (! $hasQueueSet) {
212-
if ($queue = $this->getQueueFromOnConnectionAttribute($reflectionClass)) {
212+
if ($queue = $this->getQueueFromOnQueueAttribute($reflectionClass)) {
213213
$this->onQueue($queue);
214214
}
215215
}

src/Illuminate/Foundation/Queue/InteractsWithQueueAndConnection.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use ReflectionClass;
66

7-
use function Illuminate\Support\enum_value;
8-
97
trait InteractsWithQueueAndConnection
108
{
119
/**
@@ -21,7 +19,7 @@ protected function getConnectionFromOnConnectionAttribute(ReflectionClass $refle
2119
return null;
2220
}
2321

24-
return enum_value($onConnection[0]->newInstance()->connection);
22+
return $onConnection[0]->newInstance()->connection;
2523
}
2624

2725
/**
@@ -30,13 +28,13 @@ protected function getConnectionFromOnConnectionAttribute(ReflectionClass $refle
3028
* @param \ReflectionClass $reflectionClass
3129
* @return string|\UnitEnum|null
3230
*/
33-
protected function getQueueFromOnConnectionAttribute(ReflectionClass $reflectionClass)
31+
protected function getQueueFromOnQueueAttribute(ReflectionClass $reflectionClass)
3432
{
3533
$onQueue = $reflectionClass->getAttributes(OnQueue::class);
3634
if ($onQueue === []) {
3735
return null;
3836
}
3937

40-
return enum_value($onQueue[0]->newInstance()->queue);
38+
return $onQueue[0]->newInstance()->queue;
4139
}
4240
}

src/Illuminate/Mail/Mailable.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Symfony\Component\Mailer\Header\MetadataHeader;
2929
use Symfony\Component\Mailer\Header\TagHeader;
3030
use Symfony\Component\Mime\Address;
31+
use function Illuminate\Support\enum_value;
3132

3233
class Mailable implements MailableContract, Renderable
3334
{
@@ -463,23 +464,23 @@ protected function buildSubject($message)
463464
/**
464465
* Get the queue specified on the class or from the OnQueue attribute.
465466
*
466-
* @return string|\UnitEnum|null
467+
* @return string|null
467468
*/
468469
protected function getQueue()
469470
{
470471
$queue = property_exists($this, 'queue') ? $this->queue : null;
471472

472473
if ($queue === null) {
473-
$queue = $this->getQueueFromOnConnectionAttribute(new ReflectionClass($this));
474+
$queue = $this->getQueueFromOnQueueAttribute(new ReflectionClass($this));
474475
}
475476

476-
return $queue;
477+
return $queue !== null ? enum_value($queue) : null;
477478
}
478479

479480
/**
480481
* Get the connection specified on the class or from the OnConnection attribute.
481482
*
482-
* @return string|\UnitEnum|null
483+
* @return string|null
483484
*/
484485
protected function getConnection()
485486
{
@@ -489,7 +490,7 @@ protected function getConnection()
489490
$connection = $this->getConnectionFromOnConnectionAttribute(new ReflectionClass($this));
490491
}
491492

492-
return $connection;
493+
return $connection !== null ? enum_value($connection) : null;
493494
}
494495

495496
/**

tests/Support/SupportMailTest.php

Lines changed: 101 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
use Illuminate\Foundation\Queue\OnQueue;
88
use Illuminate\Mail\Mailable;
99
use Illuminate\Mail\SendQueuedMailable;
10+
use Illuminate\Queue\Events\JobProcessing;
1011
use Illuminate\Support\Facades\Mail;
1112
use Illuminate\Support\Facades\Queue;
1213
use Orchestra\Testbench\TestCase;
14+
use PHPUnit\Framework\Attributes\DataProvider;
1315
use PHPUnit\Framework\Attributes\TestWith;
1416

1517
class SupportMailTest extends TestCase
1618
{
1719
public function testItRegisterAndCallMacros()
1820
{
19-
Mail::macro('test', fn(string $str) => $str === 'foo'
21+
Mail::macro('test', fn (string $str) => $str === 'foo'
2022
? 'it works!'
2123
: 'it failed.',
2224
);
@@ -26,7 +28,7 @@ public function testItRegisterAndCallMacros()
2628

2729
public function testItRegisterAndCallMacrosWhenFaked()
2830
{
29-
Mail::macro('test', fn(string $str) => $str === 'foo'
31+
Mail::macro('test', fn (string $str) => $str === 'foo'
3032
? 'it works!'
3133
: 'it failed.',
3234
);
@@ -46,9 +48,8 @@ public function testEmailSent()
4648
Mail::assertSent(TestMail::class);
4749
}
4850

49-
#[TestWith([TestMailWithOnQueue::class, 'my-queue'], 'string for queue')]
50-
#[TestWith([TestMailWithEnumOnQueue::class, 'queue-from-enum'], 'enum for queue')]
51-
public function testQueuedMailableRespectsStringOnQueueAttribute(string $mailableClass, string $queueName)
51+
#[DataProvider('queueDataProvider')]
52+
public function testQueuedMailableRespectsOnQueueAttribute(string $mailableClass, string $queueName)
5253
{
5354
Queue::fake();
5455

@@ -61,17 +62,87 @@ public function testQueuedMailableRespectsStringOnQueueAttribute(string $mailabl
6162
);
6263
}
6364

64-
public function testQueueableMailableUsesQueueIfSetAsProperty()
65+
#[DataProvider('queueDataProvider')]
66+
public function testQueuedMailableDispatchedLaterRespectsOnQueueAttribute(string $mailableClass, string $queueName)
67+
{
68+
Queue::fake();
69+
70+
Mail::later(100, new $mailableClass);
71+
72+
Queue::assertPushedOn(
73+
$queueName,
74+
SendQueuedMailable::class,
75+
fn ($job) => is_a($job->mailable, $mailableClass, true)
76+
);
77+
}
78+
79+
#[DataProvider('connectionDataProvider')]
80+
public function testQueuedMailableRespectsOnConnectionAttribute(string $mailableClass, string $connectionName)
81+
{
82+
Queue::fake();
83+
84+
Queue::before(function (JobProcessing $jobProcessing) use ($connectionName) {
85+
$this->assertEquals($connectionName, $jobProcessing->connectionName);
86+
});
87+
88+
Mail::send(new $mailableClass());
89+
90+
}
91+
92+
#[DataProvider('connectionDataProvider')]
93+
public function testLaterQueuedMailableRespectsOnConnectionAttribute(string $mailableClass, string $connectionName)
94+
{
95+
Queue::fake();
96+
97+
Queue::before(function (JobProcessing $jobProcessing) use ($connectionName) {
98+
$this->assertEquals($connectionName, $jobProcessing->connectionName);
99+
});
100+
101+
Mail::later(100, new $mailableClass());
102+
}
103+
104+
public function testQueueableMailableUsesQueueAndConnectionFromClassProperties()
65105
{
66106
Queue::fake();
67107
Mail::send(new TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet());
68108

69-
Queue::assertPushed(function(SendQueuedMailable $sendQueuedMailable) {
109+
Queue::assertPushed(function (SendQueuedMailable $sendQueuedMailable) {
110+
return $sendQueuedMailable->mailable instanceof TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet
111+
&& $sendQueuedMailable->connection === 'my-connection'
112+
&& $sendQueuedMailable->queue === 'some-other-queue';
113+
});
114+
}
115+
116+
public function testQueueableMailableDispatchedLaterUsesQueueAndConnectionFromClassProperties()
117+
{
118+
Queue::fake();
119+
Mail::later(100, new TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet());
120+
121+
Queue::assertPushed(function (SendQueuedMailable $sendQueuedMailable) {
70122
return $sendQueuedMailable->mailable instanceof TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet
71123
&& $sendQueuedMailable->connection === 'my-connection'
72124
&& $sendQueuedMailable->queue === 'some-other-queue';
73125
});
74126
}
127+
128+
/**
129+
* @return array<string, array{class-string<Mailable>, string}>
130+
*/
131+
public static function queueDataProvider(): array
132+
{
133+
return [
134+
'string for queue' => [TestMailWithOnQueue::class, 'my-queue'],
135+
'enum for queue' => [TestMailWithEnumOnQueue::class, 'queue-from-enum'],
136+
];
137+
}
138+
139+
public static function connectionDataProvider(): array
140+
{
141+
return [
142+
'string for connection' => [TestMailWithOnConnection::class, 'connection-string'],
143+
'enum for connection' => [TestMailWithEnumOnConnection::class, 'connection-from-enum'],
144+
];
145+
}
75146
}
76147

77148
class TestMail extends Mailable
@@ -97,6 +168,29 @@ class TestMailWithEnumOnQueue extends Mailable implements ShouldQueue
97168
{
98169
}
99170

171+
#[OnConnection('connection-string')]
172+
class TestMailWithOnConnection extends Mailable implements ShouldQueue
173+
{
174+
public $queue = 'queue';
175+
176+
public function build()
177+
{
178+
return $this->view('view');
179+
}
180+
}
181+
182+
#[OnConnection(SupportMailTestEnum::Connection)]
183+
class TestMailWithEnumOnConnection extends Mailable implements ShouldQueue
184+
{
185+
public $queue = 'queue';
186+
187+
public function build()
188+
{
189+
return $this->view('view');
190+
}
191+
}
192+
193+
100194
#[OnQueue(SupportMailTestEnum::Queue)]
101195
#[OnConnection(SupportMailTestEnum::Connection)]
102196
class TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet extends Mailable implements ShouldQueue

0 commit comments

Comments
 (0)