Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ksucherek committed Dec 5, 2024
1 parent 9c802f1 commit f294a9f
Showing 1 changed file with 50 additions and 4 deletions.
54 changes: 50 additions & 4 deletions test/src/Ouzo/Core/Logger/LoggerAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f294a9f

Please sign in to comment.