From 15f3e59abf05b77417dd0918a4a818a9e5ef9fc8 Mon Sep 17 00:00:00 2001 From: 8ctopus Date: Mon, 20 Mar 2023 09:45:18 +0400 Subject: [PATCH] Cleanup tests --- tests/FunctionalTest.php | 16 ++++----- tests/Logger/TestCase.php | 69 --------------------------------------- 2 files changed, 8 insertions(+), 77 deletions(-) diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 83c85c9..ec9881c 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -42,7 +42,7 @@ public function getLogs(Logger\Runtime $logger, bool $deferred = false) : array ); } - return TestCase::normalizeLogs($lines); + return $lines; } public function testUsages() : void @@ -90,22 +90,22 @@ public function testUsages() : void $urgent_logs = $this->getLogs($urgent_logger); - static::assertSame('alert Running out of beers 5 left, recharge: true [type: resource]' . PHP_EOL, $urgent_logs[0]); + $date = date('[Y-m-d H:i:s]'); + + static::assertSame($date . ' ALERT Running out of beers 5 left, recharge: true [type: resource]' . PHP_EOL, $urgent_logs[0]); $prefixException = 'Exception: Boo! in '; - static::assertStringStartsWith('critical OMG saw ' . $prefixException, $urgent_logs[1]); + static::assertStringStartsWith($date . ' CRITICAL OMG saw ' . $prefixException, $urgent_logs[1]); $app_logger->flushDeferredLogs(); $app_logs = $this->getLogs($app_logger, true); - var_dump($app_logs); - - static::assertStringStartsWith('critical OMG saw ' . $prefixException, $app_logs[0]); + static::assertStringStartsWith($date . ' CRITICAL OMG saw ' . $prefixException, $app_logs[0]); - static::assertStringStartsWith('error ' . $prefixException, $app_logs[1]); + static::assertStringStartsWith($date . ' ERROR ' . $prefixException, $app_logs[1]); - static::assertSame(['info Something happened -> ["xyz"]' . PHP_EOL], $this->getLogs($debug_logger)); + static::assertSame([$date . ' INFO Something happened -> ["xyz"]' . PHP_EOL], $this->getLogs($debug_logger)); } } diff --git a/tests/Logger/TestCase.php b/tests/Logger/TestCase.php index 503a11b..b3b90ef 100644 --- a/tests/Logger/TestCase.php +++ b/tests/Logger/TestCase.php @@ -11,7 +11,6 @@ namespace Apix\Log\tests\Logger; use Apix\Log\Logger\LoggerInterface; -use Exception; use stdClass; abstract class TestCase extends \PHPUnit\Framework\TestCase implements LoggerInterface @@ -19,30 +18,6 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase implements LoggerInt protected string $dest = 'build/apix-unit-test-logger.log'; protected $logger; - public static function normalizeLogs(array $logs) : array - { - return array_map(function ($line) { - return preg_replace_callback( - '{^\[.+\] (\w+) (.+)?}', - function ($match) { - return strtolower($match[1]) . ' ' . (isset($match[2]) ? $match[2] : null); - }, - $line); - }, $logs); - } - - /** - * This must return the log messages in order with a simple formatting: " ". - * - * Example ->error('Foo') would yield "error Foo" - * - * @return array - */ - public function getLogs() : array - { - return self::normalizeLogs(file($this->dest, FILE_IGNORE_NEW_LINES)); - } - public function providerMessagesAndContextes() : array { $obj = new stdClass(); @@ -72,50 +47,6 @@ public function providerMessagesAndContextes() : array ]; } - /** - * @dataProvider providerMessagesAndContextes - * - * @param string $msg - * @param mixed $context - * @param string $exp - */ - public function testMessageWithContext(string $msg, mixed $context, string $exp) : void - { - $this->getLogger()->alert('{' . $msg . '}', [$msg => $context]); - - static::assertSame(['alert ' . $exp], $this->getLogs()); - } - - /** - * @dataProvider providerMessagesAndContextes - * - * @param string $msg - * @param mixed $context - * @param string $exp - */ - public function testContextIsPermutted(string $msg, mixed $context, string $exp) : void - { - $this->getLogger()->notice($context); - - static::assertSame(['notice ' . $exp], $this->getLogs()); - } - - public function testContextIsAnException() : void - { - $this->getLogger()->critical(new Exception('Boo!')); - - $logs = $this->getLogs(); - - $prefix = version_compare(PHP_VERSION, '7.0.0-dev', '>=') - ? 'critical Exception: Boo! in ' - : "critical exception 'Exception' with message 'Boo!' in "; - - static::assertStringStartsWith( - $prefix, - $logs[0] - ); - } - /** * {@inheritDoc} */