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

Skip to content

Commit a3e17df

Browse files
committed
[Messenger] fallback to default bus if BusNameStamp missing from Envelope
1 parent a7d12ee commit a3e17df

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/Symfony/Component/Messenger/RoutableMessageBus.php

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

4646
/** @var BusNameStamp $busNameStamp */
4747
$busNameStamp = $envelope->last(BusNameStamp::class);
48-
if (null === $busNameStamp) {
49-
throw new InvalidArgumentException('Envelope does not contain a BusNameStamp.');
48+
$busNameStampName = MessageBusInterface::class;
49+
if (null !== $busNameStamp) {
50+
$busNameStampName = $busNameStamp->getBusName();
5051
}
5152

52-
if (!$this->busLocator->has($busNameStamp->getBusName())) {
53-
throw new InvalidArgumentException(sprintf('Invalid bus name "%s" on BusNameStamp.', $busNameStamp->getBusName()));
53+
if (!$this->busLocator->has($busNameStampName)) {
54+
throw new InvalidArgumentException(sprintf('Invalid bus name "%s" on BusNameStamp.', $busNameStampName));
5455
}
5556

56-
return $this->busLocator->get($busNameStamp->getBusName())->dispatch($envelope, $stamps);
57+
return $this->busLocator->get($busNameStampName)->dispatch($envelope, $stamps);
5758
}
5859
}

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,25 @@ public function testItRoutesToTheCorrectBus()
4141
$this->assertSame($envelope, $routableBus->dispatch($envelope, [$stamp]));
4242
}
4343

44-
public function testItExceptionOnMissingStamp()
45-
{
46-
$this->expectException(InvalidArgumentException::class);
47-
$this->expectExceptionMessage('does not contain a BusNameStamp');
4844

45+
46+
public function testItRoutesToDefaultBus()
47+
{
4948
$envelope = new Envelope(new \stdClass());
49+
$stamp = new DelayStamp(5);
50+
$defaultBus = $this->createMock(MessageBusInterface::class);
51+
$defaultBus->expects($this->once())->method('dispatch')->with($envelope, [$stamp])
52+
->willReturn($envelope);
5053

5154
$container = $this->createMock(ContainerInterface::class);
52-
$container->expects($this->never())->method('has');
55+
$container->expects($this->once())->method('has')->with(MessageBusInterface::class)
56+
->willReturn(true);
57+
$container->expects($this->once())->method('get')->with(MessageBusInterface::class)
58+
->willReturn($defaultBus);
5359

5460
$routableBus = new RoutableMessageBus($container);
55-
$routableBus->dispatch($envelope);
61+
62+
$this->assertSame($envelope, $routableBus->dispatch($envelope, [$stamp]));
5663
}
5764

5865
public function testItExceptionOnBusNotFound()

0 commit comments

Comments
 (0)