You've already forked Php-Template
All checks were successful
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 2m32s
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Successful in 2m48s
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Successful in 2m33s
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Successful in 2m44s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Successful in 2m53s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Successful in 3m5s
Reviewed-on: #19 Co-authored-by: Ron Rise <ron@siteworxpro.com> Co-committed-by: Ron Rise <ron@siteworxpro.com>
230 lines
6.5 KiB
PHP
230 lines
6.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Siteworxpro\Tests\Log;
|
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
use Psr\Log\LogLevel;
|
|
use Siteworxpro\App\Log\Logger;
|
|
use Siteworxpro\Tests\Unit;
|
|
|
|
class LoggerTest extends Unit
|
|
{
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
private function getLoggerWithBuffer(string $logLevel): array
|
|
{
|
|
$inputBuffer = fopen('php://memory', 'r+');
|
|
return [new Logger($logLevel, $inputBuffer), $inputBuffer];
|
|
}
|
|
|
|
private function getContents($inputBuffer): string
|
|
{
|
|
return stream_get_contents($inputBuffer, -1, 0);
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
private function testLogLevel(string $level): void
|
|
{
|
|
[$logger, $inputBuffer] = $this->getLoggerWithBuffer($level);
|
|
$logger->$level('message', ['key' => 'value']);
|
|
$output = $this->getContents($inputBuffer);
|
|
|
|
$this->assertNotEmpty($output);
|
|
$decoded = json_decode($output, true);
|
|
$this->assertEquals('message', $decoded['message']);
|
|
$this->assertEquals('value', $decoded['context']['key']);
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
private function testLogLevelEmpty(string $configLevel, string $logLevel): void
|
|
{
|
|
[$logger, $inputBuffer] = $this->getLoggerWithBuffer($configLevel);
|
|
$logger->$logLevel('message', ['key' => 'value']);
|
|
$output = $this->getContents($inputBuffer);
|
|
|
|
$this->assertEmpty($output);
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function testLogsDebugMessageWhenLevelIsDebug(): void
|
|
{
|
|
$this->testLogLevel(LogLevel::DEBUG);
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function testLogsInfoMessageWhenLevelIsInfo(): void
|
|
{
|
|
$this->testLogLevel(LogLevel::INFO);
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function testLogsWarningMessageWhenLevelIsWarning(): void
|
|
{
|
|
$this->testLogLevel(LogLevel::WARNING);
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function testLogsErrorMessageWhenLevelIsError(): void
|
|
{
|
|
$this->testLogLevel(LogLevel::ERROR);
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function testLogsCriticalMessageWhenLevelIsCritical(): void
|
|
{
|
|
$this->testLogLevel(LogLevel::CRITICAL);
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function testLogsAlertMessageWhenLevelIsAlert(): void
|
|
{
|
|
$this->testLogLevel(LogLevel::ALERT);
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function testLogsEmergencyMessageWhenLevelIsEmergency(): void
|
|
{
|
|
$this->testLogLevel(LogLevel::EMERGENCY);
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function testLogsNoticeMessageWhenLevelIsNotice(): void
|
|
{
|
|
$this->testLogLevel(LogLevel::NOTICE);
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function testDoesNotLogWhenMinimumLevelIsInfo(): void
|
|
{
|
|
$this->testLogLevelEmpty(LogLevel::INFO, LogLevel::DEBUG);
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function testDoesNotLogWhenMinimumLevelIsWarning(): void
|
|
{
|
|
$this->testLogLevelEmpty(LogLevel::WARNING, LogLevel::INFO);
|
|
$this->testLogLevelEmpty(LogLevel::WARNING, LogLevel::DEBUG);
|
|
}
|
|
|
|
/**
|
|
* @throws NotFoundExceptionInterface
|
|
* @throws ContainerExceptionInterface
|
|
*/
|
|
public function testDoesNotLogWhenMinimumLevelIsError(): void
|
|
{
|
|
$this->testLogLevelEmpty(LogLevel::ERROR, LogLevel::DEBUG);
|
|
$this->testLogLevelEmpty(LogLevel::ERROR, LogLevel::INFO);
|
|
$this->testLogLevelEmpty(LogLevel::ERROR, LogLevel::WARNING);
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function testDoesNotLogWhenMinimumLevelIsNotice(): void
|
|
{
|
|
$this->testLogLevelEmpty(LogLevel::NOTICE, LogLevel::DEBUG);
|
|
$this->testLogLevelEmpty(LogLevel::NOTICE, LogLevel::INFO);
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function testLogsMessageWithEmptyContext(): void
|
|
{
|
|
[$logger, $buffer] = $this->getLoggerWithBuffer(LogLevel::INFO);
|
|
|
|
$logger->info('Message without context');
|
|
$output = $this->getContents($buffer);
|
|
|
|
$this->assertNotEmpty($output);
|
|
$decoded = json_decode($output, true);
|
|
$this->assertEquals('Message without context', $decoded['message']);
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function testLogsMessageWithComplexContext(): void
|
|
{
|
|
[$logger, $buffer] = $this->getLoggerWithBuffer(LogLevel::INFO);
|
|
|
|
$logger->info('Complex context', [
|
|
'user_id' => 123,
|
|
'nested' => ['key' => 'value'],
|
|
'array' => [1, 2, 3]
|
|
]);
|
|
$output = $this->getContents($buffer);
|
|
|
|
$this->assertNotEmpty($output);
|
|
$decoded = json_decode($output, true);
|
|
$this->assertEquals(123, $decoded['context']['user_id']);
|
|
$this->assertEquals('value', $decoded['context']['nested']['key']);
|
|
}
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function testLogsStringableMessage(): void
|
|
{
|
|
[$logger, $buffer] = $this->getLoggerWithBuffer(LogLevel::INFO);
|
|
|
|
$stringable = new class implements \Stringable {
|
|
public function __toString(): string
|
|
{
|
|
return 'Stringable message';
|
|
}
|
|
};
|
|
|
|
$logger->info($stringable);
|
|
$output = $this->getContents($buffer);
|
|
$this->assertNotEmpty($output);
|
|
$decoded = json_decode($output, true);
|
|
$this->assertEquals('Stringable message', $decoded['message']);
|
|
}
|
|
}
|