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:
@@ -4,204 +4,135 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Application\Query\GetParentInvitations;
|
||||
|
||||
use App\Administration\Application\Dto\PaginatedResult;
|
||||
use App\Administration\Application\Port\PaginatedParentInvitationsReader;
|
||||
use App\Administration\Application\Query\GetParentInvitations\GetParentInvitationsHandler;
|
||||
use App\Administration\Application\Query\GetParentInvitations\GetParentInvitationsQuery;
|
||||
use App\Administration\Domain\Model\Invitation\InvitationCode;
|
||||
use App\Administration\Domain\Model\Invitation\ParentInvitation;
|
||||
use App\Administration\Domain\Model\User\Email;
|
||||
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\InMemoryParentInvitationRepository;
|
||||
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryUserRepository;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use App\Administration\Application\Query\GetParentInvitations\ParentInvitationDto;
|
||||
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 GetParentInvitationsHandlerTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440002';
|
||||
private const string OTHER_TENANT_ID = '550e8400-e29b-41d4-a716-446655440099';
|
||||
|
||||
private InMemoryParentInvitationRepository $invitationRepository;
|
||||
private InMemoryUserRepository $userRepository;
|
||||
private PaginatedParentInvitationsReader $reader;
|
||||
private PaginatedQueryCache $cache;
|
||||
private GetParentInvitationsHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->invitationRepository = new InMemoryParentInvitationRepository();
|
||||
$this->userRepository = new InMemoryUserRepository();
|
||||
$this->handler = new GetParentInvitationsHandler(
|
||||
$this->invitationRepository,
|
||||
$this->userRepository,
|
||||
$this->reader = $this->createMock(PaginatedParentInvitationsReader::class);
|
||||
$this->cache = new PaginatedQueryCache(
|
||||
new TagAwareAdapter(new ArrayAdapter()),
|
||||
);
|
||||
$this->handler = new GetParentInvitationsHandler($this->reader, $this->cache);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itReturnsAllInvitationsForTenant(): void
|
||||
public function returnsItemsForTenant(): void
|
||||
{
|
||||
$student = $this->createAndSaveStudent('Alice', 'Dupont');
|
||||
$this->createAndSaveInvitation($student->id, 'parent1@example.com');
|
||||
$this->createAndSaveInvitation($student->id, 'parent2@example.com');
|
||||
$dto = $this->createInvitationDto();
|
||||
$this->reader->method('findPaginated')->willReturn(
|
||||
new PaginatedResult(items: [$dto], total: 1, page: 1, limit: 30),
|
||||
);
|
||||
|
||||
$result = ($this->handler)(new GetParentInvitationsQuery(
|
||||
tenantId: self::TENANT_ID,
|
||||
));
|
||||
$result = ($this->handler)(new GetParentInvitationsQuery(tenantId: 'tenant-1'));
|
||||
|
||||
self::assertSame(2, $result->total);
|
||||
self::assertCount(2, $result->items);
|
||||
self::assertCount(1, $result->items);
|
||||
self::assertSame(1, $result->total);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itFiltersInvitationsByStatus(): void
|
||||
public function mapsDtoFields(): void
|
||||
{
|
||||
$student = $this->createAndSaveStudent('Bob', 'Martin');
|
||||
$invitation = $this->createAndSaveInvitation($student->id, 'parent@example.com');
|
||||
$this->createPendingInvitation($student->id, 'parent2@example.com');
|
||||
$dto = $this->createInvitationDto();
|
||||
$this->reader->method('findPaginated')->willReturn(
|
||||
new PaginatedResult(items: [$dto], total: 1, page: 1, limit: 30),
|
||||
);
|
||||
|
||||
$result = ($this->handler)(new GetParentInvitationsQuery(
|
||||
tenantId: self::TENANT_ID,
|
||||
$result = ($this->handler)(new GetParentInvitationsQuery(tenantId: 'tenant-1'));
|
||||
|
||||
$item = $result->items[0];
|
||||
self::assertSame('inv-1', $item->id);
|
||||
self::assertSame('student-1', $item->studentId);
|
||||
self::assertSame('parent@test.com', $item->parentEmail);
|
||||
self::assertSame('sent', $item->status);
|
||||
self::assertSame('Alice', $item->studentFirstName);
|
||||
self::assertSame('Dupont', $item->studentLastName);
|
||||
self::assertNull($item->activatedAt);
|
||||
self::assertNull($item->activatedUserId);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function paginatesResults(): void
|
||||
{
|
||||
$this->reader->method('findPaginated')->willReturn(
|
||||
new PaginatedResult(items: [], total: 50, page: 2, limit: 10),
|
||||
);
|
||||
|
||||
$result = ($this->handler)(new GetParentInvitationsQuery(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->createInvitationDto();
|
||||
$this->reader->expects(self::once())->method('findPaginated')->willReturn(
|
||||
new PaginatedResult(items: [$dto], total: 1, page: 1, limit: 30),
|
||||
);
|
||||
|
||||
$query = new GetParentInvitationsQuery(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 GetParentInvitationsQuery(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 GetParentInvitationsQuery(tenantId: 'tenant-1', limit: 500));
|
||||
|
||||
self::assertSame(100, $result->limit);
|
||||
}
|
||||
|
||||
private function createInvitationDto(): ParentInvitationDto
|
||||
{
|
||||
return new ParentInvitationDto(
|
||||
id: 'inv-1',
|
||||
studentId: 'student-1',
|
||||
parentEmail: 'parent@test.com',
|
||||
status: 'sent',
|
||||
));
|
||||
|
||||
self::assertSame(1, $result->total);
|
||||
self::assertSame('parent@example.com', $result->items[0]->parentEmail);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itFiltersInvitationsByStudentId(): void
|
||||
{
|
||||
$student1 = $this->createAndSaveStudent('Alice', 'Dupont');
|
||||
$student2 = $this->createAndSaveStudent('Bob', 'Martin');
|
||||
$this->createAndSaveInvitation($student1->id, 'parent1@example.com');
|
||||
$this->createAndSaveInvitation($student2->id, 'parent2@example.com');
|
||||
|
||||
$result = ($this->handler)(new GetParentInvitationsQuery(
|
||||
tenantId: self::TENANT_ID,
|
||||
studentId: (string) $student1->id,
|
||||
));
|
||||
|
||||
self::assertSame(1, $result->total);
|
||||
self::assertSame('parent1@example.com', $result->items[0]->parentEmail);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itSearchesByParentEmailOrStudentName(): void
|
||||
{
|
||||
$student = $this->createAndSaveStudent('Alice', 'Dupont');
|
||||
$this->createAndSaveInvitation($student->id, 'parent@example.com');
|
||||
$this->createAndSaveInvitation($student->id, 'other@example.com');
|
||||
|
||||
$result = ($this->handler)(new GetParentInvitationsQuery(
|
||||
tenantId: self::TENANT_ID,
|
||||
search: 'Alice',
|
||||
));
|
||||
|
||||
self::assertSame(2, $result->total);
|
||||
|
||||
$result = ($this->handler)(new GetParentInvitationsQuery(
|
||||
tenantId: self::TENANT_ID,
|
||||
search: 'parent@',
|
||||
));
|
||||
|
||||
self::assertSame(1, $result->total);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itPaginatesResults(): void
|
||||
{
|
||||
$student = $this->createAndSaveStudent('Alice', 'Dupont');
|
||||
for ($i = 0; $i < 5; ++$i) {
|
||||
$this->createAndSaveInvitation($student->id, "parent{$i}@example.com");
|
||||
}
|
||||
|
||||
$result = ($this->handler)(new GetParentInvitationsQuery(
|
||||
tenantId: self::TENANT_ID,
|
||||
page: 1,
|
||||
limit: 2,
|
||||
));
|
||||
|
||||
self::assertSame(5, $result->total);
|
||||
self::assertCount(2, $result->items);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itEnrichesResultsWithStudentNames(): void
|
||||
{
|
||||
$student = $this->createAndSaveStudent('Alice', 'Dupont');
|
||||
$this->createAndSaveInvitation($student->id, 'parent@example.com');
|
||||
|
||||
$result = ($this->handler)(new GetParentInvitationsQuery(
|
||||
tenantId: self::TENANT_ID,
|
||||
));
|
||||
|
||||
self::assertSame('Alice', $result->items[0]->studentFirstName);
|
||||
self::assertSame('Dupont', $result->items[0]->studentLastName);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itIsolatesByTenant(): void
|
||||
{
|
||||
$student = $this->createAndSaveStudent('Alice', 'Dupont');
|
||||
$this->createAndSaveInvitation($student->id, 'parent@example.com');
|
||||
|
||||
$result = ($this->handler)(new GetParentInvitationsQuery(
|
||||
tenantId: self::OTHER_TENANT_ID,
|
||||
));
|
||||
|
||||
self::assertSame(0, $result->total);
|
||||
}
|
||||
|
||||
private function createAndSaveStudent(string $firstName, string $lastName): User
|
||||
{
|
||||
$student = User::inviter(
|
||||
email: new Email($firstName . '@example.com'),
|
||||
role: Role::ELEVE,
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
schoolName: 'École Alpha',
|
||||
firstName: $firstName,
|
||||
lastName: $lastName,
|
||||
invitedAt: new DateTimeImmutable('2026-02-07 10:00:00'),
|
||||
createdAt: new DateTimeImmutable('2026-02-07'),
|
||||
expiresAt: new DateTimeImmutable('2026-03-07'),
|
||||
sentAt: new DateTimeImmutable('2026-02-07'),
|
||||
activatedAt: null,
|
||||
activatedUserId: null,
|
||||
studentFirstName: 'Alice',
|
||||
studentLastName: 'Dupont',
|
||||
);
|
||||
$student->pullDomainEvents();
|
||||
$this->userRepository->save($student);
|
||||
|
||||
return $student;
|
||||
}
|
||||
|
||||
private function createAndSaveInvitation(UserId $studentId, string $parentEmail): ParentInvitation
|
||||
{
|
||||
$code = bin2hex(random_bytes(16));
|
||||
$invitation = ParentInvitation::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
studentId: $studentId,
|
||||
parentEmail: new Email($parentEmail),
|
||||
code: new InvitationCode($code),
|
||||
createdAt: new DateTimeImmutable('2026-02-07 10:00:00'),
|
||||
createdBy: UserId::generate(),
|
||||
);
|
||||
$invitation->envoyer(new DateTimeImmutable('2026-02-07 10:00:00'));
|
||||
$invitation->pullDomainEvents();
|
||||
$this->invitationRepository->save($invitation);
|
||||
|
||||
return $invitation;
|
||||
}
|
||||
|
||||
private function createPendingInvitation(UserId $studentId, string $parentEmail): ParentInvitation
|
||||
{
|
||||
$code = bin2hex(random_bytes(16));
|
||||
$invitation = ParentInvitation::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
studentId: $studentId,
|
||||
parentEmail: new Email($parentEmail),
|
||||
code: new InvitationCode($code),
|
||||
createdAt: new DateTimeImmutable('2026-02-07 10:00:00'),
|
||||
createdBy: UserId::generate(),
|
||||
);
|
||||
$invitation->pullDomainEvents();
|
||||
$this->invitationRepository->save($invitation);
|
||||
|
||||
return $invitation;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user