JsonLineLoggerTest.php
Pfad: tests/Rechnung/JsonLineLoggerTest.php
Ext: php
Größe: 1317 Bytes
Geändert: 2026-07-16 17:28:06+02
<?php
declare(strict_types=1);
namespace Demo\Tests\Rechnung;
use Demo\Infrastructure\Rechnung\JsonLineLogger;
use PHPUnit\Framework\TestCase;
use RuntimeException;
final class JsonLineLoggerTest extends TestCase
{
public function testWritesCorrelatedAndRedactedJsonLine(): void
{
$path = sys_get_temp_dir() . '/invoice-audit-' . getmypid() . '-' . hrtime(true) . '.jsonl';
$logger = new JsonLineLogger($path);
$logger->logStep('run-123', 'download', 'OK', [
'apiKey' => 'secret-value',
'nested' => ['password' => 'hidden'],
'message' => 'Authorization: Bearer abc.def',
]);
$line = file_get_contents($path);
self::assertIsString($line);
$record = json_decode($line, true, 512, JSON_THROW_ON_ERROR);
self::assertSame('run-123', $record['runId']);
self::assertSame('[REDACTED]', $record['context']['apiKey']);
self::assertSame('[REDACTED]', $record['context']['nested']['password']);
self::assertStringNotContainsString('abc.def', $line);
}
public function testWriteFailureIsNotIgnored(): void
{
$logger = new JsonLineLogger('/dev/full');
$this->expectException(RuntimeException::class);
$logger->logStep('run-123', 'persist', 'ERROR');
}
}