Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 505cc53

Browse files
committed
check throw exception if default bus missing and rewrite error message
1 parent 8735909 commit 505cc53

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/Symfony/Component/Messenger/RoutableMessageBus.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,12 @@ public function dispatch($envelope, array $stamps = []): Envelope
4545

4646
/** @var BusNameStamp $busNameStamp */
4747
$busNameStamp = $envelope->last(BusNameStamp::class);
48-
$busNameStampName = MessageBusInterface::class;
49-
if (null !== $busNameStamp) {
50-
$busNameStampName = $busNameStamp->getBusName();
51-
}
48+
$busName = null !== $busNameStamp? $busNameStamp->getBusName(): MessageBusInterface::class;
5249

53-
if (!$this->busLocator->has($busNameStampName)) {
54-
throw new InvalidArgumentException(sprintf('Invalid bus name "%s" on BusNameStamp.', $busNameStampName));
50+
if (!$this->busLocator->has($busName)) {
51+
throw new InvalidArgumentException(sprintf('Bus name "%s" does not exists.', $busName));
5552
}
5653

57-
return $this->busLocator->get($busNameStampName)->dispatch($envelope, $stamps);
54+
return $this->busLocator->get($busName)->dispatch($envelope, $stamps);
5855
}
5956
}

src/Symfony/Component/Messenger/Tests/RoutableMessageBusTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,25 @@ public function testItRoutesToDefaultBus()
6060
$this->assertSame($envelope, $routableBus->dispatch($envelope, [$stamp]));
6161
}
6262

63+
public function testItExceptionOnDefaultBusNotFound()
64+
{
65+
$this->expectException(InvalidArgumentException::class);
66+
$this->expectExceptionMessage(sprintf('Bus name "%s" does not exists.', MessageBusInterface::class));
67+
68+
$envelope = new Envelope(new \stdClass());
69+
70+
$container = $this->createMock(ContainerInterface::class);
71+
$container->expects($this->once())->method('has')->with(MessageBusInterface::class)
72+
->willReturn(false);
73+
74+
$routableBus = new RoutableMessageBus($container);
75+
$routableBus->dispatch($envelope);
76+
}
77+
6378
public function testItExceptionOnBusNotFound()
6479
{
6580
$this->expectException(InvalidArgumentException::class);
66-
$this->expectExceptionMessage('Invalid bus name');
81+
$this->expectExceptionMessage(sprintf('Bus name "%s" does not exists.', 'foo_bus'));
6782

6883
$envelope = new Envelope(new \stdClass(), [new BusNameStamp('foo_bus')]);
6984

0 commit comments

Comments
 (0)