Skip to content

Commit

Permalink
Resolve parameters in attributes & annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC committed Sep 18, 2021
1 parent ac1ef3c commit dfa91eb
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 13 deletions.
2 changes: 2 additions & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
</service>

<service id="Bizkit\LoggableCommandBundle\ConfigurationProvider\AttributeConfigurationProvider">
<argument type="service" id="parameter_bag" />
<tag name="bizkit_loggable_command.configuration_provider" priority="100" />
</service>

<service id="Bizkit\LoggableCommandBundle\ConfigurationProvider\AnnotationConfigurationProvider">
<argument type="service" id="annotation_reader" />
<argument type="service" id="parameter_bag" />
<tag name="bizkit_loggable_command.configuration_provider" priority="50" />
</service>

Expand Down
11 changes: 9 additions & 2 deletions src/ConfigurationProvider/AnnotationConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Bizkit\LoggableCommandBundle\ConfigurationProvider\Attribute\LoggableOutput;
use Bizkit\LoggableCommandBundle\LoggableOutput\LoggableOutputInterface;
use Doctrine\Common\Annotations\Reader as AnnotationsReaderInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;

final class AnnotationConfigurationProvider implements ConfigurationProviderInterface
{
Expand All @@ -15,9 +16,15 @@ final class AnnotationConfigurationProvider implements ConfigurationProviderInte
*/
private $annotationsReader;

public function __construct(AnnotationsReaderInterface $annotationsReader)
/**
* @var ContainerBagInterface
*/
private $containerBag;

public function __construct(AnnotationsReaderInterface $annotationsReader, ContainerBagInterface $containerBag)
{
$this->annotationsReader = $annotationsReader;
$this->containerBag = $containerBag;
}

public function __invoke(LoggableOutputInterface $loggableOutput): array
Expand All @@ -31,6 +38,6 @@ public function __invoke(LoggableOutputInterface $loggableOutput): array
return [];
}

return $annotation->getOptions();
return $this->containerBag->resolveValue($annotation->getOptions());
}
}
13 changes: 12 additions & 1 deletion src/ConfigurationProvider/AttributeConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,20 @@

use Bizkit\LoggableCommandBundle\ConfigurationProvider\Attribute\LoggableOutput;
use Bizkit\LoggableCommandBundle\LoggableOutput\LoggableOutputInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;

