Skip to content

[Messenger] Fix routable message bus default bus #31472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<!-- routable message bus -->
<service id="messenger.routable_message_bus" class="Symfony\Component\Messenger\RoutableMessageBus">
<argument /> <!-- Message bus locator -->
<argument type="service" id="messenger.default_bus" />
</service>
</services>
</container>
24 changes: 20 additions & 4 deletions src/Symfony/Component/Messenger/RoutableMessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
class RoutableMessageBus implements MessageBusInterface
{
private $busLocator;
private $fallbackBus;

/**
* @param ContainerInterface $busLocator A locator full of MessageBusInterface objects
*/
public function __construct(ContainerInterface $busLocator)
public function __construct(ContainerInterface $busLocator, MessageBusInterface $fallbackBus = null)
{
$this->busLocator = $busLocator;
$this->fallbackBus = $fallbackBus;
}

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

/** @var BusNameStamp $busNameStamp */
return $this->getMessageBus($envelope)->dispatch($envelope, $stamps);
}

private function getMessageBus(Envelope $envelope): MessageBusInterface
{
/** @var BusNameStamp|null $busNameStamp */
$busNameStamp = $envelope->last(BusNameStamp::class);
$busName = null !== $busNameStamp ? $busNameStamp->getBusName() : MessageBusInterface::class;

if (null === $busNameStamp) {
if (null === $this->fallbackBus) {
throw new InvalidArgumentException(sprintf('Envelope is missing a BusNameStamp and no fallback message bus is configured on RoutableMessageBus.'));
}

return $this->fallbackBus;
}

$busName = $busNameStamp->getBusName();

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

return $this->busLocator->get($busName)->dispatch($envelope, $stamps);
return $this->busLocator->get($busName);
}
}
25 changes: 9 additions & 16 deletions src/Symfony/Component/Messenger/Tests/RoutableMessageBusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,41 +50,34 @@ public function testItRoutesToDefaultBus()
->willReturn($envelope);

$container = $this->createMock(ContainerInterface::class);
$container->expects($this->once())->method('has')->with(MessageBusInterface::class)
->willReturn(true);
$container->expects($this->once())->method('get')->with(MessageBusInterface::class)
->willReturn($defaultBus);

$routableBus = new RoutableMessageBus($container);
$routableBus = new RoutableMessageBus($container, $defaultBus);

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

public function testItExceptionOnDefaultBusNotFound()
public function testItExceptionOnBusNotFound()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf('Bus named "%s" does not exist.', MessageBusInterface::class));
$this->expectExceptionMessage('Bus named "my_cool_bus" does not exist.');

$envelope = new Envelope(new \stdClass());
$envelope = new Envelope(new \stdClass(), [
new BusNameStamp('my_cool_bus'),
]);

$container = $this->createMock(ContainerInterface::class);
$container->expects($this->once())->method('has')->with(MessageBusInterface::class)
->willReturn(false);

$routableBus = new RoutableMessageBus($container);
$routableBus->dispatch($envelope);
}

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

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

$container = $this->createMock(ContainerInterface::class);
$container->expects($this->once())->method('has')->willReturn(false);

$routableBus = new RoutableMessageBus($container);
$routableBus->dispatch($envelope);
}
Expand Down