From 7d3ff05ee162126dfa1273524b48c5fa1b7f1f5b Mon Sep 17 00:00:00 2001 From: Bas Kamer Date: Sun, 2 Jul 2017 17:13:37 +0200 Subject: [PATCH 1/2] avoid array_merge due to appending --- src/Plugin/ServiceLocatorPlugin.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Plugin/ServiceLocatorPlugin.php b/src/Plugin/ServiceLocatorPlugin.php index 1cba163..7f4f406 100644 --- a/src/Plugin/ServiceLocatorPlugin.php +++ b/src/Plugin/ServiceLocatorPlugin.php @@ -49,13 +49,14 @@ function (ActionEvent $actionEvent): void { $currentEventListeners = $actionEvent->getParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, []); $newEventListeners = []; - foreach ($currentEventListeners as $eventListenerAlias) { + foreach ($currentEventListeners as $key => $eventListenerAlias) { if (is_string($eventListenerAlias) && $this->serviceLocator->has($eventListenerAlias)) { - $newEventListeners[] = $this->serviceLocator->get($eventListenerAlias); + $newEventListeners[$key] = $this->serviceLocator->get($eventListenerAlias); } } - $actionEvent->setParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, array_merge($currentEventListeners, $newEventListeners)); + // merge array whilst preserving numeric keys and giving priority to newEventListeners + $actionEvent->setParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, $newEventListeners + $currentEventListeners); }, MessageBus::PRIORITY_LOCATE_HANDLER ); From c4f4bff8bcd2c7cc010910a72baa2ba10b173733 Mon Sep 17 00:00:00 2001 From: Bas Kamer Date: Sun, 2 Jul 2017 18:06:25 +0200 Subject: [PATCH 2/2] adds test that fails without fix --- tests/Plugin/ServiceLocatorPluginTest.php | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/Plugin/ServiceLocatorPluginTest.php b/tests/Plugin/ServiceLocatorPluginTest.php index 069b8b8..b7514ea 100644 --- a/tests/Plugin/ServiceLocatorPluginTest.php +++ b/tests/Plugin/ServiceLocatorPluginTest.php @@ -89,4 +89,42 @@ public function it_doesnt_override_previous_event_handlers(): void $this->assertTrue($handledOne); $this->assertSame(1, $handlerTwo->getInvokeCounter()); } + + /** + * @test + */ + public function make_sure_servicenames_do_not_end_up_as_listener_instance(): void + { + $handlerOne = new MessageHandler(); + $handlerTwo = new MessageHandler(); + $eventBus = new EventBus(); + $router = new EventRouter(); + + $container = $this->prophesize(ContainerInterface::class); + + $container->has('handler-one')->willReturn(true)->shouldBeCalled(); + $container->get('handler-one')->willReturn($handlerOne)->shouldBeCalled(); + + $container->has('handler-two')->willReturn(true)->shouldBeCalled(); + $container->get('handler-two')->willReturn($handlerTwo)->shouldBeCalled(); + + $router->route(SomethingDone::class)->to('handler-one'); + $router->route(SomethingDone::class)->to('handler-two'); + + $router->attachToMessageBus($eventBus); + + $locatorPlugin = new ServiceLocatorPlugin($container->reveal()); + + $locatorPlugin->attachToMessageBus($eventBus); + + $eventBus->attach(EventBus::EVENT_DISPATCH, + function (ActionEvent $actionEvent): void { + $listeners = $actionEvent->getParam(EventBus::EVENT_PARAM_EVENT_LISTENERS); + + $this->assertCount(2, $listeners); + $this->assertContainsOnly(MessageHandler::class, $listeners); + }, PHP_INT_MIN); + + $eventBus->dispatch(new SomethingDone(['foo' => 'bar'])); + } }