From 49c0af915de37347635b7a7718156c2afe27ef8d Mon Sep 17 00:00:00 2001 From: thePanz Date: Fri, 20 Jan 2023 15:00:31 +0100 Subject: [PATCH] [Messenger] Mention the transport which failed during the setup command The transport name can help to further investigate the underlying reasons of the failure --- .../Command/SetupTransportsCommand.php | 11 +++++-- .../Command/SetupTransportsCommandTest.php | 33 +++++++++++++++++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Messenger/Command/SetupTransportsCommand.php b/src/Symfony/Component/Messenger/Command/SetupTransportsCommand.php index 98dcfd9e9936a..d535cc9c429a2 100644 --- a/src/Symfony/Component/Messenger/Command/SetupTransportsCommand.php +++ b/src/Symfony/Component/Messenger/Command/SetupTransportsCommand.php @@ -71,11 +71,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int foreach ($transportNames as $id => $transportName) { $transport = $this->transportLocator->get($transportName); - if ($transport instanceof SetupableTransportInterface) { + if (!$transport instanceof SetupableTransportInterface) { + $io->note(sprintf('The "%s" transport does not support setup.', $transportName)); + continue; + } + + try { $transport->setup(); $io->success(sprintf('The "%s" transport was set up successfully.', $transportName)); - } else { - $io->note(sprintf('The "%s" transport does not support setup.', $transportName)); + } catch (\Exception $e) { + throw new \RuntimeException(sprintf('An error occurred while setting up the "%s" transport: ', $transportName).$e->getMessage(), 0, $e); } } diff --git a/src/Symfony/Component/Messenger/Tests/Command/SetupTransportsCommandTest.php b/src/Symfony/Component/Messenger/Tests/Command/SetupTransportsCommandTest.php index cfe9eca20b40d..a260d41cb7045 100644 --- a/src/Symfony/Component/Messenger/Tests/Command/SetupTransportsCommandTest.php +++ b/src/Symfony/Component/Messenger/Tests/Command/SetupTransportsCommandTest.php @@ -72,8 +72,6 @@ public function testReceiverNameArgument() public function testReceiverNameArgumentNotFound() { - $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage('The "not_found" transport does not exist.'); // mock a service locator /** @var MockObject&ServiceLocator $serviceLocator */ $serviceLocator = $this->createMock(ServiceLocator::class); @@ -86,9 +84,40 @@ public function testReceiverNameArgumentNotFound() $command = new SetupTransportsCommand($serviceLocator, ['amqp', 'other_transport']); $tester = new CommandTester($command); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('The "not_found" transport does not exist.'); $tester->execute(['transport' => 'not_found']); } + public function testThrowsExceptionOnTransportSetup() + { + // mock a setupable-transport, that throws + $amqpTransport = $this->createMock(SetupableTransportInterface::class); + $amqpTransport->expects($this->exactly(1)) + ->method('setup') + ->willThrowException(new \RuntimeException('Server not found')); + + // mock a service locator + /** @var MockObject&ServiceLocator $serviceLocator */ + $serviceLocator = $this->createMock(ServiceLocator::class); + $serviceLocator->expects($this->exactly(1)) + ->method('get') + ->will($this->onConsecutiveCalls( + $amqpTransport + )); + $serviceLocator + ->method('has') + ->willReturn(true); + + $command = new SetupTransportsCommand($serviceLocator, ['amqp']); + $tester = new CommandTester($command); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('An error occurred while setting up the "amqp" transport: Server not found'); + $tester->execute(['transport' => 'amqp']); + } + /** * @dataProvider provideCompletionSuggestions */