Skip to content

Commit

Permalink
Fixed tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bbankowski committed Oct 21, 2024
1 parent f17d7d3 commit d23dcf7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 36 deletions.
18 changes: 9 additions & 9 deletions src/Ouzo/Core/Logger/StdOutputLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ public function __construct(string $name, string $configuration, private string
{
}

public function log($level, $message, array $context = []): void
{
$stdOut = $this->getStreamForLogLevel($level);
$date = Clock::nowAsString();
$fileHandle = fopen($stdOut, 'a');
fwrite($fileHandle, "$date: $message\n");
fclose($fileHandle);
}

private function errorStreamName(): string
{
return "{$this->outputStreamIdentifier}://stderr";
Expand All @@ -32,13 +41,4 @@ private function getStreamForLogLevel(string $logLevel): string
}
return $this->errorStreamName();
}

public function log($level, $message, array $context = [])
{
$stdOut = $this->getStreamForLogLevel($level);
$date = Clock::nowAsString();
$fileHandle = fopen($stdOut, 'a');
fwrite($fileHandle, "$date: $message\n");
fclose($fileHandle);
}
}
2 changes: 1 addition & 1 deletion src/Ouzo/Core/Logger/SyslogLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __destruct()
closelog();
}

public function log($level, $message, array $context = [])
public function log($level, $message, array $context = []): void
{
$syslogLevel = LogLevelTranslator::toSyslogLevel($level);
if (!is_null($this->loggerConfiguration)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@
*/

use Ouzo\Config;
use Ouzo\Logger\LoggerAdapter;
use Ouzo\Logger\StdOutputLogger;
use Ouzo\Tests\Assert;
use Ouzo\Tests\StreamStub;
use Ouzo\Utilities\Clock;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\AbstractLogger;

class StdOutputLoggerTest extends TestCase
class LoggerAdapterTest extends TestCase
{
private LoggerInterface $logger;
private LoggerAdapter $logger;

protected function setUp(): void
{
parent::setUp();
Clock::freeze('2014-01-01 11:11:11');
StreamStub::register('test');
$this->logger = new StdOutputLogger('TEST', 'default', 'test');
$this->logger = new LoggerAdapter('TEST', 'default', new StdOutputLogger('TEST', 'default', 'test'));
}

protected function tearDown(): void
Expand All @@ -32,25 +33,49 @@ protected function tearDown(): void
parent::tearDown();
}

#[Test]
public function shouldSendContextWhenLoggingMessage()
{
//given
$logger = new class() extends AbstractLogger {
public ?string $currentMessage = null;
public array $currentContext = [];

public function log($level, $message, array $context = []): void
{
$this->currentMessage = $message;
$this->currentContext = $context;
}
};
$this->logger = new LoggerAdapter('TEST', 'default', $logger);

//when
$this->logger->info('Message', ['number' => 42, 'key' => 'value']);

//then
Assert::thatString($logger->currentMessage)->contains('TEST info: [ID: ] Message');
Assert::thatArray($logger->currentContext)->isEqualTo(['number' => 42, 'key' => 'value']);
}

#[Test]
public function shouldWriteErrorMessage()
{
//when
$this->logger->error('My error log line with param %s and %s.', [42, 'Zaphod']);
$this->logger->error('My error log line with param 42 and Zaphod.');

//then
$logContent = $this->_readStreamContent('test://stderr');
$logContent = $this->readStreamContent('test://stderr');
Assert::thatString($logContent)->contains('2014-01-01 11:11:11: TEST error: [ID: ] My error log line with param 42 and Zaphod.');
}

#[Test]
public function shouldWriteInfoMessage()
{
//when
$this->logger->info('My info log line with param %s and %s.', [42, 'Zaphod']);
$this->logger->info('My info log line with param 42 and Zaphod.');

//then
$logContent = $this->_readStreamContent('test://stdout');
$logContent = $this->readStreamContent('test://stdout');
Assert::thatString($logContent)->contains('2014-01-01 11:11:11: TEST info: [ID: ] My info log line with param 42 and Zaphod.');
}

Expand All @@ -59,13 +84,13 @@ public function shouldNotWriteInfoMessageIfMinimalLevelIsSetToWarning()
{
//given
Config::overrideProperty('logger', 'default', 'minimal_levels')->with(['TEST' => LOG_WARNING]);
$this->logger = new StdOutputLogger('TEST', 'default', 'test');
$this->logger = new LoggerAdapter('TEST', 'default', new StdOutputLogger('TEST', 'default', 'test'));

//when
$this->logger->info('My info log line with param %s and %s.', [42, 'Zaphod']);
$this->logger->info('My info log line with param 42 and Zaphod.');

//then
$logContent = $this->_readStreamContent('test://stdout');
$logContent = $this->readStreamContent('test://stdout');
Assert::thatString($logContent)->isEmpty();
}

Expand All @@ -74,13 +99,13 @@ public function shouldWriteInfoMessageIfMinimalLevelIsSetToInfo()
{
//given
Config::overrideProperty('logger', 'default', 'minimal_levels')->with(['TEST' => LOG_INFO]);
$this->logger = new StdOutputLogger('TEST', 'default', 'test');
$this->logger = new LoggerAdapter('TEST', 'default', new StdOutputLogger('TEST', 'default', 'test'));

//when
$this->logger->info('My info log line with param %s and %s.', [42, 'Zaphod']);
$this->logger->info('My info log line with param 42 and Zaphod.');

//then
$logContent = $this->_readStreamContent('test://stdout');
$logContent = $this->readStreamContent('test://stdout');
Assert::thatString($logContent)->isNotEmpty();
}

Expand All @@ -91,7 +116,7 @@ public function shouldWriteWarningMessage()
$this->logger->warning('My warning log line without params.');

//then
$logContent = $this->_readStreamContent('test://stdout');
$logContent = $this->readStreamContent('test://stdout');
Assert::thatString($logContent)->contains('2014-01-01 11:11:11: TEST warning: [ID: ] My warning log line without params.');
}

Expand All @@ -102,7 +127,7 @@ public function shouldWriteCriticalMessage()
$this->logger->critical('My fatal log line without params.');

//then
$logContent = $this->_readStreamContent('test://stdout');
$logContent = $this->readStreamContent('test://stdout');
Assert::thatString($logContent)->contains('2014-01-01 11:11:11: TEST critical: [ID: ] My fatal log line without params.');
}

