add console logger tests (#3)

This commit is contained in:
sunxyw 2022-05-15 00:39:48 +08:00 committed by GitHub
parent 2afe08d396
commit 37c4c2a87d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 157 additions and 0 deletions

32
phpunit.xml.dist Normal file
View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertDeprecationsToExceptions="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
testdox="true"
verbose="true"
>
<testsuites>
<testsuite name="Zhamao Logger Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src/ZM/Logger</directory>
</include>
<report>
<html outputDirectory="./build/html-coverage"/>
<clover outputFile="./build/coverage.xml"/>
</report>
</coverage>
<php>
<env name="APP_ENV" value="testing"/>
</php>
</phpunit>

View File

@ -0,0 +1,95 @@
<?php
declare(strict_types=1);
namespace Tests;
use Closure;
use InvalidArgumentException;
use Psr\Log\LogLevel;
class ConsoleLoggerTest extends TestCase
{
/**
* @dataProvider provideCanLogAtAllLevels
*/
public function testCanLogAtAllLevels($level, $message): void
{
$expected = [
"this is a {$level} message for testing",
"this is a {$level} message for testing",
];
$logger = $this->getLogger();
$logger->{$level}($message);
$logger->log($level, $message);
$this->assertSame($expected, $this->getLogs());
}
public function provideCanLogAtAllLevels(): array
{
return [
LogLevel::EMERGENCY => [LogLevel::EMERGENCY, 'this is a emergency message for testing'],
LogLevel::ALERT => [LogLevel::ALERT, 'this is a alert message for testing'],
LogLevel::CRITICAL => [LogLevel::CRITICAL, 'this is a critical message for testing'],
LogLevel::ERROR => [LogLevel::ERROR, 'this is a error message for testing'],
LogLevel::WARNING => [LogLevel::WARNING, 'this is a warning message for testing'],
LogLevel::NOTICE => [LogLevel::NOTICE, 'this is a notice message for testing'],
LogLevel::INFO => [LogLevel::INFO, 'this is a info message for testing'],
LogLevel::DEBUG => [LogLevel::DEBUG, 'this is a debug message for testing'],
];
}
public function testWillThrowsOnInvalidLevel(): void
{
$this->expectException(InvalidArgumentException::class);
$logger = $this->getLogger();
$logger->log('invalid', 'this is a message for testing');
}
public function testCanReplaceContext(): void
{
$logger = $this->getLogger();
$logger->info('this is a {message} with {nothing}', ['message' => 'info message for testing']);
$this->assertSame(['this is a info message for testing with {nothing}'], $this->getLogs());
}
public function testCanCastObjectToString(): void
{
$string = uniqid('DUMMY', true);
$dummy = $this->createMock(\Stringable::class);
$dummy->expects($this->once())->method('__toString')->willReturn($string);
$this->getLogger()->info($dummy);
$this->assertSame([$string], $this->getLogs());
}
/**
* @dataProvider provideTestCanContainAnythingInContext
*/
public function testCanContainAnythingInContext($context, $expected): void
{
$logger = $this->getLogger();
$logger->info('{context}', ['context' => $context]);
$this->assertSame([$expected], $this->getLogs());
}
public function provideTestCanContainAnythingInContext(): array
{
return [
'callable' => [[new ConsoleLoggerTest(), 'testCanContainAnythingInContext'], self::class . '@testCanContainAnythingInContext'],
'closure' => [Closure::fromCallable([$this, 'testCanContainAnythingInContext']), 'closure'],
'string' => ['string', 'string'],
'array' => [['123', '42', 'hello', 122], 'array["123","42","hello",122]'],
'object' => [new \stdClass(), 'stdClass'],
'resource' => [fopen('php://memory', 'rb'), 'resource(stream)'],
'null' => [null, 'null'],
'boolean 1' => [true, 'true'],
'boolean 2' => [false, 'false'],
'float' => [123.456, '123.456'],
'integer' => [123, '123'],
];
}
}

30
tests/TestCase.php Normal file
View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Tests;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use ZM\Logger\ConsoleLogger;
class TestCase extends \PHPUnit\Framework\TestCase
{
protected $logs = [];
protected function getLogger(): LoggerInterface
{
$logger = new ConsoleLogger(LogLevel::DEBUG);
$logger::$format = '%body%';
$logger->addLogCallback(function ($level, $output, $message, $context) {
$this->logs[] = $output;
return false;
});
return $logger;
}
protected function getLogs(): array
{
return $this->logs;
}
}