42 lines
1.3 KiB
PHP
42 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Logging;
|
|
|
|
use App\Logging\UserDataProcessor;
|
|
use Monolog\Level;
|
|
use Monolog\LogRecord;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Symfony\Bundle\SecurityBundle\Security;
|
|
use Symfony\Component\Security\Core\User\InMemoryUser;
|
|
|
|
/**
|
|
* Same hazard as InjectUserProcessorTest: this one feeds the log table through
|
|
* App\Logging\DatabaseHandler, and it is registered globally with #[AsMonologProcessor], so a
|
|
* request on the stateless "api" firewall reaches it with an in-memory user that has no getId().
|
|
*/
|
|
class UserDataProcessorTest extends TestCase
|
|
{
|
|
public function testAnApiFirewallUserGetsNoIdInsteadOfAnError(): void
|
|
{
|
|
$security = $this->createMock(Security::class);
|
|
$security->method('getUser')->willReturn(new InMemoryUser('api', null, ['ROLE_API']));
|
|
$security->method('getToken')->willReturn(null);
|
|
|
|
$processor = new UserDataProcessor($security, 'fpm-fcgi');
|
|
|
|
$record = $processor(new LogRecord(
|
|
new \DateTimeImmutable(),
|
|
'api',
|
|
Level::Info,
|
|
'Application form submission received'
|
|
));
|
|
|
|
$this->assertSame(
|
|
['id' => null, 'username' => 'api', 'roles' => ['ROLE_API']],
|
|
$record->extra['user']
|
|
);
|
|
}
|
|
}
|