From d397e57f74ada4b80c3af92f5666ad6bcf016990 Mon Sep 17 00:00:00 2001 From: Luke Kuzmish Date: Wed, 22 Jan 2025 20:32:54 -0500 Subject: [PATCH] wip --- .../Foundation/Bus/PendingDispatch.php | 14 ++--- .../Queue/InteractsWithQueueAndConnection.php | 42 +++++++++++++ .../Attributes => Queue}/OnConnection.php | 2 +- .../{Bus/Attributes => Queue}/OnQueue.php | 2 +- src/Illuminate/Mail/Mailable.php | 51 ++++++++++++---- .../BusPendingDispatchWithAttributesTest.php | 4 +- tests/Support/SupportMailTest.php | 61 ++++++++++++++++++- 7 files changed, 149 insertions(+), 27 deletions(-) create mode 100644 src/Illuminate/Foundation/Queue/InteractsWithQueueAndConnection.php rename src/Illuminate/Foundation/{Bus/Attributes => Queue}/OnConnection.php (82%) rename src/Illuminate/Foundation/{Bus/Attributes => Queue}/OnQueue.php (81%) diff --git a/src/Illuminate/Foundation/Bus/PendingDispatch.php b/src/Illuminate/Foundation/Bus/PendingDispatch.php index 5801a07e81f2..54a831a8945f 100644 --- a/src/Illuminate/Foundation/Bus/PendingDispatch.php +++ b/src/Illuminate/Foundation/Bus/PendingDispatch.php @@ -7,12 +7,12 @@ use Illuminate\Contracts\Bus\Dispatcher; use Illuminate\Contracts\Cache\Repository as Cache; use Illuminate\Contracts\Queue\ShouldBeUnique; -use Illuminate\Foundation\Bus\Attributes\OnConnection; -use Illuminate\Foundation\Bus\Attributes\OnQueue; +use Illuminate\Foundation\Queue\InteractsWithQueueAndConnection; use Illuminate\Foundation\Queue\InteractsWithUniqueJobs; class PendingDispatch { + use InteractsWithQueueAndConnection; use InteractsWithUniqueJobs; /** @@ -209,16 +209,14 @@ protected function setQueueAndConnectionFromAttributesIfNotSet(): void $reflectionClass = new \ReflectionClass($this->job); if (! $hasQueueSet) { - $onQueue = $reflectionClass->getAttributes(OnQueue::class); - if ($onQueue !== []) { - $this->onQueue($onQueue[0]->newInstance()->queue); + if ($queue = $this->getQueueFromOnConnectionAttribute($reflectionClass)) { + $this->onQueue($queue); } } if (! $hasConnectionSet) { - $onConnection = $reflectionClass->getAttributes(OnConnection::class); - if ($onConnection !== []) { - $this->onConnection($onConnection[0]->newInstance()->connection); + if ($connection = $this->getConnectionFromOnConnectionAttribute($reflectionClass)) { + $this->onConnection($connection); } } } diff --git a/src/Illuminate/Foundation/Queue/InteractsWithQueueAndConnection.php b/src/Illuminate/Foundation/Queue/InteractsWithQueueAndConnection.php new file mode 100644 index 000000000000..fc1d70d9ee34 --- /dev/null +++ b/src/Illuminate/Foundation/Queue/InteractsWithQueueAndConnection.php @@ -0,0 +1,42 @@ +getAttributes(OnConnection::class); + if ($onConnection === []) { + return null; + } + + return enum_value($onConnection[0]->newInstance()->connection); + } + + /** + * Extract the queue from OnQueue attribute if present. + * + * @param \ReflectionClass $reflectionClass + * @return string|\UnitEnum|null + */ + protected function getQueueFromOnConnectionAttribute(ReflectionClass $reflectionClass) + { + $onQueue = $reflectionClass->getAttributes(OnQueue::class); + if ($onQueue === []) { + return null; + } + + return enum_value($onQueue[0]->newInstance()->queue); + } +} diff --git a/src/Illuminate/Foundation/Bus/Attributes/OnConnection.php b/src/Illuminate/Foundation/Queue/OnConnection.php similarity index 82% rename from src/Illuminate/Foundation/Bus/Attributes/OnConnection.php rename to src/Illuminate/Foundation/Queue/OnConnection.php index 9a5f326f8d58..913d065f23af 100644 --- a/src/Illuminate/Foundation/Bus/Attributes/OnConnection.php +++ b/src/Illuminate/Foundation/Queue/OnConnection.php @@ -1,6 +1,6 @@ later($this->delay, $queue); } - $connection = property_exists($this, 'connection') ? $this->connection : null; - - $queueName = property_exists($this, 'queue') ? $this->queue : null; - - return $queue->connection($connection)->pushOn( - $queueName ?: null, $this->newQueuedJob() + return $queue->connection($this->getConnection())->pushOn( + $this->getQueue(), $this->newQueuedJob() ); } @@ -245,12 +242,8 @@ public function queue(Queue $queue) */ public function later($delay, Queue $queue) { - $connection = property_exists($this, 'connection') ? $this->connection : null; - - $queueName = property_exists($this, 'queue') ? $this->queue : null; - - return $queue->connection($connection)->laterOn( - $queueName ?: null, $delay, $this->newQueuedJob() + return $queue->connection($this->getConnection())->laterOn( + $this->getQueue(), $delay, $this->newQueuedJob() ); } @@ -467,6 +460,38 @@ protected function buildSubject($message) return $this; } + /** + * Get the queue specified on the class or from the OnQueue attribute. + * + * @return string|\UnitEnum|null + */ + protected function getQueue() + { + $queue = property_exists($this, 'queue') ? $this->queue : null; + + if ($queue === null) { + $queue = $this->getQueueFromOnConnectionAttribute(new ReflectionClass($this)); + } + + return $queue; + } + + /** + * Get the connection specified on the class or from the OnConnection attribute. + * + * @return string|\UnitEnum|null + */ + protected function getConnection() + { + $connection = property_exists($this, 'connection') ? $this->connection : null; + + if ($connection === null) { + $connection = $this->getConnectionFromOnConnectionAttribute(new ReflectionClass($this)); + } + + return $connection; + } + /** * Add all of the attachments to the message. * diff --git a/tests/Integration/Bus/BusPendingDispatchWithAttributesTest.php b/tests/Integration/Bus/BusPendingDispatchWithAttributesTest.php index 51e1ff62b5ed..08ea3471abef 100644 --- a/tests/Integration/Bus/BusPendingDispatchWithAttributesTest.php +++ b/tests/Integration/Bus/BusPendingDispatchWithAttributesTest.php @@ -4,9 +4,9 @@ use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; -use Illuminate\Foundation\Bus\Attributes\OnConnection; -use Illuminate\Foundation\Bus\Attributes\OnQueue; use Illuminate\Foundation\Bus\Dispatchable; +use Illuminate\Foundation\Queue\OnConnection; +use Illuminate\Foundation\Queue\OnQueue; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Support\Facades\Queue; use Orchestra\Testbench\TestCase; diff --git a/tests/Support/SupportMailTest.php b/tests/Support/SupportMailTest.php index 146a3ca331a5..307c72c792ec 100644 --- a/tests/Support/SupportMailTest.php +++ b/tests/Support/SupportMailTest.php @@ -2,15 +2,21 @@ namespace Illuminate\Tests\Support; +use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Foundation\Queue\OnConnection; +use Illuminate\Foundation\Queue\OnQueue; use Illuminate\Mail\Mailable; +use Illuminate\Mail\SendQueuedMailable; use Illuminate\Support\Facades\Mail; +use Illuminate\Support\Facades\Queue; use Orchestra\Testbench\TestCase; +use PHPUnit\Framework\Attributes\TestWith; class SupportMailTest extends TestCase { public function testItRegisterAndCallMacros() { - Mail::macro('test', fn (string $str) => $str === 'foo' + Mail::macro('test', fn(string $str) => $str === 'foo' ? 'it works!' : 'it failed.', ); @@ -20,7 +26,7 @@ public function testItRegisterAndCallMacros() public function testItRegisterAndCallMacrosWhenFaked() { - Mail::macro('test', fn (string $str) => $str === 'foo' + Mail::macro('test', fn(string $str) => $str === 'foo' ? 'it works!' : 'it failed.', ); @@ -39,6 +45,33 @@ public function testEmailSent() Mail::assertSent(TestMail::class); } + + #[TestWith([TestMailWithOnQueue::class, 'my-queue'], 'string for queue')] + #[TestWith([TestMailWithEnumOnQueue::class, 'queue-from-enum'], 'enum for queue')] + public function testQueuedMailableRespectsStringOnQueueAttribute(string $mailableClass, string $queueName) + { + Queue::fake(); + + Mail::send(new $mailableClass()); + + Queue::assertPushedOn( + $queueName, + SendQueuedMailable::class, + fn ($job) => is_a($job->mailable, $mailableClass, true) + ); + } + + public function testQueueableMailableUsesQueueIfSetAsProperty() + { + Queue::fake(); + Mail::send(new TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet()); + + Queue::assertPushed(function(SendQueuedMailable $sendQueuedMailable) { + return $sendQueuedMailable->mailable instanceof TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet + && $sendQueuedMailable->connection === 'my-connection' + && $sendQueuedMailable->queue === 'some-other-queue'; + }); + } } class TestMail extends Mailable @@ -53,3 +86,27 @@ public function build() return $this->view('view'); } } + +#[OnQueue('my-queue')] +class TestMailWithOnQueue extends Mailable implements ShouldQueue +{ +} + +#[OnQueue(SupportMailTestEnum::Queue)] +class TestMailWithEnumOnQueue extends Mailable implements ShouldQueue +{ +} + +#[OnQueue(SupportMailTestEnum::Queue)] +#[OnConnection(SupportMailTestEnum::Connection)] +class TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet extends Mailable implements ShouldQueue +{ + public $queue = 'some-other-queue'; + public $connection = 'my-connection'; +} + +enum SupportMailTestEnum: string +{ + case Queue = 'queue-from-enum'; + case Connection = 'connection-from-enum'; +}