Skip to content

Commit

Permalink
update local parameter to be an optionnal parameter of the bundle con…
Browse files Browse the repository at this point in the history
…fig and update tests (#18)
  • Loading branch information
Deamon authored Aug 11, 2017
1 parent da4dade commit c397149
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 14 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function getConfigTreeBuilder()
->arrayNode('application')->isRequired()
->children()
->scalarNode('name')->defaultNull()->cannotBeEmpty()->end()
->scalarNode('locale')->defaultNull()->end()
->end()
->end()
->arrayNode('handlers')->isRequired()
Expand Down
3 changes: 2 additions & 1 deletion DependencyInjection/DeamonLoggerExtraExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('processors.xml');

$definition = $container->getDefinition('deamon.logger_extra.context');
$definition->replaceArgument(1, $config['application']['name']);
$definition->replaceArgument(0, $config['application']['name']);
$definition->addArgument($config['application']['locale']);

$definition = $container->getDefinition('deamon.logger_extra.processors.web_processor');
$definition->replaceArgument(0, $config['config']);
Expand Down
6 changes: 3 additions & 3 deletions Processors/Monolog/DeamonLoggerExtraWebProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private function addUserInfo()
}

/**
* append method result of user object
* append method result of user object.
*
* @param $user
*/
Expand All @@ -151,7 +151,7 @@ private function appendUserMethodInfo($user)
}

/**
* Check if passed token is an instance of TokenInterface and an instance of config UserClass
* Check if passed token is an instance of TokenInterface and an instance of config UserClass.
*
* @param $token
*
Expand Down Expand Up @@ -182,7 +182,7 @@ private function addChannelInfo()
*/
private function addInfo($key, $value)
{
if ($this->configShowExtraInfo($key)) {
if ($this->configShowExtraInfo($key) && $value !== null) {
$this->record['extra'][$key] = $value;
}
}
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ deamon_logger_extra:
// app/config/config.yml
deamon_logger_extra:
application:
name: "loc-deamonfront" # default to null
name: "loc-deamonfront" # default to null
locale: "fr" # default to null
handlers: [default_info] # the only required field
config:
channel_prefix: "v0.1" # default to null
Expand Down
1 change: 0 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
</parameters>
<services>
<service id="deamon.logger_extra.context" class="%deamon.logger_extra.context.class%">
<argument type="string">%locale%</argument>
<argument />
</service>
</services>
Expand Down
4 changes: 2 additions & 2 deletions Services/DeamonLoggerExtraContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ class DeamonLoggerExtraContext
private $locale;
private $applicationName;

public function __construct($locale, $applicationName)
public function __construct($applicationName, $locale = null)
{
$this->locale = $locale;
$this->applicationName = $applicationName;
$this->locale = $locale;
}

public function getLocale()
Expand Down
7 changes: 6 additions & 1 deletion Tests/DependencyInjection/DeamonLoggerExtraExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public function testLoad()
$this->assertTrue($this->container->hasDefinition('deamon.logger_extra.processors.web_processor'));

$definition1 = $this->container->getDefinition('deamon.logger_extra.context');
$this->assertEquals('foo', $definition1->getArgument(1));
$this->assertEquals('foo', $definition1->getArgument(0));
$this->assertCount(2, $definition1->getArguments());
$this->assertEquals('fr', $definition1->getArgument(1));

$definition2 = $this->container->getDefinition('deamon.logger_extra.processors.web_processor');
$this->assertEquals($configs[0]['config'], $definition2->getArgument(0));
Expand Down Expand Up @@ -84,6 +86,8 @@ public function testDefaultValue()
$this->assertTrue($this->container->hasDefinition('deamon.logger_extra.processors.web_processor'));

$definition1 = $this->container->getDefinition('deamon.logger_extra.context');
$this->assertNull($definition1->getArgument(0));
$this->assertCount(2, $definition1->getArguments());
$this->assertNull($definition1->getArgument(1));

$definition2 = $this->container->getDefinition('deamon.logger_extra.processors.web_processor');
Expand Down Expand Up @@ -120,6 +124,7 @@ private function getValidConfigFull()
return [
'application' => [
'name' => 'foo',
'locale' => 'fr',
],
'handlers' => ['bar'],
'config' => [
Expand Down
25 changes: 23 additions & 2 deletions Tests/Processors/Monolog/DeamonLoggerExtraWebProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ public function testAddContextInfo()
$this->assertArrayHasKeyAndEquals('application', $record['extra'], 'foo_app');
}

/**
* @runInSeparateProcess
*/
public function testAddContextInfoWithoutLocale()
{
$config = $this->getDisplayConfig([
'env' => true,
'locale' => true,
'application_name' => true,
]);

$processor = new DeamonLoggerExtraWebProcessor($config);
$processor->setLoggerExtraContext($this->getLoggerExtraContext(null));
$processor->setEnvironment('env_foo');
$record = $processor->__invoke($this->getRecord());

$this->assertArrayHasKeyAndEquals('env', $record['extra'], 'env_foo');
$this->assertArrayNotHasKey('locale', $record['extra'], 'fr');
$this->assertArrayHasKeyAndEquals('application', $record['extra'], 'foo_app');
}

public function testAddRequestInfo()
{
$config = $this->getDisplayConfig(
Expand Down Expand Up @@ -226,9 +247,9 @@ private function getRequestStack()
return $stack;
}

private function getLoggerExtraContext()
private function getLoggerExtraContext($locale = 'fr')
{
return new DeamonLoggerExtraContext('fr', 'foo_app');
return new DeamonLoggerExtraContext('foo_app', $locale);
}

private function getTokenStorage(UserInterface $user = null)
Expand Down
6 changes: 3 additions & 3 deletions Tests/Services/DeamonLoggerExtraContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ class DeamonLoggerExtraContextTest extends TestCase
/**
* @dataProvider getLocaleDataset
*
* @param $locale
* @param string $locale
*/
public function testGetLocale($locale)
{
$context = new DeamonLoggerExtraContext($locale, '');
$context = new DeamonLoggerExtraContext('', $locale);
$this->assertEquals($locale, $context->getLocale(), sprintf('locale should be %s, %s returned.', $locale, $context->getLocale()));
}

Expand All @@ -33,7 +33,7 @@ public function getLocaleDataset()
*/
public function testGetApplicationName($applicationName)
{
$context = new DeamonLoggerExtraContext('fr', $applicationName);
$context = new DeamonLoggerExtraContext($applicationName, 'fr');
$this->assertEquals($applicationName, $context->getApplicationName(), sprintf('application_name should be %s, %s returned.', $applicationName, $context->getApplicationName()));
}

Expand Down

0 comments on commit c397149

Please sign in to comment.