Skip to content

Commit

Permalink
Fix case when handler has no channels configured
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC committed Feb 20, 2023
1 parent cfb79e9 commit 50c3d0c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 16 deletions.
10 changes: 10 additions & 0 deletions phpunit-deprecation-baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 11 additions & 3 deletions src/DependencyInjection/Compiler/ExcludeMonologChannelPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,24 @@ public function process(ContainerBuilder $container): void
/** @var string[] $exclusiveHandlerNames */
$exclusiveHandlerNames = [];

/** @var array<string, array{type: string, elements: list<string>}> $handlersToChannels */
/** @var array<string, array{type: string, elements: list<string>}|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);
}
Expand Down
7 changes: 5 additions & 2 deletions tests/BizkitLoggableCommandBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<string, array{type: string, elements: list<string>}> $handlersToChannels */
$container->loadFromExtension('monolog', [
'handlers' => [
'foobar' => [
'type' => 'stream',
'channels' => $channels,
],
],
]);

$container->compile();

/** @var array<string, array{type: string, elements: list<string>}|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']], []];
}
}

0 comments on commit 50c3d0c

Please sign in to comment.