From a8e35bc5745b30301bf45873422f65caafe9bee7 Mon Sep 17 00:00:00 2001 From: Thomas Vargiu Date: Sun, 17 Feb 2019 15:33:01 +0100 Subject: [PATCH 1/2] Allow retrieving services from container in ModuleManagerFactory --- src/Service/ModuleManagerFactory.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Service/ModuleManagerFactory.php b/src/Service/ModuleManagerFactory.php index 2d50ea820..b5f733485 100644 --- a/src/Service/ModuleManagerFactory.php +++ b/src/Service/ModuleManagerFactory.php @@ -35,8 +35,9 @@ class ModuleManagerFactory implements FactoryInterface public function __invoke(ContainerInterface $container, $name, array $options = null) { $configuration = $container->get('ApplicationConfig'); - $listenerOptions = new ListenerOptions($configuration['module_listener_options']); - $defaultListeners = new DefaultListenerAggregate($listenerOptions); + $defaultListeners = $container->has(DefaultListenerAggregate::class) + ? $container->get(DefaultListenerAggregate::class) + : $this->createDefaultListener($container); $serviceListener = $container->get('ServiceListener'); $serviceListener->addServiceManager( @@ -83,4 +84,14 @@ public function __invoke(ContainerInterface $container, $name, array $options = return $moduleManager; } + + private function createDefaultListener(ContainerInterface $container) + { + $configuration = $container->get('ApplicationConfig'); + $listenerOptions = $container->has(ListenerOptions::class) + ? $container->get(ListenerOptions::class) + : new ListenerOptions($configuration['module_listener_options']); + + return new DefaultListenerAggregate($listenerOptions); + } } From b795964865b4d46e5e84293e7b30540d9d98e3bc Mon Sep 17 00:00:00 2001 From: Thomas Vargiu Date: Thu, 21 Feb 2019 17:19:21 +0100 Subject: [PATCH 2/2] Added ModuleManagerFactory unit tests --- test/Service/ModuleManagerFactoryTest.php | 263 ++++++++++++++++++++++ 1 file changed, 263 insertions(+) create mode 100644 test/Service/ModuleManagerFactoryTest.php diff --git a/test/Service/ModuleManagerFactoryTest.php b/test/Service/ModuleManagerFactoryTest.php new file mode 100644 index 000000000..bfce48112 --- /dev/null +++ b/test/Service/ModuleManagerFactoryTest.php @@ -0,0 +1,263 @@ +prophesize(ContainerInterface::class); + $serviceListener = $this->prophesize(ServiceListenerInterface::class); + $eventManager = $this->prophesize(EventManagerInterface::class); + + $configuration = [ + 'modules' => [ + 'Foo', + ], + 'module_listener_options' => [], + ]; + $container->get('ApplicationConfig') + ->willReturn($configuration); + $container->get('ServiceListener') + ->willReturn($serviceListener->reveal()); + $container->get('EventManager') + ->willReturn($eventManager->reveal()); + + $container->has(DefaultListenerAggregate::class) + ->willReturn(false); + $container->has(ListenerOptions::class) + ->willReturn(false); + + $serviceListener->addServiceManager( + $container->reveal(), + 'service_manager', + ServiceProviderInterface::class, + 'getServiceConfig' + ) + ->shouldBeCalled(); + + $serviceListener->addServiceManager( + 'ControllerManager', + 'controllers', + ControllerProviderInterface::class, + 'getControllerConfig' + ) + ->shouldBeCalled(); + + $serviceListener->addServiceManager( + 'ControllerPluginManager', + 'controller_plugins', + ControllerPluginProviderInterface::class, + 'getControllerPluginConfig' + ) + ->shouldBeCalled(); + + $serviceListener->addServiceManager( + 'ViewHelperManager', + 'view_helpers', + ViewHelperProviderInterface::class, + 'getViewHelperConfig' + ) + ->shouldBeCalled(); + + $serviceListener->addServiceManager( + 'RoutePluginManager', + 'route_manager', + RouteProviderInterface::class, + 'getRouteConfig' + ) + ->shouldBeCalled(); + + $serviceListener->attach($eventManager->reveal()) + ->shouldBeCalled(); + + $factory = new ModuleManagerFactory(); + + $service = $factory($container->reveal(), 'ModuleManager'); + + $this->assertInstanceOf(ModuleManager::class, $service); + $this->assertInstanceOf(ModuleEvent::class, $service->getEvent()); + $this->assertSame($container->reveal(), $service->getEvent()->getParam('ServiceManager')); + $this->assertSame($eventManager->reveal(), $service->getEventManager()); + $this->assertSame($configuration['modules'], $service->getModules()); + } + + public function testFactoryWithListenerOptionsFromContainer() + { + $container = $this->prophesize(ContainerInterface::class); + $serviceListener = $this->prophesize(ServiceListenerInterface::class); + $eventManager = $this->prophesize(EventManagerInterface::class); + $listenerOptions = new ListenerOptions(); + + $configuration = [ + 'modules' => [ + 'Foo', + ], + 'module_listener_options' => [], + ]; + $container->get('ApplicationConfig') + ->willReturn($configuration); + $container->get('ServiceListener') + ->willReturn($serviceListener->reveal()); + $container->get('EventManager') + ->willReturn($eventManager->reveal()); + + $container->has(DefaultListenerAggregate::class) + ->willReturn(false); + $container->has(ListenerOptions::class) + ->willReturn(true); + + $container->get(ListenerOptions::class) + ->willReturn($listenerOptions); + + $serviceListener->addServiceManager( + $container->reveal(), + 'service_manager', + ServiceProviderInterface::class, + 'getServiceConfig' + ) + ->shouldBeCalled(); + + $serviceListener->addServiceManager( + 'ControllerManager', + 'controllers', + ControllerProviderInterface::class, + 'getControllerConfig' + ) + ->shouldBeCalled(); + + $serviceListener->addServiceManager( + 'ControllerPluginManager', + 'controller_plugins', + ControllerPluginProviderInterface::class, + 'getControllerPluginConfig' + ) + ->shouldBeCalled(); + + $serviceListener->addServiceManager( + 'ViewHelperManager', + 'view_helpers', + ViewHelperProviderInterface::class, + 'getViewHelperConfig' + ) + ->shouldBeCalled(); + + $serviceListener->addServiceManager( + 'RoutePluginManager', + 'route_manager', + RouteProviderInterface::class, + 'getRouteConfig' + ) + ->shouldBeCalled(); + + $serviceListener->attach($eventManager->reveal()) + ->shouldBeCalled(); + + $factory = new ModuleManagerFactory(); + + $service = $factory($container->reveal(), 'ModuleManager'); + + $this->assertInstanceOf(ModuleManager::class, $service); + $this->assertInstanceOf(ModuleEvent::class, $service->getEvent()); + $this->assertSame($container->reveal(), $service->getEvent()->getParam('ServiceManager')); + $this->assertSame($eventManager->reveal(), $service->getEventManager()); + $this->assertSame($configuration['modules'], $service->getModules()); + } + + public function testFactoryWithDefaultListenerAggregateFromContainer() + { + $container = $this->prophesize(ContainerInterface::class); + $serviceListener = $this->prophesize(ServiceListenerInterface::class); + $eventManager = $this->prophesize(EventManagerInterface::class); + $defaultListenerAggregate = $this->prophesize(DefaultListenerAggregate::class); + + $configuration = [ + 'modules' => [ + 'Foo', + ], + 'module_listener_options' => [], + ]; + $container->get('ApplicationConfig') + ->willReturn($configuration); + $container->get('ServiceListener') + ->willReturn($serviceListener->reveal()); + $container->get('EventManager') + ->willReturn($eventManager->reveal()); + + $container->has(DefaultListenerAggregate::class) + ->willReturn(true); + + $container->get(DefaultListenerAggregate::class) + ->willReturn($defaultListenerAggregate->reveal()); + + $serviceListener->addServiceManager( + $container->reveal(), + 'service_manager', + ServiceProviderInterface::class, + 'getServiceConfig' + ) + ->shouldBeCalled(); + + $serviceListener->addServiceManager( + 'ControllerManager', + 'controllers', + ControllerProviderInterface::class, + 'getControllerConfig' + ) + ->shouldBeCalled(); + + $serviceListener->addServiceManager( + 'ControllerPluginManager', + 'controller_plugins', + ControllerPluginProviderInterface::class, + 'getControllerPluginConfig' + ) + ->shouldBeCalled(); + + $serviceListener->addServiceManager( + 'ViewHelperManager', + 'view_helpers', + ViewHelperProviderInterface::class, + 'getViewHelperConfig' + ) + ->shouldBeCalled(); + + $serviceListener->addServiceManager( + 'RoutePluginManager', + 'route_manager', + RouteProviderInterface::class, + 'getRouteConfig' + ) + ->shouldBeCalled(); + + $defaultListenerAggregate->attach($eventManager->reveal()) + ->shouldBeCalled(); + $serviceListener->attach($eventManager->reveal()) + ->shouldBeCalled(); + + $factory = new ModuleManagerFactory(); + + $service = $factory($container->reveal(), 'ModuleManager'); + + $this->assertInstanceOf(ModuleManager::class, $service); + $this->assertInstanceOf(ModuleEvent::class, $service->getEvent()); + $this->assertSame($container->reveal(), $service->getEvent()->getParam('ServiceManager')); + $this->assertSame($eventManager->reveal(), $service->getEventManager()); + $this->assertSame($configuration['modules'], $service->getModules()); + } +}