Skip to content

Commit

Permalink
[Messenger] Mention the transport which failed during the setup command
Browse files Browse the repository at this point in the history
The transport name can help to further investigate the underlying reasons of the failure
  • Loading branch information
thePanz committed Jan 27, 2023
1 parent f06554b commit 49c0af9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
*/
Expand Down

0 comments on commit 49c0af9

Please sign in to comment.