From 37c4c2a87d3c7c0cc897282447fec618c3d73e38 Mon Sep 17 00:00:00 2001 From: sunxyw <31698606+sunxyw@users.noreply.github.com> Date: Sun, 15 May 2022 00:39:48 +0800 Subject: [PATCH 1/4] add console logger tests (#3) --- phpunit.xml.dist | 32 +++++++++++++ tests/ConsoleLoggerTest.php | 95 +++++++++++++++++++++++++++++++++++++ tests/TestCase.php | 30 ++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 phpunit.xml.dist create mode 100644 tests/ConsoleLoggerTest.php create mode 100644 tests/TestCase.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..0619b4e --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,32 @@ + + + + + ./tests + + + + + ./src/ZM/Logger + + + + + + + + + + diff --git a/tests/ConsoleLoggerTest.php b/tests/ConsoleLoggerTest.php new file mode 100644 index 0000000..60316bc --- /dev/null +++ b/tests/ConsoleLoggerTest.php @@ -0,0 +1,95 @@ +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'], + ]; + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..c5b6beb --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,30 @@ +addLogCallback(function ($level, $output, $message, $context) { + $this->logs[] = $output; + return false; + }); + return $logger; + } + + protected function getLogs(): array + { + return $this->logs; + } +} From af263a9d3b2d4d964994d965bc2d1e85901e6119 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sat, 14 May 2022 23:36:57 +0800 Subject: [PATCH 2/4] Fix ext-json warning --- src/ZM/Logger/ConsoleLogger.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ZM/Logger/ConsoleLogger.php b/src/ZM/Logger/ConsoleLogger.php index 8d0de4f..65b8ec7 100644 --- a/src/ZM/Logger/ConsoleLogger.php +++ b/src/ZM/Logger/ConsoleLogger.php @@ -219,7 +219,10 @@ private function stringify($item): string case is_string($item): return $item; case is_array($item): - return 'array' . json_encode($item, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_LINE_TERMINATORS); + if (extension_loaded('json')) { + return 'array' . json_encode($item, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_LINE_TERMINATORS); + } + return var_export($item, true); case is_object($item): return get_class($item); case is_resource($item): From cc43c4729f066947784b51f6af38fa996bd5e5f6 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Mon, 22 Aug 2022 13:07:48 +0800 Subject: [PATCH 3/4] update to 1.0.1 (add windows support) --- src/ZM/Logger/ConsoleLogger.php | 2 +- src/ZM/Logger/TablePrinter.php | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/ZM/Logger/ConsoleLogger.php b/src/ZM/Logger/ConsoleLogger.php index 65b8ec7..1a6e1a6 100644 --- a/src/ZM/Logger/ConsoleLogger.php +++ b/src/ZM/Logger/ConsoleLogger.php @@ -10,7 +10,7 @@ class ConsoleLogger extends AbstractLogger { - public const VERSION = '1.0.0-alpha'; + public const VERSION = '1.0.1'; /** * 日志输出格式 diff --git a/src/ZM/Logger/TablePrinter.php b/src/ZM/Logger/TablePrinter.php index af327e0..f4f9d3a 100644 --- a/src/ZM/Logger/TablePrinter.php +++ b/src/ZM/Logger/TablePrinter.php @@ -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; } From 2f584c0f0c54cef92628c81d00323f0cd9b10b54 Mon Sep 17 00:00:00 2001 From: Jerry Ma Date: Thu, 22 Sep 2022 22:37:41 +0800 Subject: [PATCH 4/4] Update ConsoleLogger.php --- src/ZM/Logger/ConsoleLogger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ZM/Logger/ConsoleLogger.php b/src/ZM/Logger/ConsoleLogger.php index 1a6e1a6..7b60931 100644 --- a/src/ZM/Logger/ConsoleLogger.php +++ b/src/ZM/Logger/ConsoleLogger.php @@ -10,7 +10,7 @@ class ConsoleLogger extends AbstractLogger { - public const VERSION = '1.0.1'; + public const VERSION = '1.0.2'; /** * 日志输出格式