Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/ZM/Logger/ConsoleLogger.php
  • Loading branch information
crazywhalecc committed Nov 5, 2022
2 parents 92d2812 + 2f584c0 commit 5158a0e
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 2 deletions.
32 changes: 32 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -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>
18 changes: 16 additions & 2 deletions src/ZM/Logger/TablePrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,28 @@ public function setBorderWidth(int $border_width): TablePrinter
public function fetchTerminalSize(): int
{
if (!isset($this->terminal_size)) {
/* @phpstan-ignore-next-line */
if (STDIN === false) {
return $this->terminal_size = 79;
}
$size = exec('stty size 2>/dev/null');
$size = 0;
if (DIRECTORY_SEPARATOR === '\\') {
exec('mode con', $out);
foreach ($out as $v) {
if (strpos($v, 'Columns:') !== false) {
$num = trim(explode('Columns:', $v)[1]);
$size = intval($num);
break;
}
}
} else {
$size = exec('stty size 2>/dev/null');
$size = (int) explode(' ', trim($size))[1];
}
if (empty($size)) {
return $this->terminal_size = 79;
}
return $this->terminal_size = (int) explode(' ', trim($size))[1];
return $this->terminal_size = $size;
}
return $this->terminal_size;
}
Expand Down
95 changes: 95 additions & 0 deletions tests/ConsoleLoggerTest.php
Original file line number Diff line number Diff line change
@@ -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 changes: 30 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 5158a0e

Please sign in to comment.