Skip to content

Commit

Permalink
mailable
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmastech committed Jan 23, 2025
1 parent d397e57 commit 60600fe
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Bus/PendingDispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ protected function setQueueAndConnectionFromAttributesIfNotSet(): void

$reflectionClass = new \ReflectionClass($this->job);
if (! $hasQueueSet) {
if ($queue = $this->getQueueFromOnConnectionAttribute($reflectionClass)) {
if ($queue = $this->getQueueFromOnQueueAttribute($reflectionClass)) {
$this->onQueue($queue);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use ReflectionClass;

use function Illuminate\Support\enum_value;

trait InteractsWithQueueAndConnection
{
/**
Expand All @@ -21,7 +19,7 @@ protected function getConnectionFromOnConnectionAttribute(ReflectionClass $refle
return null;
}

return enum_value($onConnection[0]->newInstance()->connection);
return $onConnection[0]->newInstance()->connection;
}

/**
Expand All @@ -30,13 +28,13 @@ protected function getConnectionFromOnConnectionAttribute(ReflectionClass $refle
* @param \ReflectionClass $reflectionClass
* @return string|\UnitEnum|null
*/
protected function getQueueFromOnConnectionAttribute(ReflectionClass $reflectionClass)
protected function getQueueFromOnQueueAttribute(ReflectionClass $reflectionClass)
{
$onQueue = $reflectionClass->getAttributes(OnQueue::class);
if ($onQueue === []) {
return null;
}

return enum_value($onQueue[0]->newInstance()->queue);
return $onQueue[0]->newInstance()->queue;
}
}
7 changes: 4 additions & 3 deletions src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Symfony\Component\Mailer\Header\MetadataHeader;
use Symfony\Component\Mailer\Header\TagHeader;
use Symfony\Component\Mime\Address;
use function Illuminate\Support\enum_value;

class Mailable implements MailableContract, Renderable
{
Expand Down Expand Up @@ -470,10 +471,10 @@ protected function getQueue()
$queue = property_exists($this, 'queue') ? $this->queue : null;

if ($queue === null) {
$queue = $this->getQueueFromOnConnectionAttribute(new ReflectionClass($this));
$queue = $this->getQueueFromOnQueueAttribute(new ReflectionClass($this));
}

return $queue;
return enum_value($queue);
}

/**
Expand All @@ -489,7 +490,7 @@ protected function getConnection()
$connection = $this->getConnectionFromOnConnectionAttribute(new ReflectionClass($this));
}

return $connection;
return enum_value($connection);
}

/**
Expand Down
108 changes: 101 additions & 7 deletions tests/Support/SupportMailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
use Illuminate\Foundation\Queue\OnQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\SendQueuedMailable;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Queue;
use Orchestra\Testbench\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
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 @@ -26,7 +28,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 @@ -46,9 +48,8 @@ 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)
#[DataProvider('queueDataProvider')]
public function testQueuedMailableRespectsOnQueueAttribute(string $mailableClass, string $queueName)
{
Queue::fake();

Expand All @@ -61,17 +62,87 @@ public function testQueuedMailableRespectsStringOnQueueAttribute(string $mailabl
);
}

public function testQueueableMailableUsesQueueIfSetAsProperty()
#[DataProvider('queueDataProvider')]
public function testQueuedMailableDispatchedLaterRespectsOnQueueAttribute(string $mailableClass, string $queueName)
{
Queue::fake();

Mail::later(100, new $mailableClass);

Queue::assertPushedOn(
$queueName,
SendQueuedMailable::class,
fn ($job) => is_a($job->mailable, $mailableClass, true)
);
}

#[DataProvider('connectionDataProvider')]
public function testQueuedMailableRespectsOnConnectionAttribute(string $mailableClass, string $connectionName)
{
Queue::fake();

Queue::before(function (JobProcessing $jobProcessing) use ($connectionName) {
$this->assertEquals($connectionName, $jobProcessing->connectionName);
});

Mail::send(new $mailableClass());

}

#[DataProvider('connectionDataProvider')]
public function testLaterQueuedMailableRespectsOnConnectionAttribute(string $mailableClass, string $connectionName)
{
Queue::fake();

Queue::before(function (JobProcessing $jobProcessing) use ($connectionName) {
$this->assertEquals($connectionName, $jobProcessing->connectionName);
});

Mail::later(100, new $mailableClass());
}

public function testQueueableMailableUsesQueueAndConnectionFromClassProperties()
{
Queue::fake();
Mail::send(new TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet());

Queue::assertPushed(function(SendQueuedMailable $sendQueuedMailable) {
Queue::assertPushed(function (SendQueuedMailable $sendQueuedMailable) {
return $sendQueuedMailable->mailable instanceof TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet
&& $sendQueuedMailable->connection === 'my-connection'
&& $sendQueuedMailable->queue === 'some-other-queue';
});
}

public function testQueueableMailableDispatchedLaterUsesQueueAndConnectionFromClassProperties()
{
Queue::fake();
Mail::later(100, new TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet());

Queue::assertPushed(function (SendQueuedMailable $sendQueuedMailable) {
return $sendQueuedMailable->mailable instanceof TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet
&& $sendQueuedMailable->connection === 'my-connection'
&& $sendQueuedMailable->queue === 'some-other-queue';
});
}

/**
* @return array<string, array{class-string<Mailable>, string}>
*/
public static function queueDataProvider(): array
{
return [
'string for queue' => [TestMailWithOnQueue::class, 'my-queue'],
'enum for queue' => [TestMailWithEnumOnQueue::class, 'queue-from-enum'],
];
}

public static function connectionDataProvider(): array
{
return [
'string for connection' => [TestMailWithOnConnection::class, 'connection-string'],
'enum for connection' => [TestMailWithEnumOnConnection::class, 'connection-from-enum'],
];
}
}

class TestMail extends Mailable
Expand All @@ -97,6 +168,29 @@ class TestMailWithEnumOnQueue extends Mailable implements ShouldQueue
{
}

#[OnConnection('connection-string')]
class TestMailWithOnConnection extends Mailable implements ShouldQueue
{
public $queue = 'queue';

public function build()
{
return $this->view('view');
}
}

#[OnConnection(SupportMailTestEnum::Connection)]
class TestMailWithEnumOnConnection extends Mailable implements ShouldQueue
{
public $queue = 'queue';

public function build()
{
return $this->view('view');
}
}


#[OnQueue(SupportMailTestEnum::Queue)]
#[OnConnection(SupportMailTestEnum::Connection)]
class TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet extends Mailable implements ShouldQueue
Expand Down

0 comments on commit 60600fe

Please sign in to comment.