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

fix ConsoleFormatter - call to a member function format() on string #31326

Merged
merged 1 commit into from May 1, 2019
Merged
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
4 changes: 3 additions & 1 deletion src/Symfony/Bridge/Monolog/Formatter/ConsoleFormatter.php
Expand Up @@ -133,7 +133,9 @@ public function format(array $record)
}

$formatted = strtr($this->options['format'], [
'%datetime%' => $record['datetime']->format($this->options['date_format']),
'%datetime%' => $record['datetime'] instanceof \DateTimeInterface
? $record['datetime']->format($this->options['date_format'])
: $record['datetime'],
'%start_tag%' => sprintf('<%s>', $levelColor),
'%level_name%' => sprintf('%-9s', $record['level_name']),
'%end_tag%' => '</>',
Expand Down
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Monolog\Tests\Formatter;

use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Monolog\Formatter\ConsoleFormatter;

class ConsoleFormatterTest extends TestCase
{
/**
* @dataProvider providerFormatTests
*/
public function testFormat(array $record, $expectedMessage)
{
$formatter = new ConsoleFormatter();
self::assertSame($expectedMessage, $formatter->format($record));
}

/**
* @return array
*/
public function providerFormatTests()
{
$currentDateTime = new \DateTime();

return [
'record with DateTime object in datetime field' => [
'record' => [
'message' => 'test',
'context' => [],
'level' => Logger::WARNING,
'level_name' => Logger::getLevelName(Logger::WARNING),
'channel' => 'test',
'datetime' => $currentDateTime,
'extra' => [],
],
'expectedMessage' => sprintf(
"%s <fg=cyan>WARNING </> <comment>[test]</> test\n",
$currentDateTime->format(ConsoleFormatter::SIMPLE_DATE)
),
],
'record with string in datetime field' => [
'record' => [
'message' => 'test',
'context' => [],
'level' => Logger::WARNING,
'level_name' => Logger::getLevelName(Logger::WARNING),
'channel' => 'test',
'datetime' => '2019-01-01T00:42:00+00:00',
'extra' => [],
],
'expectedMessage' => "2019-01-01T00:42:00+00:00 <fg=cyan>WARNING </> <comment>[test]</> test\n",
],
];
}
}