Skip to content

Commit 23e3d51

Browse files
committed
bug #31472 [Messenger] Fix routable message bus default bus (weaverryan)
This PR was merged into the 4.3 branch. Discussion ---------- [Messenger] Fix routable message bus default bus | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | none | License | MIT | Doc PR | not needed In #31288, we gave the `RoutableMessageBus` a "default" bus. We did that by using the `MessageBusInterface` service in the locator. But, no such service exists - I think that was just a huge oversight (and maybe @dirk39 named a bus this in the project he was testing on?). The services in the locator are very simply the keys under `framework.messenger.buses` or the default, which is a single `messenger.bus.default` id. There is an alias in the container for `MessageBusInterface`, but this is not added to the locator (and adding it would be a bit awkward, as `MessengerPass` is in the component and the interface alias is entirely a framework thing). Cheers! Commits ------- 42e0536d8f Changing how RoutableMessageBus fallback bus works
2 parents a0d064c + e300919 commit 23e3d51

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

RoutableMessageBus.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
class RoutableMessageBus implements MessageBusInterface
2929
{
3030
private $busLocator;
31+
private $fallbackBus;
3132

3233
/**
3334
* @param ContainerInterface $busLocator A locator full of MessageBusInterface objects
3435
*/
35-
public function __construct(ContainerInterface $busLocator)
36+
public function __construct(ContainerInterface $busLocator, MessageBusInterface $fallbackBus = null)
3637
{
3738
$this->busLocator = $busLocator;
39+
$this->fallbackBus = $fallbackBus;
3840
}
3941

4042
public function dispatch($envelope, array $stamps = []): Envelope
@@ -43,14 +45,28 @@ public function dispatch($envelope, array $stamps = []): Envelope
4345
throw new InvalidArgumentException('Messages passed to RoutableMessageBus::dispatch() must be inside an Envelope');
4446
}
4547

46-
/** @var BusNameStamp $busNameStamp */
48+
return $this->getMessageBus($envelope)->dispatch($envelope, $stamps);
49+
}
50+
51+
private function getMessageBus(Envelope $envelope): MessageBusInterface
52+
{
53+
/** @var BusNameStamp|null $busNameStamp */
4754
$busNameStamp = $envelope->last(BusNameStamp::class);
48-
$busName = null !== $busNameStamp ? $busNameStamp->getBusName() : MessageBusInterface::class;
55+
56+
if (null === $busNameStamp) {
57+
if (null === $this->fallbackBus) {
58+
throw new InvalidArgumentException(sprintf('Envelope is missing a BusNameStamp and no fallback message bus is configured on RoutableMessageBus.'));
59+
}
60+
61+
return $this->fallbackBus;
62+
}
63+
64+
$busName = $busNameStamp->getBusName();
4965

5066
if (!$this->busLocator->has($busName)) {
5167
throw new InvalidArgumentException(sprintf('Bus named "%s" does not exist.', $busName));
5268
}
5369

54-
return $this->busLocator->get($busName)->dispatch($envelope, $stamps);
70+
return $this->busLocator->get($busName);
5571
}
5672
}

Tests/RoutableMessageBusTest.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,41 +50,34 @@ public function testItRoutesToDefaultBus()
5050
->willReturn($envelope);
5151

5252
$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);
5753

58-
$routableBus = new RoutableMessageBus($container);
54+
$routableBus = new RoutableMessageBus($container, $defaultBus);
5955

6056
$this->assertSame($envelope, $routableBus->dispatch($envelope, [$stamp]));
6157
}
6258

63-
public function testItExceptionOnDefaultBusNotFound()
59+
public function testItExceptionOnBusNotFound()
6460
{
6561
$this->expectException(InvalidArgumentException::class);
66-
$this->expectExceptionMessage(sprintf('Bus named "%s" does not exist.', MessageBusInterface::class));
62+
$this->expectExceptionMessage('Bus named "my_cool_bus" does not exist.');
6763

68-
$envelope = new Envelope(new \stdClass());
64+
$envelope = new Envelope(new \stdClass(), [
65+
new BusNameStamp('my_cool_bus'),
66+
]);
6967

7068
$container = $this->createMock(ContainerInterface::class);
71-
$container->expects($this->once())->method('has')->with(MessageBusInterface::class)
72-
->willReturn(false);
73-
7469
$routableBus = new RoutableMessageBus($container);
7570
$routableBus->dispatch($envelope);
7671
}
7772

78-
public function testItExceptionOnBusNotFound()
73+
public function testItExceptionOnDefaultBusNotFound()
7974
{
8075
$this->expectException(InvalidArgumentException::class);
81-
$this->expectExceptionMessage(sprintf('Bus named "%s" does not exist.', 'foo_bus'));
76+
$this->expectExceptionMessage('Envelope is missing a BusNameStamp and no fallback message bus is configured on RoutableMessageBus.');
8277

83-
$envelope = new Envelope(new \stdClass(), [new BusNameStamp('foo_bus')]);
78+
$envelope = new Envelope(new \stdClass());
8479

8580
$container = $this->createMock(ContainerInterface::class);
86-
$container->expects($this->once())->method('has')->willReturn(false);
87-
8881
$routableBus = new RoutableMessageBus($container);
8982
$routableBus->dispatch($envelope);
9083
}

0 commit comments

Comments
 (0)