Skip to content

Commit bb7e7dd

Browse files
authored
[9.x] Passing event into viaQueue and viaConnection of Queued Listener (#44080)
* Queue dispatcher patch * Fixed CI style
1 parent 07ebd92 commit bb7e7dd

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

src/Illuminate/Events/Dispatcher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,11 +579,11 @@ protected function queueHandler($class, $method, $arguments)
579579
[$listener, $job] = $this->createListenerAndJob($class, $method, $arguments);
580580

581581
$connection = $this->resolveQueue()->connection(method_exists($listener, 'viaConnection')
582-
? $listener->viaConnection()
582+
? $listener->viaConnection($arguments[0])
583583
: $listener->connection ?? null);
584584

585585
$queue = method_exists($listener, 'viaQueue')
586-
? $listener->viaQueue()
586+
? $listener->viaQueue($arguments[0])
587587
: $listener->queue ?? null;
588588

589589
isset($listener->delay)

tests/Events/QueuedEventsTest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Contracts\Queue\ShouldQueue;
88
use Illuminate\Events\CallQueuedListener;
99
use Illuminate\Events\Dispatcher;
10+
use Illuminate\Queue\QueueManager;
1011
use Illuminate\Support\Testing\Fakes\QueueFake;
1112
use Mockery as m;
1213
use PHPUnit\Framework\TestCase;
@@ -84,6 +85,48 @@ public function testQueueIsSetByGetConnection()
8485
$d->dispatch('some.event', ['foo', 'bar']);
8586
}
8687

88+
public function testQueueIsSetByGetQueueDynamically()
89+
{
90+
$d = new Dispatcher;
91+
92+
$fakeQueue = new QueueFake(new Container);
93+
94+
$d->setQueueResolver(function () use ($fakeQueue) {
95+
return $fakeQueue;
96+
});
97+
98+
$d->listen('some.event', TestDispatcherGetQueueDynamically::class.'@handle');
99+
$d->dispatch('some.event', [['useHighPriorityQueue' => true], 'bar']);
100+
101+
$fakeQueue->assertPushedOn('p0', CallQueuedListener::class);
102+
}
103+
104+
public function testQueueIsSetByGetConnectionDynamically()
105+
{
106+
$d = new Dispatcher;
107+
$queueManager = $this->createMock(QueueManager::class);
108+
$queue = $this->createMock(Queue::class);
109+
110+
$queueManager->expects($this->once())
111+
->method('connection')
112+
->with('redis')
113+
->willReturn($queue);
114+
115+
$queue->expects($this->once())
116+
->method('pushOn')
117+
->with(null, $this->isInstanceOf(CallQueuedListener::class));
118+
119+
$d->setQueueResolver(function () use ($queueManager) {
120+
return $queueManager;
121+
});
122+
123+
$d->listen('some.event', TestDispatcherGetConnectionDynamically::class.'@handle');
124+
$d->dispatch('some.event', [
125+
['shouldUseRedisConnection' => true],
126+
'bar',
127+
]);
128+
}
129+
87130
public function testQueuePropagateRetryUntilAndMaxExceptions()
88131
{
89132
$d = new Dispatcher;
@@ -220,3 +263,39 @@ public function handle($job, $next)
220263
$next($job);
221264
}
222265
}
266+
267+
class TestDispatcherGetConnectionDynamically implements ShouldQueue
268+
{
269+
public function handle()
270+
{
271+
//
272+
}
273+
274+
public function viaConnection($event)
275+
{
276+
if ($event['shouldUseRedisConnection']) {
277+
return 'redis';
278+
}
279+
280+
return 'sqs';
281+
}
282+
}
283+
284+
class TestDispatcherGetQueueDynamically implements ShouldQueue
285+
{
286+
public $queue = 'my_queue';
287+
288+
public function handle()
289+
{
290+
//
291+
}
292+
293+
public function viaQueue($event)
294+
{
295+
if ($event['useHighPriorityQueue']) {
296+
return 'p0';
297+
}
298+
299+
return 'p99';
300+
}
301+
}

0 commit comments

Comments
 (0)