Les listes paginées (utilisateurs, classes, matières, affectations, invitations parents, droits à l'image) effectuaient des requêtes SQL complètes à chaque chargement de page, sans aucun cache. Sur les établissements avec plusieurs centaines d'enregistrements, cela causait des temps de réponse perceptibles et une charge inutile sur PostgreSQL. Cette refactorisation introduit un cache tag-aware (Redis en prod, filesystem en dev) avec invalidation événementielle, et extrait les requêtes de lecture dans des ports Application / implémentations DBAL conformes à l'architecture hexagonale. Un middleware Messenger garantit l'invalidation synchrone du cache même pour les événements routés en asynchrone (envoi d'emails), évitant ainsi toute donnée périmée côté UI.
138 lines
4.3 KiB
PHP
138 lines
4.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Unit\Administration\Application\Query\GetUsers;
|
|
|
|
use App\Administration\Application\Dto\PaginatedResult;
|
|
use App\Administration\Application\Port\PaginatedUsersReader;
|
|
use App\Administration\Application\Query\GetUsers\GetUsersHandler;
|
|
use App\Administration\Application\Query\GetUsers\GetUsersQuery;
|
|
use App\Administration\Application\Query\GetUsers\UserDto;
|
|
use App\Administration\Application\Service\Cache\PaginatedQueryCache;
|
|
use DateTimeImmutable;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
|
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
|
|
|
|
final class GetUsersHandlerTest extends TestCase
|
|
{
|
|
private PaginatedUsersReader $reader;
|
|
private PaginatedQueryCache $cache;
|
|
private GetUsersHandler $handler;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->reader = $this->createMock(PaginatedUsersReader::class);
|
|
$this->cache = new PaginatedQueryCache(
|
|
new TagAwareAdapter(new ArrayAdapter()),
|
|
);
|
|
$this->handler = new GetUsersHandler($this->reader, $this->cache);
|
|
}
|
|
|
|
#[Test]
|
|
public function returnsUsersForTenant(): void
|
|
{
|
|
$dto = $this->createUserDto();
|
|
$this->reader->method('findPaginated')->willReturn(
|
|
new PaginatedResult(items: [$dto], total: 1, page: 1, limit: 30),
|
|
);
|
|
|
|
$result = ($this->handler)(new GetUsersQuery(tenantId: 'tenant-1'));
|
|
|
|
self::assertCount(1, $result->items);
|
|
self::assertSame(1, $result->total);
|
|
}
|
|
|
|
#[Test]
|
|
public function mapsDtoFields(): void
|
|
{
|
|
$dto = $this->createUserDto();
|
|
$this->reader->method('findPaginated')->willReturn(
|
|
new PaginatedResult(items: [$dto], total: 1, page: 1, limit: 30),
|
|
);
|
|
|
|
$result = ($this->handler)(new GetUsersQuery(tenantId: 'tenant-1'));
|
|
|
|
$item = $result->items[0];
|
|
self::assertSame('user-1', $item->id);
|
|
self::assertSame('prof@test.com', $item->email);
|
|
self::assertSame('ROLE_PROF', $item->role);
|
|
self::assertSame('Dupont', $item->lastName);
|
|
}
|
|
|
|
#[Test]
|
|
public function paginatesResults(): void
|
|
{
|
|
$this->reader->method('findPaginated')->willReturn(
|
|
new PaginatedResult(items: [], total: 50, page: 2, limit: 10),
|
|
);
|
|
|
|
$result = ($this->handler)(new GetUsersQuery(tenantId: 'tenant-1', page: 2, limit: 10));
|
|
|
|
self::assertSame(50, $result->total);
|
|
self::assertSame(2, $result->page);
|
|
self::assertSame(10, $result->limit);
|
|
}
|
|
|
|
#[Test]
|
|
public function cachesResult(): void
|
|
{
|
|
$dto = $this->createUserDto();
|
|
$this->reader->expects(self::once())->method('findPaginated')->willReturn(
|
|
new PaginatedResult(items: [$dto], total: 1, page: 1, limit: 30),
|
|
);
|
|
|
|
$query = new GetUsersQuery(tenantId: 'tenant-1');
|
|
($this->handler)($query);
|
|
$result = ($this->handler)($query);
|
|
|
|
self::assertCount(1, $result->items);
|
|
}
|
|
|
|
#[Test]
|
|
public function clampsPageToMinimumOne(): void
|
|
{
|
|
$this->reader->method('findPaginated')->willReturn(
|
|
new PaginatedResult(items: [], total: 0, page: 1, limit: 30),
|
|
);
|
|
|
|
$result = ($this->handler)(new GetUsersQuery(tenantId: 'tenant-1', page: -5));
|
|
|
|
self::assertSame(1, $result->page);
|
|
}
|
|
|
|
#[Test]
|
|
public function clampsLimitToMaximumHundred(): void
|
|
{
|
|
$this->reader->method('findPaginated')->willReturn(
|
|
new PaginatedResult(items: [], total: 0, page: 1, limit: 100),
|
|
);
|
|
|
|
$result = ($this->handler)(new GetUsersQuery(tenantId: 'tenant-1', limit: 500));
|
|
|
|
self::assertSame(100, $result->limit);
|
|
}
|
|
|
|
private function createUserDto(): UserDto
|
|
{
|
|
return new UserDto(
|
|
id: 'user-1',
|
|
email: 'prof@test.com',
|
|
role: 'ROLE_PROF',
|
|
roleLabel: 'Enseignant',
|
|
roles: ['ROLE_PROF'],
|
|
firstName: 'Jean',
|
|
lastName: 'Dupont',
|
|
statut: 'actif',
|
|
createdAt: new DateTimeImmutable('2026-01-15'),
|
|
invitedAt: new DateTimeImmutable('2026-01-10'),
|
|
activatedAt: new DateTimeImmutable('2026-01-12'),
|
|
blockedAt: null,
|
|
blockedReason: null,
|
|
invitationExpiree: false,
|
|
);
|
|
}
|
|
}
|