Skip to content

Commit

Permalink
[11.x] Accepts BackedEnum for onQueue, onConnection, allOnQueue, and …
Browse files Browse the repository at this point in the history
…allOnConnection methods in the Queueable trait (#52604)

* Accepts StringBackedEnum

* Tests

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
sethsandaru and taylorotwell authored Aug 31, 2024
1 parent d343e5b commit 811ea6a
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/Illuminate/Bus/Queueable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Bus;

use BackedEnum;
use Closure;
use Illuminate\Queue\CallQueuedClosure;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -76,53 +77,65 @@ trait Queueable
/**
* Set the desired connection for the job.
*
* @param string|null $connection
* @param \BackedEnum|string|null $connection
* @return $this
*/
public function onConnection($connection)
{
$this->connection = $connection;
$this->connection = $connection instanceof BackedEnum
? $connection->value
: $connection;

return $this;
}

/**
* Set the desired queue for the job.
*
* @param string|null $queue
* @param \BackedEnum|string|null $queue
* @return $this
*/
public function onQueue($queue)
{
$this->queue = $queue;
$this->queue = $queue instanceof BackedEnum
? $queue->value
: $queue;

return $this;
}

/**
* Set the desired connection for the chain.
*
* @param string|null $connection
* @param \BackedEnum|string|null $connection
* @return $this
*/
public function allOnConnection($connection)
{
$this->chainConnection = $connection;
$this->connection = $connection;
$resolvedConnection = $connection instanceof BackedEnum
? $connection->value
: $connection;

$this->chainConnection = $resolvedConnection;
$this->connection = $resolvedConnection;

return $this;
}

/**
* Set the desired queue for the chain.
*
* @param string|null $queue
* @param \BackedEnum|string|null $queue
* @return $this
*/
public function allOnQueue($queue)
{
$this->chainQueue = $queue;
$this->queue = $queue;
$resolvedQueue = $queue instanceof BackedEnum
? $queue->value
: $queue;

$this->chainQueue = $resolvedQueue;
$this->queue = $resolvedQueue;

return $this;
}
Expand Down
89 changes: 89 additions & 0 deletions tests/Bus/QueueableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Illuminate\Tests\Bus;

use Illuminate\Bus\Queueable;
use PHPUnit\Framework\TestCase;

class QueueableTest extends TestCase
{
public static function connectionDataProvider(): array
{
return [
'uses string' => ['redis', 'redis'],
'uses BackedEnum #1' => [ConnectionEnum::SQS, 'sqs'],
'uses BackedEnum #2' => [ConnectionEnum::REDIS, 'redis'],
'uses null' => [null, null],
];
}

/**
* @dataProvider connectionDataProvider
*/
public function testOnConnection(mixed $connection, ?string $expected): void
{
$job = new FakeJob();
$job->onConnection($connection);

$this->assertSame($job->connection, $expected);
}

/**
* @dataProvider connectionDataProvider
*/
public function testAllOnConnection(mixed $connection, ?string $expected): void
{
$job = new FakeJob();
$job->allOnConnection($connection);

$this->assertSame($job->connection, $expected);
$this->assertSame($job->chainConnection, $expected);
}

public static function queuesDataProvider(): array
{
return [
'uses string' => ['high', 'high',],
'uses BackedEnum #1' => [QueueEnum::DEFAULT, 'default'],
'uses BackedEnum #2' => [QueueEnum::HIGH, 'high'],
'uses null' => [null, null],
];
}

/**
* @dataProvider queuesDataProvider
*/
public function testOnQueue(mixed $queue, ?string $expected): void
{
$job = new FakeJob();
$job->onQueue($queue);

$this->assertSame($job->queue, $expected);
}

/**
* @dataProvider queuesDataProvider
*/
public function testAllOnQueue(mixed $queue, ?string $expected): void
{
$job = new FakeJob();
$job->allOnQueue($queue);

$this->assertSame($job->queue, $expected);
$this->assertSame($job->chainQueue, $expected);
}
}

class FakeJob {
use Queueable;
}

enum ConnectionEnum: string {
case SQS = 'sqs';
case REDIS = 'redis';
}

enum QueueEnum: string {
case HIGH = 'high';
case DEFAULT = 'default';
}

0 comments on commit 811ea6a

Please sign in to comment.