Files
ephub/tests/Logger/AuditActorProcessorTest.php
T
2026-09-18 11:16:57 +02:00

53 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Logger;
use App\Entity\User;
use App\Logger\AuditActorProcessor;
use Monolog\Level;
use Monolog\LogRecord;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\Security;
/**
* The actor is the one field an audit record cannot be allowed to get wrong, so it is taken from
* the token rather than from any call site -- which is what these cover.
*/
class AuditActorProcessorTest extends TestCase
{
public function testTheActorIsTheSignedInUsersEmailAddress(): void
{
$record = $this->process(new User('[email protected]', 'Admin'));
self::assertSame('[email protected]', $record->extra['actor']);
}
/**
* PHPUnit runs under the CLI SAPI, which is the same context a console command or the
* message worker writes from -- so this is the system branch, not the anonymous one.
*/
public function testAWriteWithNoUserBehindItIsAttributedToTheSystem(): void
{
$record = $this->process(null);
self::assertSame(AuditActorProcessor::SYSTEM, $record->extra['actor']);
}
private function process(?User $user): LogRecord
{
$security = $this->createStub(Security::class);
$security->method('getUser')->willReturn($user);
$processor = new AuditActorProcessor($security);
return $processor(new LogRecord(
new \DateTimeImmutable(),
'audit',
Level::Info,
'Booking created.',
));
}
}