feat: Optimiser la pagination avec cache-aside et ports de lecture dédiés

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.
This commit is contained in:
2026-03-01 14:33:56 +01:00
parent ce05207c64
commit 23dd7177f2
41 changed files with 2854 additions and 1584 deletions

View File

@@ -4,141 +4,131 @@ declare(strict_types=1);
namespace App\Tests\Unit\Administration\Application\Query\GetStudentsImageRights;
use App\Administration\Application\Dto\PaginatedResult;
use App\Administration\Application\Port\PaginatedStudentImageRightsReader;
use App\Administration\Application\Query\GetStudentsImageRights\GetStudentsImageRightsHandler;
use App\Administration\Application\Query\GetStudentsImageRights\GetStudentsImageRightsQuery;
use App\Administration\Domain\Model\User\Email;
use App\Administration\Domain\Model\User\ImageRightsStatus;
use App\Administration\Domain\Model\User\Role;
use App\Administration\Domain\Model\User\User;
use App\Administration\Domain\Model\User\UserId;
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryUserRepository;
use App\Shared\Domain\Tenant\TenantId;
use App\Administration\Application\Query\GetStudentsImageRights\StudentImageRightsDto;
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 GetStudentsImageRightsHandlerTest extends TestCase
{
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440002';
private InMemoryUserRepository $userRepository;
private PaginatedStudentImageRightsReader $reader;
private PaginatedQueryCache $cache;
private GetStudentsImageRightsHandler $handler;
protected function setUp(): void
{
$this->userRepository = new InMemoryUserRepository();
$this->handler = new GetStudentsImageRightsHandler($this->userRepository);
}
#[Test]
public function returnsOnlyStudents(): void
{
$this->seedStudentsAndParent();
$query = new GetStudentsImageRightsQuery(tenantId: self::TENANT_ID);
$result = ($this->handler)($query);
self::assertCount(2, $result);
}
#[Test]
public function filtersStudentsByStatus(): void
{
$this->seedStudentsAndParent();
$query = new GetStudentsImageRightsQuery(
tenantId: self::TENANT_ID,
status: 'authorized',
$this->reader = $this->createMock(PaginatedStudentImageRightsReader::class);
$this->cache = new PaginatedQueryCache(
new TagAwareAdapter(new ArrayAdapter()),
);
$result = ($this->handler)($query);
self::assertCount(1, $result);
self::assertSame('authorized', $result[0]->imageRightsStatus);
$this->handler = new GetStudentsImageRightsHandler($this->reader, $this->cache);
}
#[Test]
public function returnsEmptyForNoStudents(): void
public function returnsItemsForTenant(): void
{
$query = new GetStudentsImageRightsQuery(tenantId: self::TENANT_ID);
$result = ($this->handler)($query);
self::assertCount(0, $result);
}
#[Test]
public function doesNotReturnStudentsFromOtherTenant(): void
{
$this->seedStudentsAndParent();
$query = new GetStudentsImageRightsQuery(
tenantId: '550e8400-e29b-41d4-a716-446655440099',
$dto = $this->createStudentImageRightsDto();
$this->reader->method('findPaginated')->willReturn(
new PaginatedResult(items: [$dto], total: 1, page: 1, limit: 30),
);
$result = ($this->handler)($query);
self::assertCount(0, $result);
$result = ($this->handler)(new GetStudentsImageRightsQuery(tenantId: 'tenant-1'));
self::assertCount(1, $result->items);
self::assertSame(1, $result->total);
}
#[Test]
public function returnsDtoWithCorrectFields(): void
public function mapsDtoFields(): void
{
$this->seedStudentsAndParent();
$query = new GetStudentsImageRightsQuery(
tenantId: self::TENANT_ID,
status: 'authorized',
$dto = $this->createStudentImageRightsDto();
$this->reader->method('findPaginated')->willReturn(
new PaginatedResult(items: [$dto], total: 1, page: 1, limit: 30),
);
$result = ($this->handler)($query);
self::assertCount(1, $result);
$dto = $result[0];
self::assertSame('Alice', $dto->firstName);
self::assertSame('Dupont', $dto->lastName);
self::assertSame('authorized', $dto->imageRightsStatus);
self::assertSame('Autorisé', $dto->imageRightsStatusLabel);
$result = ($this->handler)(new GetStudentsImageRightsQuery(tenantId: 'tenant-1'));
$item = $result->items[0];
self::assertSame('student-1', $item->id);
self::assertSame('Alice', $item->firstName);
self::assertSame('Dupont', $item->lastName);
self::assertSame('alice@test.com', $item->email);
self::assertSame('authorized', $item->imageRightsStatus);
self::assertSame('Autorise', $item->imageRightsStatusLabel);
self::assertSame('6eme A', $item->className);
}
private function seedStudentsAndParent(): void
#[Test]
public function paginatesResults(): void
{
$student1 = User::inviter(
email: new Email('alice@example.com'),
role: Role::ELEVE,
tenantId: TenantId::fromString(self::TENANT_ID),
schoolName: 'École Alpha',
$this->reader->method('findPaginated')->willReturn(
new PaginatedResult(items: [], total: 50, page: 2, limit: 10),
);
$result = ($this->handler)(new GetStudentsImageRightsQuery(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->createStudentImageRightsDto();
$this->reader->expects(self::once())->method('findPaginated')->willReturn(
new PaginatedResult(items: [$dto], total: 1, page: 1, limit: 30),
);
$query = new GetStudentsImageRightsQuery(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 GetStudentsImageRightsQuery(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 GetStudentsImageRightsQuery(tenantId: 'tenant-1', limit: 500));
self::assertSame(100, $result->limit);
}
private function createStudentImageRightsDto(): StudentImageRightsDto
{
return new StudentImageRightsDto(
id: 'student-1',
firstName: 'Alice',
lastName: 'Dupont',
invitedAt: new DateTimeImmutable('2026-01-15'),
dateNaissance: new DateTimeImmutable('2012-06-15'),
email: 'alice@test.com',
imageRightsStatus: 'authorized',
imageRightsStatusLabel: 'Autorise',
imageRightsUpdatedAt: new DateTimeImmutable('2026-02-01'),
className: '6eme A',
);
$student1->modifierDroitImage(
ImageRightsStatus::AUTHORIZED,
UserId::fromString('550e8400-e29b-41d4-a716-446655440099'),
new DateTimeImmutable('2026-02-01'),
);
$student2 = User::inviter(
email: new Email('bob@example.com'),
role: Role::ELEVE,
tenantId: TenantId::fromString(self::TENANT_ID),
schoolName: 'École Alpha',
firstName: 'Bob',
lastName: 'Martin',
invitedAt: new DateTimeImmutable('2026-01-15'),
dateNaissance: new DateTimeImmutable('2013-03-20'),
);
// Bob has default NOT_SPECIFIED
$parent = User::inviter(
email: new Email('parent@example.com'),
role: Role::PARENT,
tenantId: TenantId::fromString(self::TENANT_ID),
schoolName: 'École Alpha',
firstName: 'Pierre',
lastName: 'Dupont',
invitedAt: new DateTimeImmutable('2026-01-15'),
);
$this->userRepository->save($student1);
$this->userRepository->save($student2);
$this->userRepository->save($parent);
}
}