Skip to content

Commit

Permalink
Append channel prefix only once (#29)
Browse files Browse the repository at this point in the history
* delete travis php5.5 job and add test before appending prefix
  • Loading branch information
Deamon committed Oct 7, 2019
1 parent 2c3ad89 commit 17a475e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
Empty file modified .travis.yml 100644 → 100755
Empty file.
6 changes: 4 additions & 2 deletions Processors/Monolog/DeamonLoggerExtraWebProcessor.php 100644 → 100755
Expand Up @@ -168,9 +168,11 @@ private function isUserInstanceValid(?TokenInterface $token): bool
*/
private function addChannelInfo(): void
{
$this->addInfo('global_channel', $this->record['channel']);
if(!array_key_exists('global_channel', $this->record['extra'])){
$this->addInfo('global_channel', $this->record['channel']);
}

if ($this->channelPrefix !== null) {
if ($this->channelPrefix !== null && substr($this->record['channel'], 0, strlen($this->channelPrefix)) !== $this->channelPrefix) {
$this->record['channel'] = sprintf('%s.%s', $this->channelPrefix, $this->record['channel']);
}
}
Expand Down
17 changes: 15 additions & 2 deletions Tests/Processors/Monolog/DeamonLoggerExtraWebProcessorTest.php 100644 → 100755
Expand Up @@ -14,6 +14,7 @@

class DeamonLoggerExtraWebProcessorTest extends TestCase
{

public function testProcessorWithNullContainer()
{
$processor = new DeamonLoggerExtraWebProcessor();
Expand Down Expand Up @@ -170,6 +171,18 @@ public function testAddChannelInfoWithChannelPrefix()
$this->assertArrayHasKeyAndEquals('channel', $record, sprintf('prefix.%s', $originalRecord['channel']));
}

public function testAppendChannelPrefixOnlyOnce()
{
$config = $this->getDisplayConfig(['global_channel' => true], 'prefix');
$processor = new DeamonLoggerExtraWebProcessor(new MyContainerForTests(), $config);
$originalRecord = $this->getRecord();
$record = $processor($originalRecord);
$recordReparsed = $processor($record);

$this->assertArrayHasKeyAndEquals('global_channel', $recordReparsed['extra'], $originalRecord['channel'], 'global_channel must be equals to the original channel without any prefix');
$this->assertArrayHasKeyAndEquals('channel', $recordReparsed, sprintf('prefix.%s', $originalRecord['channel']), 'channel must be equals to prefix.channel');
}

protected function getDisplayConfig($trueValues, $channelPrefix = null, $user_class = null, $user_methods = null)
{
if (null === $user_class) {
Expand Down Expand Up @@ -205,10 +218,10 @@ protected function getDisplayConfig($trueValues, $channelPrefix = null, $user_cl
];
}

protected function assertArrayHasKeyAndEquals($key, $array, $value)
protected function assertArrayHasKeyAndEquals($key, $array, $value, $message = null)
{
$this->assertArrayHasKey($key, $array);
$this->assertEquals($value, $array[$key]);
$this->assertEquals($value, $array[$key], $message);
}

/**
Expand Down

0 comments on commit 17a475e

Please sign in to comment.