diff --git a/src/EnliteMonolog/Service/MonologServiceFactory.php b/src/EnliteMonolog/Service/MonologServiceFactory.php index dcc6fbb..b18577d 100644 --- a/src/EnliteMonolog/Service/MonologServiceFactory.php +++ b/src/EnliteMonolog/Service/MonologServiceFactory.php @@ -147,7 +147,7 @@ public function createFormatter($container, $formatter) $formatterClassName = $formatter['name']; - if (!class_exists($formatter['name'])) { + if (!class_exists($formatterClassName)) { throw new RuntimeException('Cannot create logger formatter (' . $formatterClassName . ')'); } @@ -202,7 +202,40 @@ public function createProcessor($container, $processor) } } - throw new RuntimeException('Unknown processor type, must be a Closure or the FQCN of an invokable class'); + if (is_array($processor)) { + if (!isset($processor['name'])) { + throw new RuntimeException('Cannot create logger processor'); + } + + $processorClassName = $processor['name']; + + if (!class_exists($processorClassName)) { + throw new RuntimeException('Cannot create logger processor (' . $processorClassName . ')'); + } + + $arguments = array_key_exists('args', $processor) ? $processor['args'] : array(); + + if (!is_array($arguments)) { + throw new RuntimeException('Arguments of processor (' . $processorClassName . ') must be array'); + } + + try { + $instance = $this->createInstanceFromArguments($processorClassName, $arguments); + } catch (\InvalidArgumentException $exception) { + throw new RuntimeException(sprintf( + 'Processor(%s) has an invalid argument configuration', + $processorClassName + ), 0, $exception); + } + + if (is_callable($instance)) { + return $instance; + } + } + + throw new RuntimeException( + 'Unknown processor type, must be a Closure, array or the FQCN of an invokable class' + ); } /** diff --git a/test/EnliteMonologTest/Service/MonologServiceFactoryTest.php b/test/EnliteMonologTest/Service/MonologServiceFactoryTest.php index b13db56..13da147 100644 --- a/test/EnliteMonologTest/Service/MonologServiceFactoryTest.php +++ b/test/EnliteMonologTest/Service/MonologServiceFactoryTest.php @@ -157,7 +157,7 @@ public function testCreateProcessorFromClassName() /** * @expectedException \RuntimeException * @expectedExceptionCode 0 - * @expectedExceptionMessage Unknown processor type, must be a Closure or the FQCN of an invokable class + * @expectedExceptionMessage Unknown processor type, must be a Closure, array or the FQCN of an invokable class */ public function testCreateProcessorNotExistsClassName() { @@ -170,7 +170,7 @@ public function testCreateProcessorNotExistsClassName() /** * @expectedException \RuntimeException * @expectedExceptionCode 0 - * @expectedExceptionMessage Unknown processor type, must be a Closure or the FQCN of an invokable class + * @expectedExceptionMessage Unknown processor type, must be a Closure, array or the FQCN of an invokable class */ public function testCreateNonCallableProcessorFromServiceName() { @@ -182,6 +182,113 @@ public function testCreateNonCallableProcessorFromServiceName() $sut->createProcessor($services, '\stdClass'); } + /** + * @expectedException \RuntimeException + * @expectedExceptionCode 0 + */ + public function testCreateProcessorWithMissingProcessorNameConfig() + { + $serviceManager = new ServiceManager(); + $factory = new MonologServiceFactory(); + + $factory->createProcessor($serviceManager, array( + + )); + } + + /** + * @expectedException \RuntimeException + * @expectedExceptionCode 0 + */ + public function testCreateProcessorNotExistsClassNameInNamePart() + { + $serviceManager = new ServiceManager(); + $factory = new MonologServiceFactory(); + + $factory->createProcessor($serviceManager, array( + 'name' => '\InvalidProcessorClassName', + )); + } + + /** + * @expectedException \RuntimeException + * @expectedExceptionCode 0 + */ + public function testCreateProcessorWithInvalidProcessorArgumentConfig() + { + $serviceManager = new ServiceManager(); + $factory = new MonologServiceFactory(); + + $factory->createProcessor($serviceManager, array( + 'name' => '\EnliteMonologTest\Service\ProcessorMock', + 'args' => 'MyArgs', + )); + } + + /** + * @expectedException \RuntimeException + * @expectedExceptionCode 0 + */ + public function testCreateProcessorWithWrongNamedArguments() + { + $serviceManager = new ServiceManager(); + $factory = new MonologServiceFactory(); + + $factory->createProcessor($serviceManager, array( + 'name' => '\EnliteMonologTest\Service\ProcessorMock', + 'args' => array( + 'notExisted' => 'test', + ), + )); + } + + public function testCreateProcessorWithArguments() + { + $serviceManager = new ServiceManager(); + $factory = new MonologServiceFactory(); + + $actual = $factory->createProcessor($serviceManager, array( + 'name' => '\EnliteMonologTest\Service\ProcessorMock', + 'args' => array( + 'test', + ), + )); + + self::assertInstanceOf('\EnliteMonologTest\Service\ProcessorMock', $actual); + } + + public function testCreateProcessorWithoutArguments() + { + $serviceManager = new ServiceManager(); + $factory = new MonologServiceFactory(); + + $actual = $factory->createProcessor($serviceManager, array( + 'name' => '\Monolog\Processor\MemoryUsageProcessor', + )); + + self::assertInstanceOf('\Monolog\Processor\MemoryUsageProcessor', $actual); + } + + public function testCreateProcessorWithNamedArguments() + { + $serviceManager = new ServiceManager(); + $factory = new MonologServiceFactory(); + + $argument = 'test'; + $actual = $factory->createProcessor($serviceManager, array( + 'name' => '\EnliteMonologTest\Service\ProcessorMock', + 'args' => array( + 'argument' => $argument, + ), + )); + + self::assertInstanceOf('\EnliteMonologTest\Service\ProcessorMock', $actual); + $reflection = new \ReflectionClass($actual); + $property = $reflection->getProperty('argument'); + $property->setAccessible(true); + self::assertSame($argument, $property->getValue($actual), 'Unable to set arguments by name'); + } + public function testCreateFormatterFromServiceName() { $serviceManager = new ServiceManager(); diff --git a/test/EnliteMonologTest/Service/ProcessorMock.php b/test/EnliteMonologTest/Service/ProcessorMock.php new file mode 100644 index 0000000..bd99fa1 --- /dev/null +++ b/test/EnliteMonologTest/Service/ProcessorMock.php @@ -0,0 +1,18 @@ +argument = $argument; + } + + public function __invoke(array $record) + { + return $record; + } +}