From cdc48b2f45a73c9df871ec3a3553681d5ea86b0e Mon Sep 17 00:00:00 2001 From: Timothy Younger Date: Sun, 26 Jan 2020 20:25:42 -0700 Subject: [PATCH] test(configurable-levels): adjust coverage anotations and bump coverage back up to 100% --- test/LogLevelConfigurationTest.php | 95 +++++++++++++++++++ .../PsrSqlLoggerConfigurableLogLevelsTest.php | 50 +++++++++- 2 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 test/LogLevelConfigurationTest.php diff --git a/test/LogLevelConfigurationTest.php b/test/LogLevelConfigurationTest.php new file mode 100644 index 0000000..926ff72 --- /dev/null +++ b/test/LogLevelConfigurationTest.php @@ -0,0 +1,95 @@ +sut = new LogLevelConfiguration([ + LogLevel::DEBUG => 0, + LogLevel::INFO => 10, + LogLevel::NOTICE => 20, + LogLevel::WARNING => 30, + LogLevel::ERROR => 40, + LogLevel::CRITICAL => 50, + LogLevel::ALERT => 60, + LogLevel::EMERGENCY => 70, + ]); + } + + public static function dataValidLevels(): array + { + return [ + [-1, null], + [0, null], + [1, LogLevel::DEBUG], + [9, LogLevel::DEBUG], + [10, LogLevel::INFO], + [11, LogLevel::INFO], + [19, LogLevel::INFO], + [20, LogLevel::NOTICE], + [21, LogLevel::NOTICE], + [29, LogLevel::NOTICE], + [30, LogLevel::WARNING], + [31, LogLevel::WARNING], + [39, LogLevel::WARNING], + [40, LogLevel::ERROR], + [41, LogLevel::ERROR], + [49, LogLevel::ERROR], + [50, LogLevel::CRITICAL], + [51, LogLevel::CRITICAL], + [59, LogLevel::CRITICAL], + [60, LogLevel::ALERT], + [61, LogLevel::ALERT], + [69, LogLevel::ALERT], + [70, LogLevel::EMERGENCY], + [71, LogLevel::EMERGENCY], + [79, LogLevel::EMERGENCY], + ]; + } + + /** + * @dataProvider dataValidLevels + * @param float $durationMs + * @param string | null $expected + */ + public function testValidLevels( + float $durationMs, + ?string $expected + ): void { + $actual = $this->sut->getApplicableLogLevel($durationMs / 1000); + + self::assertSame($expected, $actual); + } + + public function testInvalidLogLevel(): void + { + self::expectException(InvalidArgumentException::class); + self::expectExceptionMessage('invalid LogLevel detected: "InvalidLevel", please choose from: "' . print_r([ + 'EMERGENCY' => 'emergency', + 'ALERT' => 'alert', + 'CRITICAL' => 'critical', + 'ERROR' => 'error', + 'WARNING' => 'warning', + 'NOTICE' => 'notice', + 'INFO' => 'info', + 'DEBUG' => 'debug', + ], true) . '"'); + + new LogLevelConfiguration([ + 'InvalidLevel' => 1000, + ]); + } +} diff --git a/test/PsrSqlLoggerConfigurableLogLevelsTest.php b/test/PsrSqlLoggerConfigurableLogLevelsTest.php index 171d0cd..edaad16 100644 --- a/test/PsrSqlLoggerConfigurableLogLevelsTest.php +++ b/test/PsrSqlLoggerConfigurableLogLevelsTest.php @@ -4,6 +4,7 @@ use Abacaphiliac\Doctrine\LogLevelConfiguration; use Abacaphiliac\Doctrine\PsrSqlLoggerConfigurableLogLevels; +use Psr\Log\LoggerInterface; use Psr\Log\Test\TestLogger; use InvalidArgumentException; use PHPUnit\Framework\TestCase; @@ -13,7 +14,7 @@ use function usleep; /** - * @covers \Abacaphiliac\Doctrine\PsrSqlLogger + * @covers \Abacaphiliac\Doctrine\PsrSqlLoggerConfigurableLogLevels */ class PsrSqlLoggerConfigurableLogLevelsTest extends TestCase { @@ -118,6 +119,53 @@ public function testInvalidLogLevelUsedInConfiguration() : void ); } + public function testInvalidDefaultLogLevel() : void + { + $this->expectException(InvalidArgumentException::class); + new PsrSqlLoggerConfigurableLogLevels( + new class implements LoggerInterface { + public function emergency($message, array $context = array()) + { + } + + public function alert($message, array $context = array()) + { + } + + public function critical($message, array $context = array()) + { + } + + public function error($message, array $context = array()) + { + } + + public function warning($message, array $context = array()) + { + } + + public function notice($message, array $context = array()) + { + } + + public function info($message, array $context = array()) + { + } + + public function debug($message, array $context = array()) + { + } + + public function log($level, $message, array $context = array()) + { + } + }, + new LogLevelConfiguration([ + ]), + 'InvalidLevel' + ); + } + protected function setUp() { $this->logger = new TestLogger();