185 lines
5.0 KiB
PHP
185 lines
5.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Logger;
|
|
|
|
use App\Logger\ErrorCodeProcessor;
|
|
use App\Service\ErrorCodeGenerator;
|
|
use Monolog\Level;
|
|
use Monolog\LogRecord;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class ErrorCodeProcessorTest extends TestCase
|
|
{
|
|
private ErrorCodeGenerator $errorCodeService;
|
|
private ErrorCodeProcessor $processor;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->errorCodeService = $this->createMock(ErrorCodeGenerator::class);
|
|
$this->processor = new ErrorCodeProcessor($this->errorCodeService);
|
|
}
|
|
|
|
public function testIgnoresDebugLevel(): void
|
|
{
|
|
$record = new LogRecord(
|
|
datetime: new \DateTimeImmutable(),
|
|
channel: 'test',
|
|
level: Level::Debug,
|
|
message: 'Debug message',
|
|
);
|
|
|
|
$this->errorCodeService
|
|
->expects($this->never())
|
|
->method('getErrorCode');
|
|
|
|
$this->errorCodeService
|
|
->expects($this->never())
|
|
->method('markErrorOccurred');
|
|
|
|
$result = ($this->processor)($record);
|
|
|
|
$this->assertArrayNotHasKey('error_code', $result->extra);
|
|
}
|
|
|
|
public function testIgnoresInfoLevel(): void
|
|
{
|
|
$record = new LogRecord(
|
|
datetime: new \DateTimeImmutable(),
|
|
channel: 'test',
|
|
level: Level::Info,
|
|
message: 'Info message',
|
|
);
|
|
|
|
$this->errorCodeService
|
|
->expects($this->never())
|
|
->method('getErrorCode');
|
|
|
|
$this->errorCodeService
|
|
->expects($this->never())
|
|
->method('markErrorOccurred');
|
|
|
|
$result = ($this->processor)($record);
|
|
|
|
$this->assertArrayNotHasKey('error_code', $result->extra);
|
|
}
|
|
|
|
public function testIgnoresWarningLevel(): void
|
|
{
|
|
$record = new LogRecord(
|
|
datetime: new \DateTimeImmutable(),
|
|
channel: 'test',
|
|
level: Level::Warning,
|
|
message: 'Warning message',
|
|
);
|
|
|
|
$this->errorCodeService
|
|
->expects($this->never())
|
|
->method('getErrorCode');
|
|
|
|
$this->errorCodeService
|
|
->expects($this->never())
|
|
->method('markErrorOccurred');
|
|
|
|
$result = ($this->processor)($record);
|
|
|
|
$this->assertArrayNotHasKey('error_code', $result->extra);
|
|
}
|
|
|
|
public function testAddsErrorCodeForErrorLevel(): void
|
|
{
|
|
$record = new LogRecord(
|
|
datetime: new \DateTimeImmutable(),
|
|
channel: 'test',
|
|
level: Level::Error,
|
|
message: 'Error message',
|
|
);
|
|
|
|
$this->errorCodeService
|
|
->expects($this->once())
|
|
->method('getErrorCode')
|
|
->willReturn('E-ABC12345');
|
|
|
|
$this->errorCodeService
|
|
->expects($this->once())
|
|
->method('markErrorOccurred');
|
|
|
|
$result = ($this->processor)($record);
|
|
|
|
$this->assertArrayHasKey('error_code', $result->extra);
|
|
$this->assertSame('E-ABC12345', $result->extra['error_code']);
|
|
}
|
|
|
|
public function testAddsErrorCodeForCriticalLevel(): void
|
|
{
|
|
$record = new LogRecord(
|
|
datetime: new \DateTimeImmutable(),
|
|
channel: 'test',
|
|
level: Level::Critical,
|
|
message: 'Critical message',
|
|
);
|
|
|
|
$this->errorCodeService
|
|
->expects($this->once())
|
|
->method('getErrorCode')
|
|
->willReturn('E-XYZ78901');
|
|
|
|
$this->errorCodeService
|
|
->expects($this->once())
|
|
->method('markErrorOccurred');
|
|
|
|
$result = ($this->processor)($record);
|
|
|
|
$this->assertArrayHasKey('error_code', $result->extra);
|
|
$this->assertSame('E-XYZ78901', $result->extra['error_code']);
|
|
}
|
|
|
|
public function testAddsErrorCodeForEmergencyLevel(): void
|
|
{
|
|
$record = new LogRecord(
|
|
datetime: new \DateTimeImmutable(),
|
|
channel: 'test',
|
|
level: Level::Emergency,
|
|
message: 'Emergency message',
|
|
);
|
|
|
|
$this->errorCodeService
|
|
->expects($this->once())
|
|
->method('getErrorCode')
|
|
->willReturn('E-EMR99999');
|
|
|
|
$this->errorCodeService
|
|
->expects($this->once())
|
|
->method('markErrorOccurred');
|
|
|
|
$result = ($this->processor)($record);
|
|
|
|
$this->assertArrayHasKey('error_code', $result->extra);
|
|
$this->assertSame('E-EMR99999', $result->extra['error_code']);
|
|
}
|
|
|
|
public function testDoesNotAddCodeWhenServiceReturnsNull(): void
|
|
{
|
|
$record = new LogRecord(
|
|
datetime: new \DateTimeImmutable(),
|
|
channel: 'test',
|
|
level: Level::Error,
|
|
message: 'Error message',
|
|
);
|
|
|
|
$this->errorCodeService
|
|
->expects($this->once())
|
|
->method('getErrorCode')
|
|
->willReturn(null);
|
|
|
|
$this->errorCodeService
|
|
->expects($this->never())
|
|
->method('markErrorOccurred');
|
|
|
|
$result = ($this->processor)($record);
|
|
|
|
$this->assertArrayNotHasKey('error_code', $result->extra);
|
|
}
|
|
}
|