final class AttributeConfigurationProvider implements ConfigurationProviderInterface
{
/**
* @var ContainerBagInterface
*/
private $containerBag;

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

public function __invoke(LoggableOutputInterface $loggableOutput): array
{
$reflectionObject = new \ReflectionObject($loggableOutput);
Expand All @@ -22,6 +33,6 @@ public function __invoke(LoggableOutputInterface $loggableOutput): array
/** @var LoggableOutput $attribute */
$attribute = $reflectionAttribute->newInstance();

return $attribute->getOptions();
return $this->containerBag->resolveValue($attribute->getOptions());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
use Bizkit\LoggableCommandBundle\ConfigurationProvider\ConfigurationProviderInterface;
use Bizkit\LoggableCommandBundle\Tests\ConfigurationProvider\Fixtures\DummyLoggableOutput;
use Bizkit\LoggableCommandBundle\Tests\ConfigurationProvider\Fixtures\DummyLoggableOutputWithAnnotation;
use Bizkit\LoggableCommandBundle\Tests\ConfigurationProvider\Fixtures\DummyLoggableOutputWithAnnotationAndParam;
use Bizkit\LoggableCommandBundle\Tests\TestCase;
use Doctrine\Common\Annotations\Annotation;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\DocParser;
use Monolog\Logger;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;

/**
* @covers \Bizkit\LoggableCommandBundle\ConfigurationProvider\AnnotationConfigurationProvider
Expand All @@ -28,23 +32,65 @@ protected function setUp(): void

public function testProviderReturnsExpectedConfigWhenAnnotationIsFound(): void
{
$provider = $this->createConfigurationProvider();
$handlerOptions = ['filename' => 'annotation-test', 'level' => Logger::CRITICAL, 'max_files' => 4];

$provider = $this->createConfigurationProvider(
$this->createContainerBagWithResolveValueMethodCalled($handlerOptions)
);

$this->assertSame(
['filename' => 'annotation-test', 'level' => Logger::CRITICAL, 'max_files' => 4],
$handlerOptions,
$provider(new DummyLoggableOutputWithAnnotation())
);
}

public function testProviderReturnsEmptyConfigWhenAnnotationIsNotFound(): void
{
$provider = $this->createConfigurationProvider();
$provider = $this->createConfigurationProvider(
$this->createContainerBagWithoutResolveValueMethodCalled()
);

$this->assertSame([], $provider(new DummyLoggableOutput()));
}

private function createConfigurationProvider(): ConfigurationProviderInterface
public function testProviderResolvesConfigParameters(): void
{
$handlerOptions = ['filename' => 'annotation-test', 'path' => '/var/log/messenger/{filename}.log'];

$provider = $this->createConfigurationProvider(
new ContainerBag($container = new Container())
);

$container->setParameter('kernel.logs_dir', '/var/log');

$this->assertSame(
$handlerOptions,
$provider(new DummyLoggableOutputWithAnnotationAndParam())
);
}

private function createConfigurationProvider(ContainerBagInterface $containerBag): ConfigurationProviderInterface
{
return new AnnotationConfigurationProvider(new AnnotationReader(new DocParser()), $containerBag);
}

private function createContainerBagWithResolveValueMethodCalled(array $handlerOptions): ContainerBagInterface
{
return new AnnotationConfigurationProvider(new AnnotationReader(new DocParser()));
$containerBag = $this->createMock(ContainerBagInterface::class);
$containerBag->expects($this->once())
->method('resolveValue')
->with($handlerOptions)
->willReturn($this->returnArgument(0))
;

return $containerBag;
}

private function createContainerBagWithoutResolveValueMethodCalled(): ContainerBagInterface
{
$containerBag = $this->createMock(ContainerBagInterface::class);
$containerBag->expects($this->never())->method('resolveValue');

return $containerBag;
}
}
56 changes: 51 additions & 5 deletions tests/ConfigurationProvider/AttributeConfigurationProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
use Bizkit\LoggableCommandBundle\ConfigurationProvider\ConfigurationProviderInterface;
use Bizkit\LoggableCommandBundle\Tests\ConfigurationProvider\Fixtures\DummyLoggableOutput;
use Bizkit\LoggableCommandBundle\Tests\ConfigurationProvider\Fixtures\DummyLoggableOutputWithAttribute;
use Bizkit\LoggableCommandBundle\Tests\ConfigurationProvider\Fixtures\DummyLoggableOutputWithAttributeAndParam;
use Bizkit\LoggableCommandBundle\Tests\TestCase;
use Monolog\Logger;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;

/**
* @requires PHP >= 8.0
Expand All @@ -20,23 +24,65 @@ final class AttributeConfigurationProviderTest extends TestCase
{
public function testProviderReturnsExpectedConfigWhenAttributeIsFound(): void
{
$provider = $this->createConfigurationProvider();
$handlerOptions = ['filename' => 'attribute-test', 'level' => Logger::EMERGENCY, 'bubble' => true];

$provider = $this->createConfigurationProvider(
$this->createContainerBagWithResolveValueMethodCalled($handlerOptions)
);

$this->assertSame(
['filename' => 'attribute-test', 'level' => Logger::EMERGENCY, 'bubble' => true],
$handlerOptions,
$provider(new DummyLoggableOutputWithAttribute())
);
}

public function testProviderReturnsEmptyConfigWhenAttributeIsNotFound(): void
{
$provider = $this->createConfigurationProvider();
$provider = $this->createConfigurationProvider(
$this->createContainerBagWithoutResolveValueMethodCalled()
);

$this->assertSame([], $provider(new DummyLoggableOutput()));
}

private function createConfigurationProvider(): ConfigurationProviderInterface
public function testProviderResolvesConfigParameters(): void
{
$handlerOptions = ['filename' => 'attribute-test', 'path' => '/var/log/messenger/{filename}.log'];

$provider = $this->createConfigurationProvider(
new ContainerBag($container = new Container())
);

$container->setParameter('kernel.logs_dir', '/var/log');

$this->assertSame(
$handlerOptions,
$provider(new DummyLoggableOutputWithAttributeAndParam())
);
}

private function createConfigurationProvider(ContainerBagInterface $containerBag): ConfigurationProviderInterface
{
return new AttributeConfigurationProvider($containerBag);
}

private function createContainerBagWithResolveValueMethodCalled(array $handlerOptions): ContainerBagInterface
{
return new AttributeConfigurationProvider();
$containerBag = $this->createMock(ContainerBagInterface::class);
$containerBag->expects($this->once())
->method('resolveValue')
->with($handlerOptions)
->willReturn($this->returnArgument(0))
;

return $containerBag;
}

private function createContainerBagWithoutResolveValueMethodCalled(): ContainerBagInterface
{
$containerBag = $this->createMock(ContainerBagInterface::class);
$containerBag->expects($this->never())->method('resolveValue');

return $containerBag;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Bizkit\LoggableCommandBundle\Tests\ConfigurationProvider\Fixtures;

use Bizkit\LoggableCommandBundle\ConfigurationProvider\Attribute\LoggableOutput;
use Bizkit\LoggableCommandBundle\LoggableOutput\LoggableOutputInterface;
use Bizkit\LoggableCommandBundle\LoggableOutput\LoggableOutputTrait;

/**
* @LoggableOutput(filename="annotation-test", path="%kernel.logs_dir%/messenger/{filename}.log")
*/
final class DummyLoggableOutputWithAnnotationAndParam implements LoggableOutputInterface
{
use LoggableOutputTrait;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Bizkit\LoggableCommandBundle\Tests\ConfigurationProvider\Fixtures;

use Bizkit\LoggableCommandBundle\ConfigurationProvider\Attribute\LoggableOutput;
use Bizkit\LoggableCommandBundle\LoggableOutput\LoggableOutputInterface;
use Bizkit\LoggableCommandBundle\LoggableOutput\LoggableOutputTrait;

#[LoggableOutput(filename: 'attribute-test', path: '%kernel.logs_dir%/messenger/{filename}.log')]
final class DummyLoggableOutputWithAttributeAndParam implements LoggableOutputInterface
{
use LoggableOutputTrait;
}

0 comments on commit dfa91eb

Please sign in to comment.