Skip to content

Commit

Permalink
Don't use the deprecated LoggerInterfaceTest class
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander M. Turek <[email protected]>
  • Loading branch information
derrabus committed Jul 18, 2021
1 parent 9738e49 commit 1b0d5e5
Showing 1 changed file with 121 additions and 7 deletions.
128 changes: 121 additions & 7 deletions tests/Monolog/PsrLogCompatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,130 @@

namespace Monolog;

use Monolog\Handler\TestHandler;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\TestHandler;
use Monolog\Processor\PsrLogMessageProcessor;
use Psr\Log\Test\LoggerInterfaceTest;
use PHPUnit\Framework\TestCase;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

class PsrLogCompatTest extends LoggerInterfaceTest
class PsrLogCompatTest extends TestCase
{
/**
* @var TestHandler
*/
private $handler;

public function getLogger()
public function testImplements(): void
{
$this->assertInstanceOf(LoggerInterface::class, $this->getLogger());
}

/**
* @dataProvider provideLevelsAndMessages
*/
public function testLogsAtAllLevels(string $level, string $message): void
{
$logger = $this->getLogger();
$logger->{$level}($message, ['user' => 'Bob']);
$logger->log($level, $message, ['user' => 'Bob']);

$expected = [
$level.' message of level '.$level.' with context: Bob',
$level.' message of level '.$level.' with context: Bob',
];
$this->assertEquals($expected, $this->getLogs());
}

public function provideLevelsAndMessages(): array
{
return [
LogLevel::EMERGENCY => [LogLevel::EMERGENCY, 'message of level emergency with context: {user}'],
LogLevel::ALERT => [LogLevel::ALERT, 'message of level alert with context: {user}'],
LogLevel::CRITICAL => [LogLevel::CRITICAL, 'message of level critical with context: {user}'],
LogLevel::ERROR => [LogLevel::ERROR, 'message of level error with context: {user}'],
LogLevel::WARNING => [LogLevel::WARNING, 'message of level warning with context: {user}'],
LogLevel::NOTICE => [LogLevel::NOTICE, 'message of level notice with context: {user}'],
LogLevel::INFO => [LogLevel::INFO, 'message of level info with context: {user}'],
LogLevel::DEBUG => [LogLevel::DEBUG, 'message of level debug with context: {user}'],
];
}

public function testThrowsOnInvalidLevel(): void
{
$logger = $this->getLogger();

$this->expectException(InvalidArgumentException::class);
$logger->log('invalid level', 'Foo');
}

public function testContextReplacement(): void
{
$logger = $this->getLogger();
$logger->info('{Message {nothing} {user} {foo.bar} a}', ['user' => 'Bob', 'foo.bar' => 'Bar']);

$expected = ['info {Message {nothing} Bob Bar a}'];
$this->assertEquals($expected, $this->getLogs());
}

public function testObjectCastToString(): void
{
$dummy = new class {
public function __toString(): string
{
return 'DUMMY';
}
};

$this->getLogger()->warning($dummy);

$expected = ['warning DUMMY'];
$this->assertEquals($expected, $this->getLogs());
}

public function testContextCanContainAnything(): void
{
$closed = fopen('php://memory', 'r');
fclose($closed);

$context = [
'bool' => true,
'null' => null,
'string' => 'Foo',
'int' => 0,
'float' => 0.5,
'nested' => ['with object' => new class {
public function __toString()
{
return 'DummyTest';
}
}],
'object' => new \DateTime,
'resource' => fopen('php://memory', 'r'),
'closed' => $closed,
];

$this->getLogger()->warning('Crazy context data', $context);

$expected = ['warning Crazy context data'];
$this->assertEquals($expected, $this->getLogs());
}

public function testContextExceptionKeyCanBeExceptionOrOtherValues(): void
{
$logger = $this->getLogger();
$logger->warning('Random message', ['exception' => 'oops']);
$logger->critical('Uncaught Exception!', ['exception' => new \LogicException('Fail')]);

$expected = [
'warning Random message',
'critical Uncaught Exception!'
];
$this->assertEquals($expected, $this->getLogs());
}

private function getLogger(): Logger
{
$logger = new Logger('foo');
$logger->pushHandler($handler = new TestHandler);
Expand All @@ -32,10 +146,10 @@ public function getLogger()
return $logger;
}

public function getLogs()
private function getLogs(): array
{
$convert = function ($record) {
$lower = function ($match) {
$convert = static function ($record) {
$lower = static function ($match) {
return strtolower($match[0]);
};

Expand Down

0 comments on commit 1b0d5e5

Please sign in to comment.