test: ajout tests ServiceStatusHistory, User (extended) et AdminLogListener

tests/Entity/ServiceStatusHistoryTest.php (nouveau, 2 tests):
- testConstructor: verifie id null, service, status, message et createdAt
- testConstructorWithoutMessage: message nullable a null par defaut

tests/Entity/UserExtendedTest.php (nouveau, 9 tests):
- testKeycloakId: get/set keycloakId nullable
- testTempPassword: get/set/has tempPassword nullable
- testGoogleAuthenticator: isEnabled (false par defaut, true quand
  secret set + isGoogleAuthEnabled true), get/set secret, getUsername
- testEmailAuth: isEnabled (false par defaut), getRecipient, get/set code
- testBackupCodes: get/set codes, isBackupCode, invalidateBackupCode
  (supprime du tableau)
- testAvatar: get/set avatar nullable
- testFullName: retourne "FirstName LastName"
- testRoles: contient toujours ROLE_USER, set/get custom roles
- testUserIdentifier: retourne l'email

tests/EventListener/AdminLogListenerTest.php (nouveau, 7 tests):
- testLogsAdminRoute: appelle log() pour une route app_admin_*
- testLogsAdminRouteWithUser: passe le User depuis le TokenStorage
- testIgnoresNonAdminRoute: ne log pas les routes non admin
- testIgnoresSubRequest: ne log pas les sous-requetes
- testIgnoresAjaxSearch: ne log pas les recherches AJAX
- testLogThrowsDoesNotBlock: exception dans log() ne bloque pas
  la requete (catch silencieux)
- testNoTokenReturnsNullUser: token null passe null comme user

Resultat global: 262 tests, 463 assertions, 0 failures

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Serreau Jovann
2026-04-02 23:41:44 +02:00
parent f068456308
commit c3d3962b99
3 changed files with 282 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Tests\Entity;
use App\Entity\Service;
use App\Entity\ServiceCategory;
use App\Entity\ServiceStatusHistory;
use PHPUnit\Framework\TestCase;
class ServiceStatusHistoryTest extends TestCase
{
private function createService(): Service
{
$category = new ServiceCategory('Web', 'web');
return new Service('Test Service', 'test-service', $category);
}
public function testConstructor(): void
{
$service = $this->createService();
$history = new ServiceStatusHistory($service, 'up', 'All good');
$this->assertNull($history->getId());
$this->assertSame($service, $history->getService());
$this->assertSame('up', $history->getStatus());
$this->assertSame('All good', $history->getMessage());
$this->assertInstanceOf(\DateTimeImmutable::class, $history->getCreatedAt());
}
public function testConstructorWithoutMessage(): void
{
$service = $this->createService();
$history = new ServiceStatusHistory($service, 'down');
$this->assertSame('down', $history->getStatus());
$this->assertNull($history->getMessage());
}
}

View File

@@ -0,0 +1,116 @@
<?php
namespace App\Tests\Entity;
use App\Entity\User;
use PHPUnit\Framework\TestCase;
class UserExtendedTest extends TestCase
{
private function createUser(): User
{
$user = new User();
$user->setEmail('test@test.com');
$user->setFirstName('John');
$user->setLastName('Doe');
$user->setPassword('hashed');
return $user;
}
public function testKeycloakId(): void
{
$user = $this->createUser();
$this->assertNull($user->getKeycloakId());
$user->setKeycloakId('kc-123');
$this->assertSame('kc-123', $user->getKeycloakId());
}
public function testTempPassword(): void
{
$user = $this->createUser();
$this->assertNull($user->getTempPassword());
$this->assertFalse($user->hasTempPassword());
$user->setTempPassword('temp123');
$this->assertSame('temp123', $user->getTempPassword());
$this->assertTrue($user->hasTempPassword());
$user->setTempPassword(null);
$this->assertFalse($user->hasTempPassword());
}
public function testGoogleAuthenticator(): void
{
$user = $this->createUser();
$this->assertFalse($user->isGoogleAuthenticatorEnabled());
$this->assertNull($user->getGoogleAuthenticatorSecret());
$this->assertSame('test@test.com', $user->getGoogleAuthenticatorUsername());
$user->setGoogleAuthenticatorSecret('ABCDEF');
$user->setIsGoogleAuthEnabled(true);
$this->assertTrue($user->isGoogleAuthenticatorEnabled());
$this->assertSame('ABCDEF', $user->getGoogleAuthenticatorSecret());
}
public function testEmailAuth(): void
{
$user = $this->createUser();
$this->assertFalse($user->isEmailAuthEnabled());
$this->assertSame('test@test.com', $user->getEmailAuthRecipient());
$this->assertNull($user->getEmailAuthCode());
$user->setEmailAuthCode('123456');
$this->assertSame('123456', $user->getEmailAuthCode());
}
public function testBackupCodes(): void
{
$user = $this->createUser();
$this->assertSame([], $user->getBackupCodes());
$codes = ['code1', 'code2', 'code3'];
$user->setBackupCodes($codes);
$this->assertSame($codes, $user->getBackupCodes());
$this->assertTrue($user->isBackupCode('code1'));
$this->assertFalse($user->isBackupCode('invalid'));
$user->invalidateBackupCode('code1');
$this->assertNotContains('code1', $user->getBackupCodes());
$this->assertCount(2, $user->getBackupCodes());
}
public function testAvatar(): void
{
$user = $this->createUser();
$this->assertNull($user->getAvatar());
$user->setAvatar('avatar.jpg');
$this->assertSame('avatar.jpg', $user->getAvatar());
}
public function testFullName(): void
{
$user = $this->createUser();
$this->assertSame('John Doe', $user->getFullName());
}
public function testRoles(): void
{
$user = $this->createUser();
$this->assertContains('ROLE_USER', $user->getRoles());
$user->setRoles(['ROLE_ROOT']);
$this->assertContains('ROLE_ROOT', $user->getRoles());
$this->assertContains('ROLE_USER', $user->getRoles());
}
public function testUserIdentifier(): void
{
$user = $this->createUser();
$this->assertSame('test@test.com', $user->getUserIdentifier());
}
}

