Skip to content

Commit

Permalink
Merge pull request #53 from omnilight/ability_to_create_processors_wi…
Browse files Browse the repository at this point in the history
…th_arg

Added support for creation processors with arguments
  • Loading branch information
abacaphiliac authored Mar 11, 2018
2 parents a00897d + da8ed92 commit 4c3064f
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 4 deletions.
37 changes: 35 additions & 2 deletions src/EnliteMonolog/Service/MonologServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 . ')');
}

Expand Down Expand Up @@ -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'
);
}

/**
Expand Down
111 changes: 109 additions & 2 deletions test/EnliteMonologTest/Service/MonologServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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()
{
Expand All @@ -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();
Expand Down
18 changes: 18 additions & 0 deletions test/EnliteMonologTest/Service/ProcessorMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace EnliteMonologTest\Service;

class ProcessorMock
{
private $argument;

public function __construct($argument)
{
$this->argument = $argument;
}

public function __invoke(array $record)
{
return $record;
}
}

0 comments on commit 4c3064f

Please sign in to comment.