diff --git a/src/queue/src/Queue.php b/src/queue/src/Queue.php index f53de7df6..859084cb9 100644 --- a/src/queue/src/Queue.php +++ b/src/queue/src/Queue.php @@ -372,8 +372,10 @@ public function getContainer(): ContainerInterface /** * Set the IoC container instance. */ - public function setContainer(ContainerInterface $container): void + public function setContainer(ContainerInterface $container): static { $this->container = $container; + + return $this; } } diff --git a/src/queue/src/QueueManager.php b/src/queue/src/QueueManager.php index 5e44ecb67..0a07821c6 100644 --- a/src/queue/src/QueueManager.php +++ b/src/queue/src/QueueManager.php @@ -139,14 +139,11 @@ public function connection(?string $name = null): Queue // If the connection has not been resolved yet we will resolve it now as all // of the connections are resolved when they are actually needed so we do // not make any unnecessary connection to the various queue end-points. - if (! isset($this->connections[$name])) { - $this->connections[$name] = $this->resolve($name); - - /* @phpstan-ignore-next-line */ - $this->connections[$name]->setContainer($this->app); + if ($queue = $this->connections[$name] ?? null) { + return $queue; } - return $this->connections[$name]; + return $this->connections[$name] = $this->resolve($name); } /** @@ -162,9 +159,11 @@ protected function resolve(string $name): Queue throw new InvalidArgumentException("The [{$name}] queue connection has not been configured."); } + /** @phpstan-ignore-next-line */ $resolver = fn () => $this->getConnector($config['driver']) ->connect($config) - ->setConnectionName($name); + ->setConnectionName($name) + ->setContainer($this->app); if (in_array($config['driver'], $this->poolables)) { return $this->createPoolProxy( diff --git a/tests/Queue/QueueManagerTest.php b/tests/Queue/QueueManagerTest.php index 3cbf2f754..1811a1ef7 100644 --- a/tests/Queue/QueueManagerTest.php +++ b/tests/Queue/QueueManagerTest.php @@ -41,12 +41,12 @@ public function testDefaultConnectionCanBeResolved() $connector = m::mock(ConnectorInterface::class); $queue = m::mock(Queue::class); $queue->shouldReceive('setConnectionName')->once()->with('sync')->andReturnSelf(); + $queue->shouldReceive('setContainer')->once()->with($container)->andReturnSelf(); $connector->shouldReceive('connect')->once()->with(['driver' => 'sync'])->andReturn($queue); $manager->addConnector('sync', function () use ($connector) { return $connector; }); - $queue->shouldReceive('setContainer')->once()->with($container); $this->assertSame($queue, $manager->connection('sync')); } @@ -65,7 +65,7 @@ public function testOtherConnectionCanBeResolved() $manager->addConnector('bar', function () use ($connector) { return $connector; }); - $queue->shouldReceive('setContainer')->once()->with($container); + $queue->shouldReceive('setContainer')->once()->with($container)->andReturnSelf(); $this->assertSame($queue, $manager->connection('foo')); } @@ -84,7 +84,7 @@ public function testNullConnectionCanBeResolved() $manager->addConnector('null', function () use ($connector) { return $connector; }); - $queue->shouldReceive('setContainer')->once()->with($container); + $queue->shouldReceive('setContainer')->once()->with($container)->andReturnSelf(); $this->assertSame($queue, $manager->connection('null')); } @@ -98,14 +98,10 @@ public function testAddPoolableConnector() $manager = new QueueManager($container); $connector = m::mock(ConnectorInterface::class); - $queue = m::mock(Queue::class); - $queue->shouldReceive('setConnectionName')->once()->with('foo')->andReturnSelf(); - $connector->shouldReceive('connect')->once()->with(['driver' => 'bar'])->andReturn($queue); $manager->addConnector('bar', function () use ($connector) { return $connector; }); $manager->addPoolable('bar'); - $queue->shouldReceive('setContainer')->once()->with($container); $this->assertInstanceOf(QueuePoolProxy::class, $manager->connection('foo')); }