Skip to content

Commit 50c3d0c

Browse files
committed
Fix case when handler has no channels configured
1 parent cfb79e9 commit 50c3d0c

File tree

4 files changed

+65
-16
lines changed

4 files changed

+65
-16
lines changed

phpunit-deprecation-baseline.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
"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.",
55
"count": 1
66
},
7+
{
8+
"location": "Bizkit\\LoggableCommandBundle\\Tests\\DependencyInjection\\Compiler\\ExcludeMonologChannelPassTest::testChannelIsExcludedWhenExpected",
9+
"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.",
10+
"count": 7
11+
},
12+
{
13+
"location": "Bizkit\\LoggableCommandBundle\\Tests\\DependencyInjection\\Compiler\\ExcludeMonologChannelPassTest::testChannelIsExcludedWhenExpected",
14+
"message": "Function libxml_disable_entity_loader() is deprecated",
15+
"count": 56
16+
},
717
{
818
"location": "Bizkit\\LoggableCommandBundle\\Tests\\DependencyInjection\\BizkitLoggableCommandExtensionTest::testAttributeConfigurationProviderIsNotRemovedWhenPHP8",
919
"message": "Function libxml_disable_entity_loader() is deprecated",

src/DependencyInjection/Compiler/ExcludeMonologChannelPass.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,24 @@ public function process(ContainerBuilder $container): void
1919
/** @var string[] $exclusiveHandlerNames */
2020
$exclusiveHandlerNames = [];
2121

22-
/** @var array<string, array{type: string, elements: list<string>}> $handlersToChannels */
22+
/** @var array<string, array{type: string, elements: list<string>}|null> $handlersToChannels */
2323
$handlersToChannels = $container->getParameter('monolog.handlers_to_channels');
2424
foreach ($handlersToChannels as $id => &$handlersToChannel) {
25-
if ('exclusive' !== $handlersToChannel['type']) {
25+
if (null === $handlersToChannel) {
26+
$handlersToChannel = [
27+
'type' => 'exclusive',
28+
'elements' => [],
29+
];
30+
} elseif ('exclusive' !== $handlersToChannel['type']) {
2631
continue;
2732
}
2833

2934
if (false !== $index = array_search('!'.$monologChannelName, $handlersToChannel['elements'], true)) {
3035
array_splice($handlersToChannel['elements'], $index, 1);
31-
} else {
36+
if (!$handlersToChannel['elements']) {
37+
$handlersToChannel = null;
38+
}
39+
} elseif (!\in_array($monologChannelName, $handlersToChannel['elements'], true)) {
3240
$handlersToChannel['elements'][] = $monologChannelName;
3341
$exclusiveHandlerNames[] = substr($id, 16);
3442
}

tests/BizkitLoggableCommandBundleTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
1010
use Symfony\Bundle\MonologBundle\MonologBundle;
1111
use Symfony\Component\DependencyInjection\ContainerBuilder;
12+
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
1213

1314
/**
1415
* @covers \Bizkit\LoggableCommandBundle\BizkitLoggableCommandBundle
@@ -19,8 +20,10 @@ public function testCompilerPassIsRegisteredWithCorrectPriority(): void
1920
{
2021
$container = new ContainerBuilder();
2122

22-
(new MonologBundle())->build($container);
23-
(new BizkitLoggableCommandBundle())->build($container);
23+
/** @var BundleInterface $bundle */
24+
foreach ([new MonologBundle(), new BizkitLoggableCommandBundle()] as $bundle) {
25+
$bundle->build($container);
26+
}
2427

2528
$compilerPassIndexes = [];
2629
foreach ($container->getCompilerPassConfig()->getBeforeOptimizationPasses() as $i => $compilerPass) {

tests/DependencyInjection/Compiler/ExcludeMonologChannelPassTest.php

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
namespace Bizkit\LoggableCommandBundle\Tests\DependencyInjection\Compiler;
66

7+
use Bizkit\LoggableCommandBundle\BizkitLoggableCommandBundle;
78
use Bizkit\LoggableCommandBundle\DependencyInjection\Compiler\ExcludeMonologChannelPass;
89
use Bizkit\LoggableCommandBundle\Tests\TestCase;
10+
use Symfony\Bundle\MonologBundle\MonologBundle;
911
use Symfony\Component\DependencyInjection\ContainerBuilder;
12+
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
1013

1114
/**
1215
* @covers \Bizkit\LoggableCommandBundle\DependencyInjection\Compiler\ExcludeMonologChannelPass
@@ -16,27 +19,52 @@ final class ExcludeMonologChannelPassTest extends TestCase
1619
/**
1720
* @dataProvider handlerChannels
1821
*/
19-
public function testChannelIsExcludedWhenExpected(?array $channels, array $expectedChannels, array $expectedLog): void
22+
public function testChannelIsExcludedWhenExpected(?array $channels, ?array $expectedChannels, array $expectedLog): void
2023
{
2124
$container = new ContainerBuilder();
22-
$container->setParameter('bizkit_loggable_command.channel_name', 'channel_name');
23-
$container->setParameter('monolog.handlers_to_channels', ['monolog.handler.foobar' => $channels]);
25+
$container->setParameter('kernel.logs_dir', __DIR__);
26+
$container->setParameter('kernel.environment', 'dev');
2427

25-
(new ExcludeMonologChannelPass())->process($container);
28+
/** @var BundleInterface $bundle */
29+
foreach ([new MonologBundle(), new BizkitLoggableCommandBundle()] as $bundle) {
30+
$container->registerExtension($extension = $bundle->getContainerExtension());
31+
$bundle->build($container);
32+
$container->loadFromExtension($extension->getAlias());
33+
}
2634

27-
/** @var array<string, array{type: string, elements: list<string>}> $handlersToChannels */
35+
$container->loadFromExtension('monolog', [
36+
'handlers' => [
37+
'foobar' => [
38+
'type' => 'stream',
39+
'channels' => $channels,
40+
],
41+
],
42+
]);
43+
44+
$container->compile();
45+
46+
/** @var array<string, array{type: string, elements: list<string>}|null> $handlersToChannels */
2847
$handlersToChannels = $container->getParameter('monolog.handlers_to_channels');
29-
$this->assertSame($expectedChannels, $handlersToChannels['monolog.handler.foobar']['elements']);
48+
$this->assertSame($expectedChannels, $handlersToChannels['monolog.handler.foobar']);
3049

31-
$this->assertSame($expectedLog, $container->getCompiler()->getLog());
50+
$this->assertSame($expectedLog, array_values(array_filter(
51+
$container->getCompiler()->getLog(),
52+
function (string $log): bool {
53+
return 0 === strpos($log, ExcludeMonologChannelPass::class);
54+
}
55+
)));
3256
}
3357

3458
public function handlerChannels(): iterable
3559
{
36-
$log = sprintf('%s: Excluded Monolog channel "channel_name" from the following exclusive handlers "foobar".', ExcludeMonologChannelPass::class);
60+
$log = sprintf('%s: Excluded Monolog channel "loggable_output" from the following exclusive handlers "foobar".', ExcludeMonologChannelPass::class);
3761

38-
yield 'Inclusive' => [['type' => 'inclusive', 'elements' => ['foo', 'bar', 'baz']], ['foo', 'bar', 'baz'], []];
39-
yield 'Exclusive without exception' => [['type' => 'exclusive', 'elements' => ['foo', 'baz']], ['foo', 'baz', 'channel_name'], [$log]];
40-
yield 'Exclusive with exception' => [['type' => 'exclusive', 'elements' => ['foo', '!channel_name', 'baz']], ['foo', 'baz'], []];
62+
yield 'None' => [null, ['type' => 'exclusive', 'elements' => ['loggable_output']], [$log]];
63+
yield 'Empty array' => [[], ['type' => 'exclusive', 'elements' => ['loggable_output']], [$log]];
64+
yield 'Inclusive' => [['app'], ['type' => 'inclusive', 'elements' => ['app']], []];
65+
yield 'Exclusive without exception' => [['!event'], ['type' => 'exclusive', 'elements' => ['event', 'loggable_output']], [$log]];
66+
yield 'Exclusive with exception' => [['!event', '!!loggable_output'], ['type' => 'exclusive', 'elements' => ['event']], []];
67+
yield 'Exclusive with only an exception' => [['!!loggable_output'], null, []];
68+
yield 'Explicitly excluded' => [['!loggable_output'], ['type' => 'exclusive', 'elements' => ['loggable_output']], []];
4169
}
4270
}

0 commit comments

Comments
 (0)