Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmastech committed Jan 23, 2025
1 parent 326a7b6 commit d397e57
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 27 deletions.
14 changes: 6 additions & 8 deletions src/Illuminate/Foundation/Bus/PendingDispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Illuminate\Foundation\Queue;

use ReflectionClass;

use function Illuminate\Support\enum_value;

trait InteractsWithQueueAndConnection
{
/**
* Extract the connection from OnConnection attribute if present.
*
* @param \ReflectionClass $reflectionClass
* @return string|\UnitEnum|null
*/
protected function getConnectionFromOnConnectionAttribute(ReflectionClass $reflectionClass)
{
$onConnection = $reflectionClass->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);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Illuminate\Foundation\Bus\Attributes;
namespace Illuminate\Foundation\Queue;

use Attribute;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Illuminate\Foundation\Bus\Attributes;
namespace Illuminate\Foundation\Queue;

use Attribute;

Expand Down
51 changes: 38 additions & 13 deletions src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Foundation\Queue\InteractsWithQueueAndConnection;
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
Expand All @@ -30,7 +31,7 @@

class Mailable implements MailableContract, Renderable
{
use Conditionable, ForwardsCalls, Localizable, Tappable, Macroable {
use Conditionable, ForwardsCalls, Localizable, Tappable, InteractsWithQueueAndConnection, Macroable {
__call as macroCall;
}

Expand Down Expand Up @@ -227,12 +228,8 @@ public function queue(Queue $queue)
return $this->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()
);
}

Expand All @@ -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()
);
}

Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
61 changes: 59 additions & 2 deletions tests/Support/SupportMailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
);
Expand All @@ -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.',
);
Expand All @@ -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
Expand All @@ -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';
}

0 comments on commit d397e57

Please sign in to comment.