Expand All @@ -116,7 +141,7 @@ public function shouldWriteDebugMessageIfDebugIsOn()
$this->logger->debug('My debug log line without params.');

//then
$logContent = $this->_readStreamContent('test://stdout');
$logContent = $this->readStreamContent('test://stdout');
Assert::thatString($logContent)->contains('2014-01-01 11:11:11: TEST debug: [ID: ] My debug log line without params.');
}

Expand All @@ -130,12 +155,12 @@ public function shouldIgnoreDebugMessageIfDebugIsOff()
$this->logger->debug('My debug log line without params.');

//then
$logContent = $this->_readStreamContent('test://stdout');
$logContent = $this->readStreamContent('test://stdout');
Assert::thatString($logContent)->hasSize(0);
Config::clearProperty('debug');
}

private function _readStreamContent($streamFile)
private function readStreamContent(string $streamFile): string
{
return file_get_contents($streamFile);
}
Expand Down
12 changes: 5 additions & 7 deletions test/src/Ouzo/Core/Logger/SyslogLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@

namespace Ouzo\Logger;


use Ouzo\Config;
use Ouzo\Tests\Mock\Mock;
use Ouzo\Tests\Mock\MockInterface;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

class SyslogLoggerTest extends TestCase
{
private LoggerInterface $logger;
private SyslogAdapter $syslogAdapter;
private LoggerAdapter $logger;
private SyslogAdapter|MockInterface $syslogAdapter;

protected function setUp(): void
{
parent::setUp();
$this->syslogAdapter = Mock::create(SyslogAdapter::class);
$this->logger = new SyslogLogger('TEST', 'default', $this->syslogAdapter);
$this->logger = new LoggerAdapter('TEST', 'default', new SyslogLogger('TEST', 'default', $this->syslogAdapter));
}

protected function tearDown(): void
Expand All @@ -35,10 +34,9 @@ protected function tearDown(): void
public function shouldWriteErrorMessage()
{
//when
$this->logger->error('My error log line with param %s and %s.', [42, 'Zaphod']);
$this->logger->error('My error log line with param 42 and Zaphod.');

//then
Mock::verify($this->syslogAdapter)->log(LOG_ERR, 'TEST error: [ID: ] My error log line with param 42 and Zaphod.');
}

}

0 comments on commit d23dcf7

Please sign in to comment.