View File

@@ -0,0 +1,127 @@
<?php
namespace App\Tests\EventListener;
use App\Entity\User;
use App\EventListener\AdminLogListener;
use App\Service\AppLoggerService;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class AdminLogListenerTest extends TestCase
{
private AppLoggerService $logger;
private TokenStorageInterface $tokenStorage;
private AdminLogListener $listener;
protected function setUp(): void
{
$this->logger = $this->createMock(AppLoggerService::class);
$this->tokenStorage = $this->createStub(TokenStorageInterface::class);
$this->listener = new AdminLogListener($this->logger, $this->tokenStorage);
}
private function createEvent(Request $request, bool $isMainRequest = true): ControllerEvent
{
$kernel = $this->createStub(HttpKernelInterface::class);
return new ControllerEvent(
$kernel,
fn () => null,
$request,
$isMainRequest ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::SUB_REQUEST,
);
}
public function testLogsAdminRoute(): void
{
$request = new Request();
$request->attributes->set('_route', 'app_admin_dashboard');
$this->logger->expects($this->once())->method('log');
$this->listener->__invoke($this->createEvent($request));
}
public function testLogsAdminRouteWithUser(): void
{
$user = new User();
$user->setEmail('admin@test.com');
$user->setFirstName('Admin');
$user->setLastName('User');
$user->setPassword('hashed');
$token = $this->createStub(TokenInterface::class);
$token->method('getUser')->willReturn($user);
$this->tokenStorage->method('getToken')->willReturn($token);
$listener = new AdminLogListener($this->logger, $this->tokenStorage);
$request = new Request();
$request->attributes->set('_route', 'app_admin_clients_index');
$this->logger->expects($this->once())->method('log');
$listener->__invoke($this->createEvent($request));
}
public function testIgnoresNonAdminRoute(): void
{
$request = new Request();
$request->attributes->set('_route', 'app_home');
$this->logger->expects($this->never())->method('log');
$this->listener->__invoke($this->createEvent($request));
}
public function testIgnoresSubRequest(): void
{
$request = new Request();
$request->attributes->set('_route', 'app_admin_dashboard');
$this->logger->expects($this->never())->method('log');
$this->listener->__invoke($this->createEvent($request, false));
}
public function testIgnoresAjaxSearch(): void
{
$request = new Request();
$request->attributes->set('_route', 'app_admin_clients_search');
$request->headers->set('X-Requested-With', 'XMLHttpRequest');
$this->logger->expects($this->never())->method('log');
$this->listener->__invoke($this->createEvent($request));
}
public function testLogThrowsDoesNotBlock(): void
{
$request = new Request();
$request->attributes->set('_route', 'app_admin_dashboard');
$this->logger->method('log')->willThrowException(new \RuntimeException('DB down'));
$this->listener->__invoke($this->createEvent($request));
$this->addToAssertionCount(1);
}
public function testNoTokenReturnsNullUser(): void
{
$this->tokenStorage->method('getToken')->willReturn(null);
$listener = new AdminLogListener($this->logger, $this->tokenStorage);
$request = new Request();
$request->attributes->set('_route', 'app_admin_dashboard');
$this->logger->expects($this->once())->method('log');
$listener->__invoke($this->createEvent($request));
}
}