diff --git a/test/src/Ouzo/Core/Logger/LoggerAdapterTest.php b/test/src/Ouzo/Core/Logger/LoggerAdapterTest.php index aa8480ad..414b94a6 100644 --- a/test/src/Ouzo/Core/Logger/LoggerAdapterTest.php +++ b/test/src/Ouzo/Core/Logger/LoggerAdapterTest.php @@ -12,15 +12,18 @@ use Ouzo\Tests\Mock\MockInterface; use Ouzo\Tests\StreamStub; use Ouzo\Utilities\Clock; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Psr\Log\AbstractLogger; use Psr\Log\LoggerInterface; +use Psr\Log\LogLevel; class LoggerAdapterTest extends TestCase { private LoggerAdapter $logger; + protected function setUp(): void { parent::setUp(); @@ -164,21 +167,64 @@ public function shouldIgnoreDebugMessageIfDebugIsOff() } #[Test] - public function shouldGetAsInstanceOfLoggerInterface(): void + public function shouldGetLoggerInterface(): void + { + // given + $loggerInterface = $this->logger->asLoggerInterface(); + + // when + // then + Assert::that($loggerInterface)->isInstanceOf(LoggerInterface::class); + } + + #[Test] + #[DataProvider('logLevels')] + public function shouldWriteLoggingIsDelegatedToLoggerInterfaceLogWhenLogIsCalled(string $logLevel): void { // given Config::overrideProperty('debug')->with(true); $loggerInterface = $this->logger->asLoggerInterface(); + // when - $loggerInterface->debug('My debug log line without params.'); + $loggerInterface->log($logLevel, 'My log line'); // then - Assert::that($loggerInterface)->isInstanceOf(LoggerInterface::class); $logContent = $this->readStreamContent('test://stdout'); - Assert::thatString($logContent)->contains('2014-01-01 11:11:11: TEST debug: [ID: ] My debug log line without params.'); + Assert::thatString($logContent)->contains("2014-01-01 11:11:11: TEST $logLevel: [ID: ] My log line"); + } + #[Test] + #[DataProvider('logLevels')] + public function shouldWriteLogWhenLoggingIsDelegatedToLoggerInterfaceAndAppropriateMethodIsCalled(string $logLevel): void + { + // given + Config::overrideProperty('debug')->with(true); + $loggerInterface = $this->logger->asLoggerInterface(); + + // when + $loggerInterface->$logLevel('My log line'); + + // then + $logContent = $this->readStreamContent('test://stdout'); + Assert::thatString($logContent)->contains("2014-01-01 11:11:11: TEST $logLevel: [ID: ] My log line"); } + public static function logLevels(): array + { + return [ + [LogLevel::INFO], + [LogLevel::DEBUG], + [LogLevel::ALERT], + [LogLevel::CRITICAL], + [LogLevel::ERROR], + [LogLevel::WARNING], + [LogLevel::NOTICE], + [LogLevel::EMERGENCY], + ]; + + } + + private function readStreamContent(string $streamFile): string { return file_get_contents($streamFile);