[Messenger] Don't prevent dispatch out of another bus

This commit is contained in:
Maxime Steinhausser 2020-08-28 12:11:33 +02:00 committed by Fabien Potencier
parent 9cd3e674e6
commit 1e8ae43372
3 changed files with 29 additions and 5 deletions

View File

@ -6,6 +6,7 @@ CHANGELOG
* Added `FlattenExceptionNormalizer` to give more information about the exception on Messenger background processes. The `FlattenExceptionNormalizer` has a higher priority than `ProblemNormalizer` and it is only used when the Messenger serialization context is set.
* Added factory methods to `DelayStamp`.
* Removed the exception when dispatching a message with a `DispatchAfterCurrentBusStamp` and not in a context of another dispatch call
5.1.0
-----

View File

@ -44,12 +44,13 @@ class DispatchAfterCurrentBusMiddleware implements MiddlewareInterface
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
if (null !== $envelope->last(DispatchAfterCurrentBusStamp::class)) {
if (!$this->isRootDispatchCallRunning) {
throw new \LogicException(sprintf('You can only use a "%s" stamp in the context of a message handler.', DispatchAfterCurrentBusStamp::class));
}
$this->queue[] = new QueuedEnvelope($envelope, $stack);
if ($this->isRootDispatchCallRunning) {
$this->queue[] = new QueuedEnvelope($envelope, $stack);
return $envelope;
return $envelope;
}
$envelope = $envelope->withoutAll(DispatchAfterCurrentBusStamp::class);
}
if ($this->isRootDispatchCallRunning) {

View File

@ -256,6 +256,28 @@ class DispatchAfterCurrentBusMiddlewareTest extends TestCase
$messageBus->dispatch($message);
}
public function testDispatchOutOfAnotherHandlerDispatchesAndRemoveStamp()
{
$event = new DummyEvent('First event');
$middleware = new DispatchAfterCurrentBusMiddleware();
$handlingMiddleware = $this->createMock(MiddlewareInterface::class);
$handlingMiddleware
->method('handle')
->with($this->expectHandledMessage($event))
->will($this->willHandleMessage());
$eventBus = new MessageBus([
$middleware,
$handlingMiddleware,
]);
$enveloppe = $eventBus->dispatch($event, [new DispatchAfterCurrentBusStamp()]);
self::assertNull($enveloppe->last(DispatchAfterCurrentBusStamp::class));
}
private function expectHandledMessage($message): Callback
{
return $this->callback(function (Envelope $envelope) use ($message) {