Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using OutputInterface::VERBOSITY_* constants in PHP config #439

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion DependencyInjection/Configuration.php
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Console\Output\OutputInterface;

/**
* This class contains the configuration information for the bundle
Expand Down Expand Up @@ -1015,10 +1016,13 @@ private function addVerbosityLevelSection(ArrayNodeDefinition $handerNode)
->then(function ($v) {
$map = [];
$verbosities = ['VERBOSITY_QUIET', 'VERBOSITY_NORMAL', 'VERBOSITY_VERBOSE', 'VERBOSITY_VERY_VERBOSE', 'VERBOSITY_DEBUG'];
// allow numeric indexed array with ascendning verbosity and lowercase names of the constants
$verbosityConstants = [OutputInterface::VERBOSITY_QUIET, OutputInterface::VERBOSITY_NORMAL, OutputInterface::VERBOSITY_VERBOSE, OutputInterface::VERBOSITY_VERY_VERBOSE, OutputInterface::VERBOSITY_DEBUG];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The array should look like [OutputInterface::VERBOSITY_QUIET => 'VERBOSITY_QUIET', ...] to remove the array_search and make the code more readable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can make the code a little clearer with this changes: HeahDude#1

// allow numeric indexed array with ascending verbosity and lowercase names of the constants
foreach ($v as $verbosity => $level) {
if (is_int($verbosity) && isset($verbosities[$verbosity])) {
$map[$verbosities[$verbosity]] = strtoupper($level);
} elseif (is_int($verbosity) && false !== $i = \array_search($verbosity, $verbosityConstants, true)) {
$map[$verbosities[$i]] = strtoupper($level);
} else {
$map[strtoupper($verbosity)] = strtoupper($level);
}
Expand Down
51 changes: 38 additions & 13 deletions Tests/DependencyInjection/ConfigurationTest.php
Expand Up @@ -267,18 +267,17 @@ public function testWithTelegramBotHandler()
$this->assertEquals('-100', $config['handlers']['telegram']['channel']);
}

public function testWithConsoleHandler()
/**
* @dataProvider provideConsoleHandlerCases
*/
public function testWithConsoleHandler(array $verbosityLevels, array $expectedVerbosityMap)
{
$configs = [
[
'handlers' => [
'console' => [
'type' => 'console',
'verbosity_levels' => [
'VERBOSITY_NORMAL' => 'NOTICE',
'verbosity_verbose' => 'info',
'VERBOSITY_very_VERBOSE' => '200'
]
'verbosity_levels' => $verbosityLevels
]
]
]
Expand All @@ -287,13 +286,39 @@ public function testWithConsoleHandler()
$config = $this->process($configs);

$this->assertSame('console', $config['handlers']['console']['type']);
$this->assertSame([
OutputInterface::VERBOSITY_NORMAL => Logger::NOTICE,
OutputInterface::VERBOSITY_VERBOSE => Logger::INFO,
OutputInterface::VERBOSITY_VERY_VERBOSE => 200,
OutputInterface::VERBOSITY_QUIET => Logger::ERROR,
OutputInterface::VERBOSITY_DEBUG => Logger::DEBUG
], $config['handlers']['console']['verbosity_levels']);
$this->assertSame($expectedVerbosityMap, $config['handlers']['console']['verbosity_levels']);
}

public function provideConsoleHandlerCases(): iterable
{
yield 'with strings only' => [
[
'VERBOSITY_NORMAL' => 'NOTICE',
'verbosity_verbose' => 'info',
'VERBOSITY_very_VERBOSE' => '200'
],
[
OutputInterface::VERBOSITY_NORMAL => Logger::NOTICE,
OutputInterface::VERBOSITY_VERBOSE => Logger::INFO,
OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::INFO,
OutputInterface::VERBOSITY_QUIET => Logger::ERROR,
OutputInterface::VERBOSITY_DEBUG => Logger::DEBUG
]
];
yield 'with constants' => [
[
OutputInterface::VERBOSITY_NORMAL => Logger::NOTICE,
'verbosity_verbose' => 'info',
OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::INFO
],
[
OutputInterface::VERBOSITY_NORMAL => Logger::NOTICE,
OutputInterface::VERBOSITY_VERBOSE => Logger::INFO,
OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::INFO,
OutputInterface::VERBOSITY_QUIET => Logger::ERROR,
OutputInterface::VERBOSITY_DEBUG => Logger::DEBUG
]
];
}

public function testWithType()
Expand Down