Skip to content

Commit 811ea6a

Browse files
[11.x] Accepts BackedEnum for onQueue, onConnection, allOnQueue, and allOnConnection methods in the Queueable trait (#52604)
* Accepts StringBackedEnum * Tests * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent d343e5b commit 811ea6a

File tree

2 files changed

+112
-10
lines changed

2 files changed

+112
-10
lines changed

src/Illuminate/Bus/Queueable.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Bus;
44

5+
use BackedEnum;
56
use Closure;
67
use Illuminate\Queue\CallQueuedClosure;
78
use Illuminate\Support\Arr;
@@ -76,53 +77,65 @@ trait Queueable
7677
/**
7778
* Set the desired connection for the job.
7879
*
79-
* @param string|null $connection
80+
* @param \BackedEnum|string|null $connection
8081
* @return $this
8182
*/
8283
public function onConnection($connection)
8384
{
84-
$this->connection = $connection;
85+
$this->connection = $connection instanceof BackedEnum
86+
? $connection->value
87+
: $connection;
8588

8689
return $this;
8790
}
8891

8992
/**
9093
* Set the desired queue for the job.
9194
*
92-
* @param string|null $queue
95+
* @param \BackedEnum|string|null $queue
9396
* @return $this
9497
*/
9598
public function onQueue($queue)
9699
{
97-
$this->queue = $queue;
100+
$this->queue = $queue instanceof BackedEnum
101+
? $queue->value
102+
: $queue;
98103

99104
return $this;
100105
}
101106

102107
/**
103108
* Set the desired connection for the chain.
104109
*
105-
* @param string|null $connection
110+
* @param \BackedEnum|string|null $connection
106111
* @return $this
107112
*/
108113
public function allOnConnection($connection)
109114
{
110-
$this->chainConnection = $connection;
111-
$this->connection = $connection;
115+
$resolvedConnection = $connection instanceof BackedEnum
116+
? $connection->value
117+
: $connection;
118+
119+
$this->chainConnection = $resolvedConnection;
120+
$this->connection = $resolvedConnection;
112121

113122
return $this;
114123
}
115124

116125
/**
117126
* Set the desired queue for the chain.
118127
*
119-
* @param string|null $queue
128+
* @param \BackedEnum|string|null $queue
120129
* @return $this
121130
*/
122131
public function allOnQueue($queue)
123132
{
124-
$this->chainQueue = $queue;
125-
$this->queue = $queue;
133+
$resolvedQueue = $queue instanceof BackedEnum
134+
? $queue->value
135+
: $queue;
136+
137+
$this->chainQueue = $resolvedQueue;
138+
$this->queue = $resolvedQueue;
126139

127140
return $this;
128141
}

tests/Bus/QueueableTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Bus;
4+
5+
use Illuminate\Bus\Queueable;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class QueueableTest extends TestCase
9+
{
10+
public static function connectionDataProvider(): array
11+
{
12+
return [
13+
'uses string' => ['redis', 'redis'],
14+
'uses BackedEnum #1' => [ConnectionEnum::SQS, 'sqs'],
15+
'uses BackedEnum #2' => [ConnectionEnum::REDIS, 'redis'],
16+
'uses null' => [null, null],
17+
];
18+
}
19+
20+
/**
21+
* @dataProvider connectionDataProvider
22+
*/
23+
public function testOnConnection(mixed $connection, ?string $expected): void
24+
{
25+
$job = new FakeJob();
26+
$job->onConnection($connection);
27+
28+
$this->assertSame($job->connection, $expected);
29+
}
30+
31+
/**
32+
* @dataProvider connectionDataProvider
33+
*/
34+
public function testAllOnConnection(mixed $connection, ?string $expected): void
35+
{
36+
$job = new FakeJob();
37+
$job->allOnConnection($connection);
38+
39+
$this->assertSame($job->connection, $expected);
40+
$this->assertSame($job->chainConnection, $expected);
41+
}
42+
43+
public static function queuesDataProvider(): array
44+
{
45+
return [
46+
'uses string' => ['high', 'high',],
47+
'uses BackedEnum #1' => [QueueEnum::DEFAULT, 'default'],
48+
'uses BackedEnum #2' => [QueueEnum::HIGH, 'high'],
49+
'uses null' => [null, null],
50+
];
51+
}
52+
53+
/**
54+
* @dataProvider queuesDataProvider
55+
*/
56+
public function testOnQueue(mixed $queue, ?string $expected): void
57+
{
58+
$job = new FakeJob();
59+
$job->onQueue($queue);
60+
61+
$this->assertSame($job->queue, $expected);
62+
}
63+
64+
/**
65+
* @dataProvider queuesDataProvider
66+
*/
67+
public function testAllOnQueue(mixed $queue, ?string $expected): void
68+
{
69+
$job = new FakeJob();
70+
$job->allOnQueue($queue);
71+
72+
$this->assertSame($job->queue, $expected);
73+
$this->assertSame($job->chainQueue, $expected);
74+
}
75+
}
76+
77+
class FakeJob {
78+
use Queueable;
79+
}
80+
81+
enum ConnectionEnum: string {
82+
case SQS = 'sqs';
83+
case REDIS = 'redis';
84+
}
85+
86+
enum QueueEnum: string {
87+
case HIGH = 'high';
88+
case DEFAULT = 'default';
89+
}

0 commit comments

Comments
 (0)