diff --git a/phpunit-deprecation-baseline.json b/phpunit-deprecation-baseline.json index 16bb268..a0a4c4c 100644 --- a/phpunit-deprecation-baseline.json +++ b/phpunit-deprecation-baseline.json @@ -4,6 +4,16 @@ "message": "The Symfony\\Bundle\\MonologBundle\\DependencyInjection\\Compiler\\DebugHandlerPass class is deprecated since version 2.12 and will be removed in 4.0. Use AddDebugLogProcessorPass in FrameworkBundle instead.", "count": 1 }, + { + "location": "Bizkit\\LoggableCommandBundle\\Tests\\DependencyInjection\\Compiler\\ExcludeMonologChannelPassTest::testChannelIsExcludedWhenExpected", + "message": "The Symfony\\Bundle\\MonologBundle\\DependencyInjection\\Compiler\\DebugHandlerPass class is deprecated since version 2.12 and will be removed in 4.0. Use AddDebugLogProcessorPass in FrameworkBundle instead.", + "count": 7 + }, + { + "location": "Bizkit\\LoggableCommandBundle\\Tests\\DependencyInjection\\Compiler\\ExcludeMonologChannelPassTest::testChannelIsExcludedWhenExpected", + "message": "Function libxml_disable_entity_loader() is deprecated", + "count": 56 + }, { "location": "Bizkit\\LoggableCommandBundle\\Tests\\DependencyInjection\\BizkitLoggableCommandExtensionTest::testAttributeConfigurationProviderIsNotRemovedWhenPHP8", "message": "Function libxml_disable_entity_loader() is deprecated", diff --git a/src/DependencyInjection/Compiler/ExcludeMonologChannelPass.php b/src/DependencyInjection/Compiler/ExcludeMonologChannelPass.php index 0ed4cfb..50c3043 100644 --- a/src/DependencyInjection/Compiler/ExcludeMonologChannelPass.php +++ b/src/DependencyInjection/Compiler/ExcludeMonologChannelPass.php @@ -19,16 +19,24 @@ public function process(ContainerBuilder $container): void /** @var string[] $exclusiveHandlerNames */ $exclusiveHandlerNames = []; - /** @var array}> $handlersToChannels */ + /** @var array}|null> $handlersToChannels */ $handlersToChannels = $container->getParameter('monolog.handlers_to_channels'); foreach ($handlersToChannels as $id => &$handlersToChannel) { - if ('exclusive' !== $handlersToChannel['type']) { + if (null === $handlersToChannel) { + $handlersToChannel = [ + 'type' => 'exclusive', + 'elements' => [], + ]; + } elseif ('exclusive' !== $handlersToChannel['type']) { continue; } if (false !== $index = array_search('!'.$monologChannelName, $handlersToChannel['elements'], true)) { array_splice($handlersToChannel['elements'], $index, 1); - } else { + if (!$handlersToChannel['elements']) { + $handlersToChannel = null; + } + } elseif (!\in_array($monologChannelName, $handlersToChannel['elements'], true)) { $handlersToChannel['elements'][] = $monologChannelName; $exclusiveHandlerNames[] = substr($id, 16); } diff --git a/tests/BizkitLoggableCommandBundleTest.php b/tests/BizkitLoggableCommandBundleTest.php index 5a38a37..0b672e9 100644 --- a/tests/BizkitLoggableCommandBundleTest.php +++ b/tests/BizkitLoggableCommandBundleTest.php @@ -9,6 +9,7 @@ use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass; use Symfony\Bundle\MonologBundle\MonologBundle; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\HttpKernel\Bundle\BundleInterface; /** * @covers \Bizkit\LoggableCommandBundle\BizkitLoggableCommandBundle @@ -19,8 +20,10 @@ public function testCompilerPassIsRegisteredWithCorrectPriority(): void { $container = new ContainerBuilder(); - (new MonologBundle())->build($container); - (new BizkitLoggableCommandBundle())->build($container); + /** @var BundleInterface $bundle */ + foreach ([new MonologBundle(), new BizkitLoggableCommandBundle()] as $bundle) { + $bundle->build($container); + } $compilerPassIndexes = []; foreach ($container->getCompilerPassConfig()->getBeforeOptimizationPasses() as $i => $compilerPass) { diff --git a/tests/DependencyInjection/Compiler/ExcludeMonologChannelPassTest.php b/tests/DependencyInjection/Compiler/ExcludeMonologChannelPassTest.php index 5cf5918..d9f3a18 100644 --- a/tests/DependencyInjection/Compiler/ExcludeMonologChannelPassTest.php +++ b/tests/DependencyInjection/Compiler/ExcludeMonologChannelPassTest.php @@ -4,9 +4,12 @@ namespace Bizkit\LoggableCommandBundle\Tests\DependencyInjection\Compiler; +use Bizkit\LoggableCommandBundle\BizkitLoggableCommandBundle; use Bizkit\LoggableCommandBundle\DependencyInjection\Compiler\ExcludeMonologChannelPass; use Bizkit\LoggableCommandBundle\Tests\TestCase; +use Symfony\Bundle\MonologBundle\MonologBundle; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\HttpKernel\Bundle\BundleInterface; /** * @covers \Bizkit\LoggableCommandBundle\DependencyInjection\Compiler\ExcludeMonologChannelPass @@ -16,27 +19,52 @@ final class ExcludeMonologChannelPassTest extends TestCase /** * @dataProvider handlerChannels */ - public function testChannelIsExcludedWhenExpected(?array $channels, array $expectedChannels, array $expectedLog): void + public function testChannelIsExcludedWhenExpected(?array $channels, ?array $expectedChannels, array $expectedLog): void { $container = new ContainerBuilder(); - $container->setParameter('bizkit_loggable_command.channel_name', 'channel_name'); - $container->setParameter('monolog.handlers_to_channels', ['monolog.handler.foobar' => $channels]); + $container->setParameter('kernel.logs_dir', __DIR__); + $container->setParameter('kernel.environment', 'dev'); - (new ExcludeMonologChannelPass())->process($container); + /** @var BundleInterface $bundle */ + foreach ([new MonologBundle(), new BizkitLoggableCommandBundle()] as $bundle) { + $container->registerExtension($extension = $bundle->getContainerExtension()); + $bundle->build($container); + $container->loadFromExtension($extension->getAlias()); + } - /** @var array}> $handlersToChannels */ + $container->loadFromExtension('monolog', [ + 'handlers' => [ + 'foobar' => [ + 'type' => 'stream', + 'channels' => $channels, + ], + ], + ]); + + $container->compile(); + + /** @var array}|null> $handlersToChannels */ $handlersToChannels = $container->getParameter('monolog.handlers_to_channels'); - $this->assertSame($expectedChannels, $handlersToChannels['monolog.handler.foobar']['elements']); + $this->assertSame($expectedChannels, $handlersToChannels['monolog.handler.foobar']); - $this->assertSame($expectedLog, $container->getCompiler()->getLog()); + $this->assertSame($expectedLog, array_values(array_filter( + $container->getCompiler()->getLog(), + function (string $log): bool { + return 0 === strpos($log, ExcludeMonologChannelPass::class); + } + ))); } public function handlerChannels(): iterable { - $log = sprintf('%s: Excluded Monolog channel "channel_name" from the following exclusive handlers "foobar".', ExcludeMonologChannelPass::class); + $log = sprintf('%s: Excluded Monolog channel "loggable_output" from the following exclusive handlers "foobar".', ExcludeMonologChannelPass::class); - yield 'Inclusive' => [['type' => 'inclusive', 'elements' => ['foo', 'bar', 'baz']], ['foo', 'bar', 'baz'], []]; - yield 'Exclusive without exception' => [['type' => 'exclusive', 'elements' => ['foo', 'baz']], ['foo', 'baz', 'channel_name'], [$log]]; - yield 'Exclusive with exception' => [['type' => 'exclusive', 'elements' => ['foo', '!channel_name', 'baz']], ['foo', 'baz'], []]; + yield 'None' => [null, ['type' => 'exclusive', 'elements' => ['loggable_output']], [$log]]; + yield 'Empty array' => [[], ['type' => 'exclusive', 'elements' => ['loggable_output']], [$log]]; + yield 'Inclusive' => [['app'], ['type' => 'inclusive', 'elements' => ['app']], []]; + yield 'Exclusive without exception' => [['!event'], ['type' => 'exclusive', 'elements' => ['event', 'loggable_output']], [$log]]; + yield 'Exclusive with exception' => [['!event', '!!loggable_output'], ['type' => 'exclusive', 'elements' => ['event']], []]; + yield 'Exclusive with only an exception' => [['!!loggable_output'], null, []]; + yield 'Explicitly excluded' => [['!loggable_output'], ['type' => 'exclusive', 'elements' => ['loggable_output']], []]; } }