Skip to content
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

Finalize trigger enables propagation if it was stopped #184

Merged
merged 5 commits into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/QueryBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function dispatch($query): PromiseInterface
}
} catch (\Throwable $exception) {
$actionEvent->setParam(self::EVENT_PARAM_EXCEPTION, $exception);
//$actionEvent->stopPropagation(false);
$actionEvent->stopPropagation(false);
} finally {
$this->triggerFinalize($actionEvent);
}
Expand Down
30 changes: 30 additions & 0 deletions tests/CommandBusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,34 @@ public function it_passes_queued_commands_to_command_dispatch_exception_in_case_
$this->assertSame(1, count($commandDispatchException->getPendingCommands()));
$this->assertSame(CustomMessage::class, get_class($commandDispatchException->getPendingCommands()[0]));
}

/**
* @test
*/
public function it_always_triggers_finalize_listeners_regardless_whether_the_propagation_of_the_event_has_been_stopped(): void
{
$this->commandBus->attach(CommandBus::EVENT_DISPATCH, function (ActionEvent $event) {
$event->setParam(CommandBus::EVENT_PARAM_MESSAGE_HANDLER, function (): void {
});
}, CommandBus::PRIORITY_LOCATE_HANDLER + 1);
$this->commandBus->attach(CommandBus::EVENT_DISPATCH, function (ActionEvent $event): void {
$event->stopPropagation();
throw new \RuntimeException('boom');
Copy link
Member

Choose a reason for hiding this comment

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

1

}, CommandBus::PRIORITY_INVOKE_HANDLER - 1);

$this->commandBus->attach(MessageBus::EVENT_FINALIZE, function (): void {
}, 3);
$finalizeHasBeenCalled = false;
$this->commandBus->attach(MessageBus::EVENT_FINALIZE, function () use (&$finalizeHasBeenCalled): void {
$finalizeHasBeenCalled = true;
}, 2);

try {
$this->commandBus->dispatch('a message');
} catch (\Throwable $e) {
// ignore
}

$this->assertTrue($finalizeHasBeenCalled);
}
}
30 changes: 30 additions & 0 deletions tests/EventBusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,34 @@ function (ActionEvent $actionEvent) use (&$finalizeIsTriggered, &$listenerExcept
$this->assertSame($customMessage, $handler->getLastMessage());
$this->assertEquals(2, $handler->getInvokeCounter());
}

/**
* @test
*/
public function it_always_triggers_finalize_listeners_regardless_whether_the_propagation_of_the_event_has_been_stopped(): void
{
$this->eventBus->attach(EventBus::EVENT_DISPATCH, function (ActionEvent $event) {
$event->setParam(EventBus::EVENT_PARAM_MESSAGE_HANDLER, function (): void {
});
}, EventBus::PRIORITY_LOCATE_HANDLER + 1);
$this->eventBus->attach(EventBus::EVENT_DISPATCH, function (ActionEvent $event): void {
$event->stopPropagation();
throw new \RuntimeException('boom');
Copy link
Member

Choose a reason for hiding this comment

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

2

}, EventBus::PRIORITY_INVOKE_HANDLER - 1);

$this->eventBus->attach(MessageBus::EVENT_FINALIZE, function (): void {
}, 3);
$finalizeHasBeenCalled = false;
$this->eventBus->attach(MessageBus::EVENT_FINALIZE, function () use (&$finalizeHasBeenCalled): void {
$finalizeHasBeenCalled = true;
}, 2);

try {
$this->eventBus->dispatch('a message');
} catch (\Throwable $e) {
// ignore
}

$this->assertTrue($finalizeHasBeenCalled);
}
}
1 change: 0 additions & 1 deletion tests/Plugin/MessageFactoryPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ function (ActionEvent $actionEvent): void {

/**
* @test
* @group by
*/
public function it_will_return_early_if_message_name_not_present_in_message(): void
{
Expand Down
26 changes: 26 additions & 0 deletions tests/QueryBusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,30 @@ function (ActionEvent $actionEvent) use (&$exceptionParamWasSet): void {

$this->assertTrue($exceptionParamWasSet);
}

/**
* @test
*/
public function it_always_triggers_finalize_listeners_regardless_whether_the_propagation_of_the_event_has_been_stopped(): void
{
$this->queryBus->attach(QueryBus::EVENT_DISPATCH, function (ActionEvent $event) {
$event->setParam(QueryBus::EVENT_PARAM_MESSAGE_HANDLER, function (): void {
});
}, QueryBus::PRIORITY_LOCATE_HANDLER + 1);
$this->queryBus->attach(QueryBus::EVENT_DISPATCH, function (ActionEvent $event): void {
$event->stopPropagation();
throw new \RuntimeException('boom');
Copy link
Member

Choose a reason for hiding this comment

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

3

}, QueryBus::PRIORITY_INVOKE_HANDLER - 1);

$this->queryBus->attach(MessageBus::EVENT_FINALIZE, function (): void {
}, 3);
$finalizeHasBeenCalled = false;
$this->queryBus->attach(MessageBus::EVENT_FINALIZE, function () use (&$finalizeHasBeenCalled): void {
$finalizeHasBeenCalled = true;
}, 2);

$this->queryBus->dispatch('a message');

$this->assertTrue($finalizeHasBeenCalled);
}
}