diff --git a/Resources/config/processors.yaml b/Resources/config/processors.yaml index 3b72930..7514448 100644 --- a/Resources/config/processors.yaml +++ b/Resources/config/processors.yaml @@ -7,9 +7,9 @@ services: - setEnvironment: - '%kernel.environment%' - setTokenStorage: - - '@security.token_storage' + - '@?security.token_storage' - setRequestStack: - - '@request_stack' + - '@?request_stack' Deamon\LoggerExtraBundle\Processors\Monolog\DeamonLoggerExtraWebProcessor: alias: deamon.logger_extra.processors.web_processor diff --git a/Tests/DependencyInjection/DeamonLoggerExtraExtensionTest.php b/Tests/DependencyInjection/DeamonLoggerExtraExtensionTest.php index 277d9ad..292cd54 100755 --- a/Tests/DependencyInjection/DeamonLoggerExtraExtensionTest.php +++ b/Tests/DependencyInjection/DeamonLoggerExtraExtensionTest.php @@ -4,7 +4,11 @@ use Deamon\LoggerExtraBundle\DependencyInjection\DeamonLoggerExtraExtension; use PHPUnit\Framework\TestCase; +use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; +use Symfony\Component\DependencyInjection\Reference; class DeamonLoggerExtraExtensionTest extends TestCase { @@ -120,6 +124,21 @@ public function testConvertStringHandlerToArray(): void $this->assertEquals('bar', $tag[0]['handler']); } + public function testOptionalServiceReferencesUseIgnoreOnInvalidBehavior(): void + { + $loader = new YamlFileLoader( + $this->container, + new FileLocator(__DIR__ . '/../../Resources/config') + ); + $loader->load('processors.yaml'); + + $definition = $this->container->getDefinition('deamon.logger_extra.processors.web_processor'); + $methodCalls = $definition->getMethodCalls(); + + $this->assertReferenceUsesIgnoreOnInvalidBehavior($methodCalls, 'setTokenStorage'); + $this->assertReferenceUsesIgnoreOnInvalidBehavior($methodCalls, 'setRequestStack'); + } + /** * @return array */ @@ -163,4 +182,24 @@ private function getValidConfigMin(): array 'config' => null, ]; } + + /** + * @param array}> $methodCalls + */ + private function assertReferenceUsesIgnoreOnInvalidBehavior(array $methodCalls, string $methodName): void + { + foreach ($methodCalls as $methodCall) { + if ($methodCall[0] !== $methodName) { + continue; + } + + $this->assertCount(1, $methodCall[1]); + $this->assertInstanceOf(Reference::class, $methodCall[1][0]); + $this->assertSame(ContainerInterface::IGNORE_ON_INVALID_REFERENCE, $methodCall[1][0]->getInvalidBehavior()); + + return; + } + + $this->fail(sprintf('Method call "%s" was not found.', $methodName)); + } }