From ae8056f5c57cf78e6a5c9f909eb3adc73243e61f Mon Sep 17 00:00:00 2001 From: Nicolas Eeckeloo Date: Fri, 5 Dec 2014 10:40:02 +0100 Subject: [PATCH] Improve flexibility --- src/Factory/LoggerFactory.php | 17 +++++++++++++-- tests/Factory/LoggerFactoryTest.php | 34 +++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/Factory/LoggerFactory.php b/src/Factory/LoggerFactory.php index 50bf78b..64b920e 100644 --- a/src/Factory/LoggerFactory.php +++ b/src/Factory/LoggerFactory.php @@ -54,7 +54,7 @@ private function createHandler($handler) )); } - $handlerPluginManager = $this->serviceLocator->get('MonologModule\Handler\HandlerPluginManager'); + $handlerPluginManager = $this->getPluginManager('MonologModule\Handler\HandlerPluginManager'); $instance = $this->createInstanceFromParams($handler, $handlerPluginManager); if (isset($handler['formatter'])) { @@ -81,7 +81,7 @@ private function createFormatter($formatter) )); } - $formatterPluginManager = $this->serviceLocator->get('MonologModule\Formatter\FormatterPluginManager'); + $formatterPluginManager = $this->getPluginManager('MonologModule\Formatter\FormatterPluginManager'); return $this->createInstanceFromParams($formatter, $formatterPluginManager); } @@ -131,4 +131,17 @@ private function createProcessor($processor) 'Processor must be a callable or the FQCN of an invokable class' ); } + + /** + * @param string $name + * @return \Zend\ServiceManager\AbstractPluginManager + */ + protected function getPluginManager($name) + { + if (!$this->serviceLocator || !$this->serviceLocator->has($name)) { + return null; + } + + return $this->serviceLocator->get($name); + } } diff --git a/tests/Factory/LoggerFactoryTest.php b/tests/Factory/LoggerFactoryTest.php index 4b59994..4ef48bb 100644 --- a/tests/Factory/LoggerFactoryTest.php +++ b/tests/Factory/LoggerFactoryTest.php @@ -34,6 +34,11 @@ public function testCreateLoggerWithoutName() public function testCreateLoggerWithHandler() { $serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface'); + $serviceLocator + ->expects($this->once()) + ->method('has') + ->with('MonologModule\Handler\HandlerPluginManager') + ->will($this->returnValue(true)); $serviceLocator ->expects($this->once()) ->method('get') @@ -59,6 +64,11 @@ public function testCreateLoggerWithHandler() public function testCreateLoggerWithHandlerIncludingOptions() { $serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface'); + $serviceLocator + ->expects($this->once()) + ->method('has') + ->with('MonologModule\Handler\HandlerPluginManager') + ->will($this->returnValue(true)); $serviceLocator ->expects($this->once()) ->method('get') @@ -122,11 +132,21 @@ public function testCreateLoggerWithHandlerAndFormatter() $serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface'); $serviceLocator ->expects($this->at(0)) + ->method('has') + ->with('MonologModule\Handler\HandlerPluginManager') + ->will($this->returnValue(true)); + $serviceLocator + ->expects($this->at(1)) ->method('get') ->with('MonologModule\Handler\HandlerPluginManager') ->will($this->returnValue(null)); $serviceLocator - ->expects($this->at(1)) + ->expects($this->at(2)) + ->method('has') + ->with('MonologModule\Formatter\FormatterPluginManager') + ->will($this->returnValue(true)); + $serviceLocator + ->expects($this->at(3)) ->method('get') ->with('MonologModule\Formatter\FormatterPluginManager') ->will($this->returnValue(null)); @@ -158,11 +178,21 @@ public function testCreateLoggerWithHandlerAndFormatterIncludingOptions() $serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface'); $serviceLocator ->expects($this->at(0)) + ->method('has') + ->with('MonologModule\Handler\HandlerPluginManager') + ->will($this->returnValue(true)); + $serviceLocator + ->expects($this->at(1)) ->method('get') ->with('MonologModule\Handler\HandlerPluginManager') ->will($this->returnValue(null)); $serviceLocator - ->expects($this->at(1)) + ->expects($this->at(2)) + ->method('has') + ->with('MonologModule\Formatter\FormatterPluginManager') + ->will($this->returnValue(true)); + $serviceLocator + ->expects($this->at(3)) ->method('get') ->with('MonologModule\Formatter\FormatterPluginManager') ->will($this->returnValue(null));