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

Skip to content

[Messenger] RoutableMessageBus route to default bus #31288

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 1, 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
10 changes: 4 additions & 6 deletions src/Symfony/Component/Messenger/RoutableMessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,12 @@ public function dispatch($envelope, array $stamps = []): Envelope

/** @var BusNameStamp $busNameStamp */
$busNameStamp = $envelope->last(BusNameStamp::class);
if (null === $busNameStamp) {
throw new InvalidArgumentException('Envelope does not contain a BusNameStamp.');
}
$busName = null !== $busNameStamp ? $busNameStamp->getBusName() : MessageBusInterface::class;

if (!$this->busLocator->has($busNameStamp->getBusName())) {
throw new InvalidArgumentException(sprintf('Invalid bus name "%s" on BusNameStamp.', $busNameStamp->getBusName()));
if (!$this->busLocator->has($busName)) {
throw new InvalidArgumentException(sprintf('Bus name "%s" does not exists.', $busName));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new InvalidArgumentException(sprintf('Bus name "%s" does not exists.', $busName));
throw new InvalidArgumentException(sprintf('Bus named "%s" does not exist.', $busName));

}

return $this->busLocator->get($busNameStamp->getBusName())->dispatch($envelope, $stamps);
return $this->busLocator->get($busName)->dispatch($envelope, $stamps);
}
}
28 changes: 24 additions & 4 deletions src/Symfony/Component/Messenger/Tests/RoutableMessageBusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,35 @@ public function testItRoutesToTheCorrectBus()
$this->assertSame($envelope, $routableBus->dispatch($envelope, [$stamp]));
}

public function testItExceptionOnMissingStamp()
public function testItRoutesToDefaultBus()
{
$envelope = new Envelope(new \stdClass());
$stamp = new DelayStamp(5);
$defaultBus = $this->createMock(MessageBusInterface::class);
$defaultBus->expects($this->once())->method('dispatch')->with($envelope, [$stamp])
->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);

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

public function testItExceptionOnDefaultBusNotFound()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('does not contain a BusNameStamp');
$this->expectExceptionMessage(sprintf('Bus name "%s" does not exists.', MessageBusInterface::class));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move these to annotations instead actually? It's more readable.


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

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

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

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

Expand Down