Skip to content

Commit 4a0e112

Browse files
dirk39fabpot
authored andcommitted
[Messenger] RoutableMessageBus route to default bus
1 parent 36273d4 commit 4a0e112

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

RoutableMessageBus.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,12 @@ 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.');
50-
}
48+
$busName = null !== $busNameStamp ? $busNameStamp->getBusName() : MessageBusInterface::class;
5149

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

56-
return $this->busLocator->get($busNameStamp->getBusName())->dispatch($envelope, $stamps);
54+
return $this->busLocator->get($busName)->dispatch($envelope, $stamps);
5755
}
5856
}

Tests/RoutableMessageBusTest.php

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

44-
public function testItExceptionOnMissingStamp()
44+
public function testItRoutesToDefaultBus()
45+
{
46+
$envelope = new Envelope(new \stdClass());
47+
$stamp = new DelayStamp(5);
48+
$defaultBus = $this->createMock(MessageBusInterface::class);
49+
$defaultBus->expects($this->once())->method('dispatch')->with($envelope, [$stamp])
50+
->willReturn($envelope);
51+
52+
$container = $this->createMock(ContainerInterface::class);
53+
$container->expects($this->once())->method('has')->with(MessageBusInterface::class)
54+
->willReturn(true);
55+
$container->expects($this->once())->method('get')->with(MessageBusInterface::class)
56+
->willReturn($defaultBus);
57+
58+
$routableBus = new RoutableMessageBus($container);
59+
60+
$this->assertSame($envelope, $routableBus->dispatch($envelope, [$stamp]));
61+
}
62+
63+
public function testItExceptionOnDefaultBusNotFound()
4564
{
4665
$this->expectException(InvalidArgumentException::class);
47-
$this->expectExceptionMessage('does not contain a BusNameStamp');
66+
$this->expectExceptionMessage(sprintf('Bus name "%s" does not exists.', MessageBusInterface::class));
4867

4968
$envelope = new Envelope(new \stdClass());
5069

5170
$container = $this->createMock(ContainerInterface::class);
52-
$container->expects($this->never())->method('has');
71+
$container->expects($this->once())->method('has')->with(MessageBusInterface::class)
72+
->willReturn(false);
5373

5474
$routableBus = new RoutableMessageBus($container);
5575
$routableBus->dispatch($envelope);
@@ -58,7 +78,7 @@ public function testItExceptionOnMissingStamp()
5878
public function testItExceptionOnBusNotFound()
5979
{
6080
$this->expectException(InvalidArgumentException::class);
61-
$this->expectExceptionMessage('Invalid bus name');
81+
$this->expectExceptionMessage(sprintf('Bus name "%s" does not exists.', 'foo_bus'));
6282

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

0 commit comments

Comments
 (0)