diff --git a/src/Illuminate/Events/Dispatcher.php b/src/Illuminate/Events/Dispatcher.php index ca8d86f78732..444401964ca4 100755 --- a/src/Illuminate/Events/Dispatcher.php +++ b/src/Illuminate/Events/Dispatcher.php @@ -549,9 +549,11 @@ protected function queueHandler($class, $method, $arguments) { [$listener, $job] = $this->createListenerAndJob($class, $method, $arguments); - $connection = $this->resolveQueue()->connection( - $listener->connection ?? null - ); + $connectionName = method_exists($listener, 'viaConnection') + ? $listener->viaConnection() + : $listener->connection ?? null; + + $connection = $this->resolveQueue()->connection($connectionName); $queue = method_exists($listener, 'viaQueue') ? $listener->viaQueue() diff --git a/tests/Events/QueuedEventsTest.php b/tests/Events/QueuedEventsTest.php index c34f7cb006d9..1a6da8ef98c4 100644 --- a/tests/Events/QueuedEventsTest.php +++ b/tests/Events/QueuedEventsTest.php @@ -67,6 +67,23 @@ public function testQueueIsSetByGetQueue() $fakeQueue->assertPushedOn('some_other_queue', CallQueuedListener::class); } + public function testQueueIsSetByGetConnection() + { + $d = new Dispatcher; + $queue = m::mock(Queue::class); + + $queue->shouldReceive('connection')->once()->with('some_other_connection')->andReturnSelf(); + + $queue->shouldReceive('pushOn')->once()->with(null, m::type(CallQueuedListener::class)); + + $d->setQueueResolver(function () use ($queue) { + return $queue; + }); + + $d->listen('some.event', TestDispatcherGetConnection::class.'@handle'); + $d->dispatch('some.event', ['foo', 'bar']); + } + public function testQueuePropagateRetryUntilAndMaxExceptions() { $d = new Dispatcher; @@ -123,6 +140,21 @@ public function viaQueue() } } +class TestDispatcherGetConnection implements ShouldQueue +{ + public $connection = 'my_connection'; + + public function handle() + { + // + } + + public function viaConnection() + { + return 'some_other_connection'; + } +} + class TestDispatcherOptions implements ShouldQueue { public $maxExceptions = 1;