feat: Calculer automatiquement les moyennes après chaque saisie de notes
Some checks failed
CI / Backend Tests (push) Has been cancelled
CI / Frontend Tests (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled
CI / Naming Conventions (push) Has been cancelled
CI / Build Check (push) Has been cancelled

Les enseignants ont besoin de moyennes à jour immédiatement après la
publication ou modification des notes, sans attendre un batch nocturne.

Le système recalcule via Domain Events synchrones : statistiques
d'évaluation (min/max/moyenne/médiane), moyennes matières pondérées
(normalisation /20), et moyenne générale par élève. Les résultats sont
stockés dans des tables dénormalisées avec cache Redis (TTL 5 min).

Trois endpoints API exposent les données avec contrôle d'accès par rôle.
Une commande console permet le backfill des données historiques au
déploiement.
This commit is contained in:
2026-03-30 06:22:03 +02:00
parent b70d5ec2ad
commit aedde6707e
694 changed files with 109792 additions and 75 deletions

View File

@@ -0,0 +1,113 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Scolarite\Infrastructure\Cache;
use App\Scolarite\Domain\Model\Evaluation\ClassStatistics;
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
use App\Scolarite\Infrastructure\Cache\CachingEvaluationStatisticsRepository;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationStatisticsRepository;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
final class CachingEvaluationStatisticsRepositoryTest extends TestCase
{
private InMemoryEvaluationStatisticsRepository $inner;
private CachingEvaluationStatisticsRepository $cached;
protected function setUp(): void
{
$this->inner = new InMemoryEvaluationStatisticsRepository();
$this->cached = new CachingEvaluationStatisticsRepository(
inner: $this->inner,
cache: new ArrayAdapter(),
);
}
#[Test]
public function itCachesStatisticsOnRead(): void
{
$evaluationId = EvaluationId::generate();
$stats = new ClassStatistics(average: 14.5, min: 8.0, max: 19.0, median: 15.0, gradedCount: 5);
$this->inner->save($evaluationId, $stats);
// Premier appel : va au inner
$result1 = $this->cached->findByEvaluation($evaluationId);
self::assertNotNull($result1);
self::assertSame(14.5, $result1->average);
// Deuxième appel : devrait venir du cache (même résultat)
$result2 = $this->cached->findByEvaluation($evaluationId);
self::assertNotNull($result2);
self::assertSame(14.5, $result2->average);
}
#[Test]
public function itInvalidatesCacheOnSave(): void
{
$evaluationId = EvaluationId::generate();
$stats1 = new ClassStatistics(average: 14.0, min: 8.0, max: 19.0, median: 14.0, gradedCount: 3);
// Sauvegarder et lire pour remplir le cache
$this->cached->save($evaluationId, $stats1);
$this->cached->findByEvaluation($evaluationId);
// Mettre à jour
$stats2 = new ClassStatistics(average: 16.0, min: 10.0, max: 20.0, median: 16.0, gradedCount: 4);
$this->cached->save($evaluationId, $stats2);
$result = $this->cached->findByEvaluation($evaluationId);
self::assertNotNull($result);
self::assertSame(16.0, $result->average);
}
#[Test]
public function itReturnsNullForUnknownEvaluation(): void
{
$result = $this->cached->findByEvaluation(EvaluationId::generate());
self::assertNull($result);
}
#[Test]
public function itInvalidatesCacheOnDelete(): void
{
$evaluationId = EvaluationId::generate();
$stats = new ClassStatistics(average: 14.5, min: 8.0, max: 19.0, median: 15.0, gradedCount: 5);
$this->cached->save($evaluationId, $stats);
// Remplir le cache
$this->cached->findByEvaluation($evaluationId);
// Supprimer
$this->cached->delete($evaluationId);
// Le cache ne doit plus retourner l'ancienne valeur
self::assertNull($this->cached->findByEvaluation($evaluationId));
}
#[Test]
public function itCachesNullResultForUnknownEvaluation(): void
{
$evaluationId = EvaluationId::generate();
// Premier appel : null → mis en cache
self::assertNull($this->cached->findByEvaluation($evaluationId));
// Sauvegarder directement dans inner (sans passer par le cache)
$this->inner->save($evaluationId, new ClassStatistics(
average: 12.0,
min: 10.0,
max: 14.0,
median: 12.0,
gradedCount: 2,
));
// Le cache retourne encore null (valeur cachée)
$result = $this->cached->findByEvaluation($evaluationId);
self::assertNull($result);
}
}

View File

@@ -0,0 +1,152 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Scolarite\Infrastructure\Cache;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\User\UserId;
use App\Scolarite\Infrastructure\Cache\CachingStudentAverageRepository;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryStudentAverageRepository;
use App\Shared\Domain\Tenant\TenantId;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
final class CachingStudentAverageRepositoryTest extends TestCase
{
private const string TENANT_ID = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';
private const string STUDENT_ID = '22222222-2222-2222-2222-222222222222';
private const string SUBJECT_ID = '66666666-6666-6666-6666-666666666666';
private const string PERIOD_ID = '11111111-1111-1111-1111-111111111111';
private InMemoryStudentAverageRepository $inner;
private CachingStudentAverageRepository $cached;
protected function setUp(): void
{
$this->inner = new InMemoryStudentAverageRepository();
$this->cached = new CachingStudentAverageRepository(
inner: $this->inner,
cache: new ArrayAdapter(),
);
}
#[Test]
public function itCachesSubjectAveragesOnRead(): void
{
$tenantId = TenantId::fromString(self::TENANT_ID);
$studentId = UserId::fromString(self::STUDENT_ID);
$this->inner->saveSubjectAverage(
$tenantId,
$studentId,
SubjectId::fromString(self::SUBJECT_ID),
self::PERIOD_ID,
15.0,
3,
);
$result1 = $this->cached->findSubjectAveragesForStudent($studentId, self::PERIOD_ID, $tenantId);
$result2 = $this->cached->findSubjectAveragesForStudent($studentId, self::PERIOD_ID, $tenantId);
self::assertSame([15.0], $result1);
self::assertSame([15.0], $result2);
}
#[Test]
public function itInvalidatesCacheOnSaveSubjectAverage(): void
{
$tenantId = TenantId::fromString(self::TENANT_ID);
$studentId = UserId::fromString(self::STUDENT_ID);
$subjectId = SubjectId::fromString(self::SUBJECT_ID);
$this->cached->saveSubjectAverage($tenantId, $studentId, $subjectId, self::PERIOD_ID, 14.0, 2);
// Remplir le cache
$this->cached->findSubjectAveragesForStudent($studentId, self::PERIOD_ID, $tenantId);
// Mettre à jour → doit invalider le cache
$this->cached->saveSubjectAverage($tenantId, $studentId, $subjectId, self::PERIOD_ID, 16.0, 3);
$result = $this->cached->findSubjectAveragesForStudent($studentId, self::PERIOD_ID, $tenantId);
self::assertSame([16.0], $result);
}
#[Test]
public function itReturnsEmptyArrayWhenNoAverages(): void
{
$result = $this->cached->findSubjectAveragesForStudent(
UserId::fromString(self::STUDENT_ID),
self::PERIOD_ID,
TenantId::fromString(self::TENANT_ID),
);
self::assertSame([], $result);
}
#[Test]
public function itDelegatesToInnerForGeneralAverage(): void
{
$tenantId = TenantId::fromString(self::TENANT_ID);
$studentId = UserId::fromString(self::STUDENT_ID);
$this->cached->saveGeneralAverage($tenantId, $studentId, self::PERIOD_ID, 13.5);
$result = $this->cached->findGeneralAverageForStudent($studentId, self::PERIOD_ID, $tenantId);
self::assertSame(13.5, $result);
}
#[Test]
public function itInvalidatesCacheOnDeleteSubjectAverage(): void
{
$tenantId = TenantId::fromString(self::TENANT_ID);
$studentId = UserId::fromString(self::STUDENT_ID);
$subjectId = SubjectId::fromString(self::SUBJECT_ID);
$this->cached->saveSubjectAverage($tenantId, $studentId, $subjectId, self::PERIOD_ID, 14.0, 2);
// Remplir le cache
$this->cached->findSubjectAveragesForStudent($studentId, self::PERIOD_ID, $tenantId);
// Supprimer → doit invalider le cache
$this->cached->deleteSubjectAverage($studentId, $subjectId, self::PERIOD_ID, $tenantId);
$result = $this->cached->findSubjectAveragesForStudent($studentId, self::PERIOD_ID, $tenantId);
self::assertSame([], $result);
}
#[Test]
public function itCachesMultipleSubjectAverages(): void
{
$tenantId = TenantId::fromString(self::TENANT_ID);
$studentId = UserId::fromString(self::STUDENT_ID);
$subject2Id = '77777777-7777-7777-7777-777777777777';
$this->cached->saveSubjectAverage(
$tenantId,
$studentId,
SubjectId::fromString(self::SUBJECT_ID),
self::PERIOD_ID,
15.0,
3,
);
$this->cached->saveSubjectAverage(
$tenantId,
$studentId,
SubjectId::fromString($subject2Id),
self::PERIOD_ID,
12.0,
2,
);
$result = $this->cached->findSubjectAveragesForStudent($studentId, self::PERIOD_ID, $tenantId);
self::assertCount(2, $result);
self::assertContains(15.0, $result);
self::assertContains(12.0, $result);
}
}

View File

@@ -0,0 +1,280 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Scolarite\Infrastructure\Console;
use App\Administration\Domain\Model\SchoolClass\ClassId;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\User\UserId;
use App\Scolarite\Application\Port\PeriodFinder;
use App\Scolarite\Application\Port\PeriodInfo;
use App\Scolarite\Application\Service\RecalculerMoyennesService;
use App\Scolarite\Domain\Model\Evaluation\Coefficient;
use App\Scolarite\Domain\Model\Evaluation\Evaluation;
use App\Scolarite\Domain\Model\Evaluation\GradeScale;
use App\Scolarite\Domain\Model\Grade\Grade;
use App\Scolarite\Domain\Model\Grade\GradeStatus;
use App\Scolarite\Domain\Model\Grade\GradeValue;
use App\Scolarite\Domain\Service\AverageCalculator;
use App\Scolarite\Infrastructure\Console\RecalculerToutesMoyennesCommand;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationRepository;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationStatisticsRepository;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryGradeRepository;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryStudentAverageRepository;
use App\Shared\Domain\Tenant\TenantId;
use App\Shared\Infrastructure\Tenant\TenantConfig;
use App\Shared\Infrastructure\Tenant\TenantContext;
use App\Shared\Infrastructure\Tenant\TenantDatabaseSwitcher;
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
use App\Shared\Infrastructure\Tenant\TenantRegistry;
use DateTimeImmutable;
use Override;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
final class RecalculerToutesMoyennesCommandTest extends TestCase
{
private const string TENANT_ID = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';
public const string PERIOD_ID = '11111111-1111-1111-1111-111111111111';
private const string STUDENT_ID = '22222222-2222-2222-2222-222222222222';
private const string TEACHER_ID = '44444444-4444-4444-4444-444444444444';
private const string CLASS_ID = '55555555-5555-5555-5555-555555555555';
private const string SUBJECT_ID = '66666666-6666-6666-6666-666666666666';
private InMemoryEvaluationRepository $evaluationRepo;
private InMemoryGradeRepository $gradeRepo;
private InMemoryEvaluationStatisticsRepository $evalStatsRepo;
private InMemoryStudentAverageRepository $studentAvgRepo;
private TenantConfig $tenantConfig;
protected function setUp(): void
{
$this->evaluationRepo = new InMemoryEvaluationRepository();
$this->gradeRepo = new InMemoryGradeRepository();
$this->evalStatsRepo = new InMemoryEvaluationStatisticsRepository();
$this->studentAvgRepo = new InMemoryStudentAverageRepository();
$this->tenantConfig = new TenantConfig(
tenantId: InfraTenantId::fromString(self::TENANT_ID),
subdomain: 'ecole-test',
databaseUrl: 'postgresql://test',
);
}
#[Test]
public function itBackfillsStatisticsAndAveragesForPublishedEvaluations(): void
{
$this->seedPublishedEvaluationWithGrades();
$tester = $this->executeCommand();
self::assertSame(0, $tester->getStatusCode(), $tester->getDisplay());
self::assertStringContainsString('1 évaluation(s) publiée(s)', $tester->getDisplay());
self::assertStringContainsString('1 évaluation(s) traitée(s) avec succès', $tester->getDisplay());
// Vérifier que les stats évaluation sont créées
$evaluations = $this->evaluationRepo->findAllWithPublishedGrades(TenantId::fromString(self::TENANT_ID));
$stats = $this->evalStatsRepo->findByEvaluation($evaluations[0]->id);
self::assertNotNull($stats);
self::assertSame(2, $stats->gradedCount);
// Vérifier que la moyenne matière est créée
$subjectAvg = $this->studentAvgRepo->findSubjectAverage(
UserId::fromString(self::STUDENT_ID),
SubjectId::fromString(self::SUBJECT_ID),
self::PERIOD_ID,
TenantId::fromString(self::TENANT_ID),
);
self::assertNotNull($subjectAvg);
self::assertSame(14.0, $subjectAvg['average']);
}
#[Test]
public function itReportsSuccessWhenNoPublishedEvaluations(): void
{
$tester = $this->executeCommand();
self::assertSame(0, $tester->getStatusCode());
self::assertStringContainsString('aucune évaluation publiée', $tester->getDisplay());
self::assertStringContainsString('0 évaluation(s) traitée(s) avec succès', $tester->getDisplay());
}
#[Test]
public function itIgnoresUnpublishedEvaluations(): void
{
$tenantId = TenantId::fromString(self::TENANT_ID);
$now = new DateTimeImmutable();
// Évaluation NON publiée
$evaluation = Evaluation::creer(
tenantId: $tenantId,
classId: ClassId::fromString(self::CLASS_ID),
subjectId: SubjectId::fromString(self::SUBJECT_ID),
teacherId: UserId::fromString(self::TEACHER_ID),
title: 'Non publiée',
description: null,
evaluationDate: new DateTimeImmutable('2026-02-15'),
gradeScale: new GradeScale(20),
coefficient: new Coefficient(1.0),
now: $now,
);
$evaluation->pullDomainEvents();
$this->evaluationRepo->save($evaluation);
$tester = $this->executeCommand();
self::assertSame(0, $tester->getStatusCode());
self::assertStringContainsString('aucune évaluation publiée', $tester->getDisplay());
}
#[Test]
public function itProcessesMultipleEvaluations(): void
{
$this->seedPublishedEvaluationWithGrades();
$this->seedPublishedEvaluationWithGrades(coefficient: 2.0);
$tester = $this->executeCommand();
self::assertSame(0, $tester->getStatusCode());
self::assertStringContainsString('2 évaluation(s) publiée(s)', $tester->getDisplay());
self::assertStringContainsString('2 évaluation(s) traitée(s) avec succès', $tester->getDisplay());
}
private function seedPublishedEvaluationWithGrades(float $coefficient = 1.0): void
{
$tenantId = TenantId::fromString(self::TENANT_ID);
$now = new DateTimeImmutable();
$evaluation = Evaluation::creer(
tenantId: $tenantId,
classId: ClassId::fromString(self::CLASS_ID),
subjectId: SubjectId::fromString(self::SUBJECT_ID),
teacherId: UserId::fromString(self::TEACHER_ID),
title: 'Contrôle',
description: null,
evaluationDate: new DateTimeImmutable('2026-02-15'),
gradeScale: new GradeScale(20),
coefficient: new Coefficient($coefficient),
now: $now,
);
$evaluation->publierNotes($now);
$evaluation->pullDomainEvents();
$this->evaluationRepo->save($evaluation);
$grade1 = Grade::saisir(
tenantId: $tenantId,
evaluationId: $evaluation->id,
studentId: UserId::fromString(self::STUDENT_ID),
value: new GradeValue(14.0),
status: GradeStatus::GRADED,
gradeScale: new GradeScale(20),
createdBy: UserId::fromString(self::TEACHER_ID),
now: $now,
);
$grade1->pullDomainEvents();
$this->gradeRepo->save($grade1);
$grade2 = Grade::saisir(
tenantId: $tenantId,
evaluationId: $evaluation->id,
studentId: UserId::fromString('33333333-3333-3333-3333-333333333333'),
value: new GradeValue(10.0),
status: GradeStatus::GRADED,
gradeScale: new GradeScale(20),
createdBy: UserId::fromString(self::TEACHER_ID),
now: $now,
);
$grade2->pullDomainEvents();
$this->gradeRepo->save($grade2);
}
private function executeCommand(): CommandTester
{
$tenantContext = new TenantContext();
$periodFinder = new class implements PeriodFinder {
public function findForDate(DateTimeImmutable $date, TenantId $tenantId): ?PeriodInfo
{
return new PeriodInfo(
periodId: RecalculerToutesMoyennesCommandTest::PERIOD_ID,
startDate: new DateTimeImmutable('2026-01-01'),
endDate: new DateTimeImmutable('2026-03-31'),
);
}
};
$service = new RecalculerMoyennesService(
evaluationRepository: $this->evaluationRepo,
gradeRepository: $this->gradeRepo,
evaluationStatisticsRepository: $this->evalStatsRepo,
studentAverageRepository: $this->studentAvgRepo,
periodFinder: $periodFinder,
calculator: new AverageCalculator(),
);
$tenantRegistry = new class($this->tenantConfig) implements TenantRegistry {
public function __construct(private readonly TenantConfig $config)
{
}
#[Override]
public function getConfig(InfraTenantId $tenantId): TenantConfig
{
return $this->config;
}
#[Override]
public function getBySubdomain(string $subdomain): TenantConfig
{
return $this->config;
}
#[Override]
public function exists(string $subdomain): bool
{
return true;
}
#[Override]
public function getAllConfigs(): array
{
return [$this->config];
}
};
$databaseSwitcher = new class implements TenantDatabaseSwitcher {
#[Override]
public function useTenantDatabase(string $databaseUrl): void
{
}
#[Override]
public function useDefaultDatabase(): void
{
}
#[Override]
public function currentDatabaseUrl(): ?string
{
return null;
}
};
$command = new RecalculerToutesMoyennesCommand(
evaluationRepository: $this->evaluationRepo,
tenantRegistry: $tenantRegistry,
tenantContext: $tenantContext,
databaseSwitcher: $databaseSwitcher,
service: $service,
);
$tester = new CommandTester($command);
$tester->execute([]);
return $tester;
}
}

View File

@@ -0,0 +1,302 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Scolarite\Infrastructure\EventHandler;
use App\Administration\Domain\Model\SchoolClass\ClassId;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\User\UserId;
use App\Scolarite\Application\Port\PeriodFinder;
use App\Scolarite\Application\Port\PeriodInfo;
use App\Scolarite\Application\Service\RecalculerMoyennesService;
use App\Scolarite\Domain\Event\EvaluationModifiee;
use App\Scolarite\Domain\Model\Evaluation\Coefficient;
use App\Scolarite\Domain\Model\Evaluation\Evaluation;
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
use App\Scolarite\Domain\Model\Evaluation\GradeScale;
use App\Scolarite\Domain\Model\Grade\Grade;
use App\Scolarite\Domain\Model\Grade\GradeStatus;
use App\Scolarite\Domain\Model\Grade\GradeValue;
use App\Scolarite\Domain\Service\AverageCalculator;
use App\Scolarite\Infrastructure\EventHandler\RecalculerMoyennesOnEvaluationModifieeHandler;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationRepository;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationStatisticsRepository;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryGradeRepository;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryStudentAverageRepository;
use App\Shared\Domain\Tenant\TenantId;
use App\Shared\Infrastructure\Tenant\TenantConfig;
use App\Shared\Infrastructure\Tenant\TenantContext;
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class RecalculerMoyennesOnEvaluationModifieeHandlerTest extends TestCase
{
private const string TENANT_ID = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';
public const string PERIOD_ID = '11111111-1111-1111-1111-111111111111';
private const string STUDENT_1 = '22222222-2222-2222-2222-222222222222';
private const string STUDENT_2 = '33333333-3333-3333-3333-333333333333';
private const string TEACHER_ID = '44444444-4444-4444-4444-444444444444';
private const string CLASS_ID = '55555555-5555-5555-5555-555555555555';
private const string SUBJECT_ID = '66666666-6666-6666-6666-666666666666';
private InMemoryEvaluationRepository $evaluationRepo;
private InMemoryGradeRepository $gradeRepo;
private InMemoryEvaluationStatisticsRepository $evalStatsRepo;
private InMemoryStudentAverageRepository $studentAvgRepo;
private RecalculerMoyennesOnEvaluationModifieeHandler $handler;
protected function setUp(): void
{
$this->evaluationRepo = new InMemoryEvaluationRepository();
$this->gradeRepo = new InMemoryGradeRepository();
$this->evalStatsRepo = new InMemoryEvaluationStatisticsRepository();
$this->studentAvgRepo = new InMemoryStudentAverageRepository();
$tenantContext = new TenantContext();
$tenantContext->setCurrentTenant(new TenantConfig(
tenantId: InfraTenantId::fromString(self::TENANT_ID),
subdomain: 'test',
databaseUrl: 'postgresql://test',
));
$periodFinder = new class implements PeriodFinder {
public function findForDate(DateTimeImmutable $date, TenantId $tenantId): ?PeriodInfo
{
return new PeriodInfo(
periodId: RecalculerMoyennesOnEvaluationModifieeHandlerTest::PERIOD_ID,
startDate: new DateTimeImmutable('2026-01-01'),
endDate: new DateTimeImmutable('2026-03-31'),
);
}
};
$service = new RecalculerMoyennesService(
evaluationRepository: $this->evaluationRepo,
gradeRepository: $this->gradeRepo,
evaluationStatisticsRepository: $this->evalStatsRepo,
studentAverageRepository: $this->studentAvgRepo,
periodFinder: $periodFinder,
calculator: new AverageCalculator(),
);
$this->handler = new RecalculerMoyennesOnEvaluationModifieeHandler(
tenantContext: $tenantContext,
service: $service,
);
}
#[Test]
public function itRecalculatesStatisticsWhenEvaluationModified(): void
{
$evaluationId = $this->seedPublishedEvaluationWithGrades(
grades: [
[self::STUDENT_1, 14.0, GradeStatus::GRADED],
[self::STUDENT_2, 8.0, GradeStatus::GRADED],
],
);
($this->handler)(new EvaluationModifiee(
evaluationId: $evaluationId,
title: 'Titre modifié',
evaluationDate: new DateTimeImmutable('2026-02-15'),
occurredOn: new DateTimeImmutable(),
));
$stats = $this->evalStatsRepo->findByEvaluation($evaluationId);
self::assertNotNull($stats);
self::assertSame(11.0, $stats->average);
self::assertSame(8.0, $stats->min);
self::assertSame(14.0, $stats->max);
self::assertSame(2, $stats->gradedCount);
}
#[Test]
public function itRecalculatesStudentAveragesWhenEvaluationModified(): void
{
$evaluationId = $this->seedPublishedEvaluationWithGrades(
grades: [
[self::STUDENT_1, 16.0, GradeStatus::GRADED],
[self::STUDENT_2, 12.0, GradeStatus::GRADED],
],
);
($this->handler)(new EvaluationModifiee(
evaluationId: $evaluationId,
title: 'Titre modifié',
evaluationDate: new DateTimeImmutable('2026-02-15'),
occurredOn: new DateTimeImmutable(),
));
$student1Avg = $this->studentAvgRepo->findSubjectAverage(
UserId::fromString(self::STUDENT_1),
SubjectId::fromString(self::SUBJECT_ID),
self::PERIOD_ID,
TenantId::fromString(self::TENANT_ID),
);
self::assertNotNull($student1Avg);
self::assertSame(16.0, $student1Avg['average']);
$student2Avg = $this->studentAvgRepo->findSubjectAverage(
UserId::fromString(self::STUDENT_2),
SubjectId::fromString(self::SUBJECT_ID),
self::PERIOD_ID,
TenantId::fromString(self::TENANT_ID),
);
self::assertNotNull($student2Avg);
self::assertSame(12.0, $student2Avg['average']);
}
#[Test]
public function itRecalculatesGeneralAverageForAllStudents(): void
{
$evaluationId = $this->seedPublishedEvaluationWithGrades(
grades: [
[self::STUDENT_1, 14.0, GradeStatus::GRADED],
],
);
($this->handler)(new EvaluationModifiee(
evaluationId: $evaluationId,
title: 'Titre modifié',
evaluationDate: new DateTimeImmutable('2026-02-15'),
occurredOn: new DateTimeImmutable(),
));
$generalAvg = $this->studentAvgRepo->findGeneralAverageForStudent(
UserId::fromString(self::STUDENT_1),
self::PERIOD_ID,
TenantId::fromString(self::TENANT_ID),
);
self::assertSame(14.0, $generalAvg);
}
#[Test]
public function itRecalculatesStatsButNotStudentAveragesWhenNotPublished(): void
{
$evaluationId = $this->seedUnpublishedEvaluationWithGrades(
grades: [
[self::STUDENT_1, 14.0, GradeStatus::GRADED],
],
);
($this->handler)(new EvaluationModifiee(
evaluationId: $evaluationId,
title: 'Titre modifié',
evaluationDate: new DateTimeImmutable('2026-02-15'),
occurredOn: new DateTimeImmutable(),
));
// Les stats sont calculées (le handler ne filtre pas sur publication)
$stats = $this->evalStatsRepo->findByEvaluation($evaluationId);
self::assertNotNull($stats);
self::assertSame(14.0, $stats->average);
// Mais pas de recalcul des moyennes élèves (recalculerTousEleves filtre)
self::assertNull($this->studentAvgRepo->findSubjectAverage(
UserId::fromString(self::STUDENT_1),
SubjectId::fromString(self::SUBJECT_ID),
self::PERIOD_ID,
TenantId::fromString(self::TENANT_ID),
));
}
#[Test]
public function itExcludesAbsentStudentsFromStatistics(): void
{
$evaluationId = $this->seedPublishedEvaluationWithGrades(
grades: [
[self::STUDENT_1, 18.0, GradeStatus::GRADED],
[self::STUDENT_2, null, GradeStatus::ABSENT],
],
);
($this->handler)(new EvaluationModifiee(
evaluationId: $evaluationId,
title: 'Titre modifié',
evaluationDate: new DateTimeImmutable('2026-02-15'),
occurredOn: new DateTimeImmutable(),
));
$stats = $this->evalStatsRepo->findByEvaluation($evaluationId);
self::assertNotNull($stats);
self::assertSame(18.0, $stats->average);
self::assertSame(1, $stats->gradedCount);
}
/**
* @param list<array{0: string, 1: float|null, 2: GradeStatus}> $grades
*/
private function seedPublishedEvaluationWithGrades(
array $grades,
float $coefficient = 1.0,
): EvaluationId {
return $this->seedEvaluationWithGrades($grades, $coefficient, published: true);
}
/**
* @param list<array{0: string, 1: float|null, 2: GradeStatus}> $grades
*/
private function seedUnpublishedEvaluationWithGrades(
array $grades,
float $coefficient = 1.0,
): EvaluationId {
return $this->seedEvaluationWithGrades($grades, $coefficient, published: false);
}
/**
* @param list<array{0: string, 1: float|null, 2: GradeStatus}> $grades
*/
private function seedEvaluationWithGrades(
array $grades,
float $coefficient = 1.0,
bool $published = true,
): EvaluationId {
$tenantId = TenantId::fromString(self::TENANT_ID);
$now = new DateTimeImmutable();
$evaluation = Evaluation::creer(
tenantId: $tenantId,
classId: ClassId::fromString(self::CLASS_ID),
subjectId: SubjectId::fromString(self::SUBJECT_ID),
teacherId: UserId::fromString(self::TEACHER_ID),
title: 'Test Evaluation',
description: null,
evaluationDate: new DateTimeImmutable('2026-02-15'),
gradeScale: new GradeScale(20),
coefficient: new Coefficient($coefficient),
now: $now,
);
if ($published) {
$evaluation->publierNotes($now);
}
$evaluation->pullDomainEvents();
$this->evaluationRepo->save($evaluation);
foreach ($grades as [$studentId, $value, $status]) {
$grade = Grade::saisir(
tenantId: $tenantId,
evaluationId: $evaluation->id,
studentId: UserId::fromString($studentId),
value: $value !== null ? new GradeValue($value) : null,
status: $status,
gradeScale: new GradeScale(20),
createdBy: UserId::fromString(self::TEACHER_ID),
now: $now,
);
$grade->pullDomainEvents();
$this->gradeRepo->save($grade);
}
return $evaluation->id;
}
}

View File

@@ -0,0 +1,342 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Scolarite\Infrastructure\EventHandler;
use App\Administration\Domain\Model\SchoolClass\ClassId;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\User\UserId;
use App\Scolarite\Application\Port\PeriodFinder;
use App\Scolarite\Application\Port\PeriodInfo;
use App\Scolarite\Application\Service\RecalculerMoyennesService;
use App\Scolarite\Domain\Event\EvaluationSupprimee;
use App\Scolarite\Domain\Model\Evaluation\ClassStatistics;
use App\Scolarite\Domain\Model\Evaluation\Coefficient;
use App\Scolarite\Domain\Model\Evaluation\Evaluation;
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
use App\Scolarite\Domain\Model\Evaluation\GradeScale;
use App\Scolarite\Domain\Model\Grade\Grade;
use App\Scolarite\Domain\Model\Grade\GradeStatus;
use App\Scolarite\Domain\Model\Grade\GradeValue;
use App\Scolarite\Domain\Service\AverageCalculator;
use App\Scolarite\Infrastructure\EventHandler\RecalculerMoyennesOnEvaluationSupprimeeHandler;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationRepository;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationStatisticsRepository;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryGradeRepository;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryStudentAverageRepository;
use App\Shared\Domain\Tenant\TenantId;
use App\Shared\Infrastructure\Tenant\TenantConfig;
use App\Shared\Infrastructure\Tenant\TenantContext;
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class RecalculerMoyennesOnEvaluationSupprimeeHandlerTest extends TestCase
{
private const string TENANT_ID = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';
public const string PERIOD_ID = '11111111-1111-1111-1111-111111111111';
private const string STUDENT_1 = '22222222-2222-2222-2222-222222222222';
private const string STUDENT_2 = '33333333-3333-3333-3333-333333333333';
private const string TEACHER_ID = '44444444-4444-4444-4444-444444444444';
private const string CLASS_ID = '55555555-5555-5555-5555-555555555555';
private const string SUBJECT_ID = '66666666-6666-6666-6666-666666666666';
private InMemoryEvaluationRepository $evaluationRepo;
private InMemoryGradeRepository $gradeRepo;
private InMemoryEvaluationStatisticsRepository $evalStatsRepo;
private InMemoryStudentAverageRepository $studentAvgRepo;
private RecalculerMoyennesOnEvaluationSupprimeeHandler $handler;
protected function setUp(): void
{
$this->evaluationRepo = new InMemoryEvaluationRepository();
$this->gradeRepo = new InMemoryGradeRepository();
$this->evalStatsRepo = new InMemoryEvaluationStatisticsRepository();
$this->studentAvgRepo = new InMemoryStudentAverageRepository();
$tenantContext = new TenantContext();
$tenantContext->setCurrentTenant(new TenantConfig(
tenantId: InfraTenantId::fromString(self::TENANT_ID),
subdomain: 'test',
databaseUrl: 'postgresql://test',
));
$periodFinder = new class implements PeriodFinder {
public function findForDate(DateTimeImmutable $date, TenantId $tenantId): ?PeriodInfo
{
return new PeriodInfo(
periodId: RecalculerMoyennesOnEvaluationSupprimeeHandlerTest::PERIOD_ID,
startDate: new DateTimeImmutable('2026-01-01'),
endDate: new DateTimeImmutable('2026-03-31'),
);
}
};
$service = new RecalculerMoyennesService(
evaluationRepository: $this->evaluationRepo,
gradeRepository: $this->gradeRepo,
evaluationStatisticsRepository: $this->evalStatsRepo,
studentAverageRepository: $this->studentAvgRepo,
periodFinder: $periodFinder,
calculator: new AverageCalculator(),
);
$this->handler = new RecalculerMoyennesOnEvaluationSupprimeeHandler(
tenantContext: $tenantContext,
evaluationRepository: $this->evaluationRepo,
gradeRepository: $this->gradeRepo,
evaluationStatisticsRepository: $this->evalStatsRepo,
periodFinder: $periodFinder,
service: $service,
);
}
#[Test]
public function itDeletesEvaluationStatisticsOnDeletion(): void
{
$evaluationId = $this->seedPublishedEvaluationWithGrades(
grades: [
[self::STUDENT_1, 14.0, GradeStatus::GRADED],
[self::STUDENT_2, 10.0, GradeStatus::GRADED],
],
);
// Pré-remplir les stats
$this->evalStatsRepo->save($evaluationId, new ClassStatistics(
average: 12.0,
min: 10.0,
max: 14.0,
median: 12.0,
gradedCount: 2,
));
self::assertNotNull($this->evalStatsRepo->findByEvaluation($evaluationId));
($this->handler)(new EvaluationSupprimee(
evaluationId: $evaluationId,
occurredOn: new DateTimeImmutable(),
));
self::assertNull($this->evalStatsRepo->findByEvaluation($evaluationId));
}
#[Test]
public function itRecalculatesStudentAveragesAfterDeletion(): void
{
$tenantId = TenantId::fromString(self::TENANT_ID);
// Première évaluation (sera supprimée)
$evalToDelete = $this->seedPublishedEvaluationWithGrades(
grades: [
[self::STUDENT_1, 10.0, GradeStatus::GRADED],
],
);
// Deuxième évaluation (reste)
$evalRemaining = $this->seedPublishedEvaluationWithGrades(
grades: [
[self::STUDENT_1, 18.0, GradeStatus::GRADED],
],
);
// Pré-remplir les moyennes (comme si les deux évaluations comptaient)
$this->studentAvgRepo->saveSubjectAverage(
$tenantId,
UserId::fromString(self::STUDENT_1),
SubjectId::fromString(self::SUBJECT_ID),
self::PERIOD_ID,
14.0, // (10+18)/2
2,
);
// Supprimer la première évaluation (status DELETED mais encore accessible)
$evaluation = $this->evaluationRepo->findById($evalToDelete, $tenantId);
$evaluation->supprimer(new DateTimeImmutable());
$evaluation->pullDomainEvents();
$this->evaluationRepo->save($evaluation);
($this->handler)(new EvaluationSupprimee(
evaluationId: $evalToDelete,
occurredOn: new DateTimeImmutable(),
));
// La moyenne doit être recalculée sans l'évaluation supprimée
$subjectAvg = $this->studentAvgRepo->findSubjectAverage(
UserId::fromString(self::STUDENT_1),
SubjectId::fromString(self::SUBJECT_ID),
self::PERIOD_ID,
$tenantId,
);
self::assertNotNull($subjectAvg);
self::assertSame(18.0, $subjectAvg['average']);
self::assertSame(1, $subjectAvg['gradeCount']);
}
#[Test]
public function itDoesNothingWhenEvaluationNotFound(): void
{
$unknownId = EvaluationId::generate();
($this->handler)(new EvaluationSupprimee(
evaluationId: $unknownId,
occurredOn: new DateTimeImmutable(),
));
self::assertNull($this->evalStatsRepo->findByEvaluation($unknownId));
}
#[Test]
public function itOnlyDeletesStatsWhenGradesWereNotPublished(): void
{
$evaluationId = $this->seedUnpublishedEvaluationWithGrades(
grades: [
[self::STUDENT_1, 14.0, GradeStatus::GRADED],
],
);
// Pré-remplir des stats (cas hypothétique)
$this->evalStatsRepo->save($evaluationId, new ClassStatistics(
average: 14.0,
min: 14.0,
max: 14.0,
median: 14.0,
gradedCount: 1,
));
($this->handler)(new EvaluationSupprimee(
evaluationId: $evaluationId,
occurredOn: new DateTimeImmutable(),
));
// Stats supprimées
self::assertNull($this->evalStatsRepo->findByEvaluation($evaluationId));
// Pas de recalcul de moyennes élèves (notes non publiées)
self::assertNull($this->studentAvgRepo->findSubjectAverage(
UserId::fromString(self::STUDENT_1),
SubjectId::fromString(self::SUBJECT_ID),
self::PERIOD_ID,
TenantId::fromString(self::TENANT_ID),
));
}
#[Test]
public function itRecalculatesGeneralAverageAfterDeletion(): void
{
$tenantId = TenantId::fromString(self::TENANT_ID);
$evaluationId = $this->seedPublishedEvaluationWithGrades(
grades: [
[self::STUDENT_1, 14.0, GradeStatus::GRADED],
],
);
// Pré-remplir
$this->studentAvgRepo->saveSubjectAverage(
$tenantId,
UserId::fromString(self::STUDENT_1),
SubjectId::fromString(self::SUBJECT_ID),
self::PERIOD_ID,
14.0,
1,
);
$this->studentAvgRepo->saveGeneralAverage(
$tenantId,
UserId::fromString(self::STUDENT_1),
self::PERIOD_ID,
14.0,
);
// Supprimer l'évaluation
$evaluation = $this->evaluationRepo->findById($evaluationId, $tenantId);
$evaluation->supprimer(new DateTimeImmutable());
$evaluation->pullDomainEvents();
$this->evaluationRepo->save($evaluation);
($this->handler)(new EvaluationSupprimee(
evaluationId: $evaluationId,
occurredOn: new DateTimeImmutable(),
));
// Plus aucune note publiée → moyennes supprimées
$generalAvg = $this->studentAvgRepo->findGeneralAverageForStudent(
UserId::fromString(self::STUDENT_1),
self::PERIOD_ID,
$tenantId,
);
self::assertNull($generalAvg);
}
/**
* @param list<array{0: string, 1: float|null, 2: GradeStatus}> $grades
*/
private function seedPublishedEvaluationWithGrades(
array $grades,
float $coefficient = 1.0,
): EvaluationId {
return $this->seedEvaluationWithGrades($grades, $coefficient, published: true);
}
/**
* @param list<array{0: string, 1: float|null, 2: GradeStatus}> $grades
*/
private function seedUnpublishedEvaluationWithGrades(
array $grades,
float $coefficient = 1.0,
): EvaluationId {
return $this->seedEvaluationWithGrades($grades, $coefficient, published: false);
}
/**
* @param list<array{0: string, 1: float|null, 2: GradeStatus}> $grades
*/
private function seedEvaluationWithGrades(
array $grades,
float $coefficient = 1.0,
bool $published = true,
): EvaluationId {
$tenantId = TenantId::fromString(self::TENANT_ID);
$now = new DateTimeImmutable();
$evaluation = Evaluation::creer(
tenantId: $tenantId,
classId: ClassId::fromString(self::CLASS_ID),
subjectId: SubjectId::fromString(self::SUBJECT_ID),
teacherId: UserId::fromString(self::TEACHER_ID),
title: 'Test Evaluation',
description: null,
evaluationDate: new DateTimeImmutable('2026-02-15'),
gradeScale: new GradeScale(20),
coefficient: new Coefficient($coefficient),
now: $now,
);
if ($published) {
$evaluation->publierNotes($now);
}
$evaluation->pullDomainEvents();
$this->evaluationRepo->save($evaluation);
foreach ($grades as [$studentId, $value, $status]) {
$grade = Grade::saisir(
tenantId: $tenantId,
evaluationId: $evaluation->id,
studentId: UserId::fromString($studentId),
value: $value !== null ? new GradeValue($value) : null,
status: $status,
gradeScale: new GradeScale(20),
createdBy: UserId::fromString(self::TEACHER_ID),
now: $now,
);
$grade->pullDomainEvents();
$this->gradeRepo->save($grade);
}
return $evaluation->id;
}
}

View File

@@ -0,0 +1,328 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Scolarite\Infrastructure\EventHandler;
use App\Administration\Domain\Model\SchoolClass\ClassId;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\User\UserId;
use App\Scolarite\Application\Port\PeriodFinder;
use App\Scolarite\Application\Port\PeriodInfo;
use App\Scolarite\Application\Service\RecalculerMoyennesService;
use App\Scolarite\Domain\Event\NoteModifiee;
use App\Scolarite\Domain\Event\NoteSaisie;
use App\Scolarite\Domain\Model\Evaluation\Coefficient;
use App\Scolarite\Domain\Model\Evaluation\Evaluation;
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
use App\Scolarite\Domain\Model\Evaluation\GradeScale;
use App\Scolarite\Domain\Model\Grade\Grade;
use App\Scolarite\Domain\Model\Grade\GradeId;
use App\Scolarite\Domain\Model\Grade\GradeStatus;
use App\Scolarite\Domain\Model\Grade\GradeValue;
use App\Scolarite\Domain\Service\AverageCalculator;
use App\Scolarite\Infrastructure\EventHandler\RecalculerMoyennesOnNoteModifieeHandler;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationRepository;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationStatisticsRepository;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryGradeRepository;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryStudentAverageRepository;
use App\Shared\Domain\Tenant\TenantId;
use App\Shared\Infrastructure\Tenant\TenantConfig;
use App\Shared\Infrastructure\Tenant\TenantContext;
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class RecalculerMoyennesOnNoteModifieeHandlerTest extends TestCase
{
private const string TENANT_ID = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';
public const string PERIOD_ID = '11111111-1111-1111-1111-111111111111';
private const string STUDENT_ID = '22222222-2222-2222-2222-222222222222';
private const string TEACHER_ID = '44444444-4444-4444-4444-444444444444';
private const string CLASS_ID = '55555555-5555-5555-5555-555555555555';
private const string SUBJECT_ID = '66666666-6666-6666-6666-666666666666';
private InMemoryEvaluationRepository $evaluationRepo;
private InMemoryGradeRepository $gradeRepo;
private InMemoryEvaluationStatisticsRepository $evalStatsRepo;
private InMemoryStudentAverageRepository $studentAvgRepo;
private RecalculerMoyennesOnNoteModifieeHandler $handler;
protected function setUp(): void
{
$this->evaluationRepo = new InMemoryEvaluationRepository();
$this->gradeRepo = new InMemoryGradeRepository();
$this->evalStatsRepo = new InMemoryEvaluationStatisticsRepository();
$this->studentAvgRepo = new InMemoryStudentAverageRepository();
$tenantContext = new TenantContext();
$tenantContext->setCurrentTenant(new TenantConfig(
tenantId: InfraTenantId::fromString(self::TENANT_ID),
subdomain: 'test',
databaseUrl: 'postgresql://test',
));
$periodFinder = new class implements PeriodFinder {
public function findForDate(DateTimeImmutable $date, TenantId $tenantId): ?PeriodInfo
{
return new PeriodInfo(
periodId: RecalculerMoyennesOnNoteModifieeHandlerTest::PERIOD_ID,
startDate: new DateTimeImmutable('2026-01-01'),
endDate: new DateTimeImmutable('2026-03-31'),
);
}
};
$service = new RecalculerMoyennesService(
evaluationRepository: $this->evaluationRepo,
gradeRepository: $this->gradeRepo,
evaluationStatisticsRepository: $this->evalStatsRepo,
studentAverageRepository: $this->studentAvgRepo,
periodFinder: $periodFinder,
calculator: new AverageCalculator(),
);
$this->handler = new RecalculerMoyennesOnNoteModifieeHandler(
tenantContext: $tenantContext,
evaluationRepository: $this->evaluationRepo,
gradeRepository: $this->gradeRepo,
periodFinder: $periodFinder,
service: $service,
);
}
#[Test]
public function itRecalculatesStatisticsWhenGradeModifiedAfterPublication(): void
{
$tenantId = TenantId::fromString(self::TENANT_ID);
$now = new DateTimeImmutable();
$evaluation = $this->seedPublishedEvaluation();
// Deux notes initiales
$grade1 = $this->seedGrade($evaluation->id, self::STUDENT_ID, 14.0, GradeStatus::GRADED);
$this->seedGrade($evaluation->id, '77777777-7777-7777-7777-777777777777', 10.0, GradeStatus::GRADED);
// Simuler modification de la note
$grade1->modifier(
value: new GradeValue(18.0),
status: GradeStatus::GRADED,
gradeScale: new GradeScale(20),
modifiedBy: UserId::fromString(self::TEACHER_ID),
now: $now,
);
$grade1->pullDomainEvents();
$this->gradeRepo->save($grade1);
$event = new NoteModifiee(
gradeId: $grade1->id,
evaluationId: (string) $evaluation->id,
oldValue: 14.0,
newValue: 18.0,
oldStatus: 'graded',
newStatus: 'graded',
modifiedBy: self::TEACHER_ID,
occurredOn: $now,
);
($this->handler)($event);
// Statistiques recalculées
$stats = $this->evalStatsRepo->findByEvaluation($evaluation->id);
self::assertNotNull($stats);
self::assertSame(14.0, $stats->average); // (18+10)/2
self::assertSame(10.0, $stats->min);
self::assertSame(18.0, $stats->max);
// Moyenne matière recalculée pour l'élève
$subjectAvg = $this->studentAvgRepo->findSubjectAverage(
UserId::fromString(self::STUDENT_ID),
SubjectId::fromString(self::SUBJECT_ID),
self::PERIOD_ID,
$tenantId,
);
self::assertNotNull($subjectAvg);
self::assertSame(18.0, $subjectAvg['average']);
}
#[Test]
public function itDoesNothingWhenGradesNotYetPublished(): void
{
$now = new DateTimeImmutable();
$tenantId = TenantId::fromString(self::TENANT_ID);
// Évaluation NON publiée
$evaluation = Evaluation::creer(
tenantId: $tenantId,
classId: ClassId::fromString(self::CLASS_ID),
subjectId: SubjectId::fromString(self::SUBJECT_ID),
teacherId: UserId::fromString(self::TEACHER_ID),
title: 'Test',
description: null,
evaluationDate: new DateTimeImmutable('2026-02-15'),
gradeScale: new GradeScale(20),
coefficient: new Coefficient(1.0),
now: $now,
);
$evaluation->pullDomainEvents();
$this->evaluationRepo->save($evaluation);
$grade = $this->seedGrade($evaluation->id, self::STUDENT_ID, 14.0, GradeStatus::GRADED);
$event = new NoteModifiee(
gradeId: $grade->id,
evaluationId: (string) $evaluation->id,
oldValue: 10.0,
newValue: 14.0,
oldStatus: 'graded',
newStatus: 'graded',
modifiedBy: self::TEACHER_ID,
occurredOn: $now,
);
($this->handler)($event);
self::assertNull($this->evalStatsRepo->findByEvaluation($evaluation->id));
}
#[Test]
public function itRecalculatesOnNoteSaisieWhenAlreadyPublished(): void
{
$tenantId = TenantId::fromString(self::TENANT_ID);
$now = new DateTimeImmutable();
$evaluation = $this->seedPublishedEvaluation();
$grade = $this->seedGrade($evaluation->id, self::STUDENT_ID, 16.0, GradeStatus::GRADED);
$event = new NoteSaisie(
gradeId: $grade->id,
evaluationId: (string) $evaluation->id,
studentId: self::STUDENT_ID,
value: 16.0,
status: 'graded',
createdBy: self::TEACHER_ID,
occurredOn: $now,
);
($this->handler)($event);
$stats = $this->evalStatsRepo->findByEvaluation($evaluation->id);
self::assertNotNull($stats);
self::assertSame(16.0, $stats->average);
$subjectAvg = $this->studentAvgRepo->findSubjectAverage(
UserId::fromString(self::STUDENT_ID),
SubjectId::fromString(self::SUBJECT_ID),
self::PERIOD_ID,
$tenantId,
);
self::assertNotNull($subjectAvg);
self::assertSame(16.0, $subjectAvg['average']);
}
#[Test]
public function itDoesNothingWhenGradeNotFound(): void
{
$now = new DateTimeImmutable();
$evaluation = $this->seedPublishedEvaluation();
$event = new NoteModifiee(
gradeId: GradeId::generate(),
evaluationId: (string) $evaluation->id,
oldValue: 10.0,
newValue: 14.0,
oldStatus: 'graded',
newStatus: 'graded',
modifiedBy: self::TEACHER_ID,
occurredOn: $now,
);
($this->handler)($event);
// Les stats sont recalculées (car l'évaluation est publiée),
// mais pas de moyenne élève (grade introuvable)
$subjectAvg = $this->studentAvgRepo->findSubjectAverage(
UserId::fromString(self::STUDENT_ID),
SubjectId::fromString(self::SUBJECT_ID),
self::PERIOD_ID,
TenantId::fromString(self::TENANT_ID),
);
self::assertNull($subjectAvg);
}
#[Test]
public function itDoesNothingWhenEvaluationNotFound(): void
{
$now = new DateTimeImmutable();
$unknownEvalId = EvaluationId::generate();
$event = new NoteModifiee(
gradeId: GradeId::generate(),
evaluationId: (string) $unknownEvalId,
oldValue: 10.0,
newValue: 14.0,
oldStatus: 'graded',
newStatus: 'graded',
modifiedBy: self::TEACHER_ID,
occurredOn: $now,
);
($this->handler)($event);
self::assertNull($this->evalStatsRepo->findByEvaluation($unknownEvalId));
}
private function seedPublishedEvaluation(): Evaluation
{
$tenantId = TenantId::fromString(self::TENANT_ID);
$now = new DateTimeImmutable();
$evaluation = Evaluation::creer(
tenantId: $tenantId,
classId: ClassId::fromString(self::CLASS_ID),
subjectId: SubjectId::fromString(self::SUBJECT_ID),
teacherId: UserId::fromString(self::TEACHER_ID),
title: 'Test Evaluation',
description: null,
evaluationDate: new DateTimeImmutable('2026-02-15'),
gradeScale: new GradeScale(20),
coefficient: new Coefficient(1.0),
now: $now,
);
$evaluation->publierNotes($now);
$evaluation->pullDomainEvents();
$this->evaluationRepo->save($evaluation);
return $evaluation;
}
private function seedGrade(
EvaluationId $evaluationId,
string $studentId,
?float $value,
GradeStatus $status,
): Grade {
$tenantId = TenantId::fromString(self::TENANT_ID);
$now = new DateTimeImmutable();
$grade = Grade::saisir(
tenantId: $tenantId,
evaluationId: $evaluationId,
studentId: UserId::fromString($studentId),
value: $value !== null ? new GradeValue($value) : null,
status: $status,
gradeScale: new GradeScale(20),
createdBy: UserId::fromString(self::TEACHER_ID),
now: $now,
);
$grade->pullDomainEvents();
$this->gradeRepo->save($grade);
return $grade;
}
}

View File

@@ -0,0 +1,301 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Scolarite\Infrastructure\EventHandler;
use App\Administration\Domain\Model\SchoolClass\ClassId;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\User\UserId;
use App\Scolarite\Application\Port\PeriodFinder;
use App\Scolarite\Application\Port\PeriodInfo;
use App\Scolarite\Application\Service\RecalculerMoyennesService;
use App\Scolarite\Domain\Event\NotesPubliees;
use App\Scolarite\Domain\Model\Evaluation\Coefficient;
use App\Scolarite\Domain\Model\Evaluation\Evaluation;
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
use App\Scolarite\Domain\Model\Evaluation\GradeScale;
use App\Scolarite\Domain\Model\Grade\Grade;
use App\Scolarite\Domain\Model\Grade\GradeStatus;
use App\Scolarite\Domain\Model\Grade\GradeValue;
use App\Scolarite\Domain\Service\AverageCalculator;
use App\Scolarite\Infrastructure\EventHandler\RecalculerMoyennesOnNotesPublieesHandler;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationRepository;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationStatisticsRepository;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryGradeRepository;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryStudentAverageRepository;
use App\Shared\Domain\Tenant\TenantId;
use App\Shared\Infrastructure\Tenant\TenantConfig;
use App\Shared\Infrastructure\Tenant\TenantContext;
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class RecalculerMoyennesOnNotesPublieesHandlerTest extends TestCase
{
private const string TENANT_ID = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';
public const string PERIOD_ID = '11111111-1111-1111-1111-111111111111';
private const string STUDENT_1 = '22222222-2222-2222-2222-222222222222';
private const string STUDENT_2 = '33333333-3333-3333-3333-333333333333';
private const string TEACHER_ID = '44444444-4444-4444-4444-444444444444';
private const string CLASS_ID = '55555555-5555-5555-5555-555555555555';
private const string SUBJECT_ID = '66666666-6666-6666-6666-666666666666';
private InMemoryEvaluationRepository $evaluationRepo;
private InMemoryGradeRepository $gradeRepo;
private InMemoryEvaluationStatisticsRepository $evalStatsRepo;
private InMemoryStudentAverageRepository $studentAvgRepo;
private RecalculerMoyennesOnNotesPublieesHandler $handler;
protected function setUp(): void
{
$this->evaluationRepo = new InMemoryEvaluationRepository();
$this->gradeRepo = new InMemoryGradeRepository();
$this->evalStatsRepo = new InMemoryEvaluationStatisticsRepository();
$this->studentAvgRepo = new InMemoryStudentAverageRepository();
$tenantContext = new TenantContext();
$tenantContext->setCurrentTenant(new TenantConfig(
tenantId: InfraTenantId::fromString(self::TENANT_ID),
subdomain: 'test',
databaseUrl: 'postgresql://test',
));
$periodFinder = new class implements PeriodFinder {
public function findForDate(DateTimeImmutable $date, TenantId $tenantId): ?PeriodInfo
{
return new PeriodInfo(
periodId: RecalculerMoyennesOnNotesPublieesHandlerTest::PERIOD_ID,
startDate: new DateTimeImmutable('2026-01-01'),
endDate: new DateTimeImmutable('2026-03-31'),
);
}
};
$service = new RecalculerMoyennesService(
evaluationRepository: $this->evaluationRepo,
gradeRepository: $this->gradeRepo,
evaluationStatisticsRepository: $this->evalStatsRepo,
studentAverageRepository: $this->studentAvgRepo,
periodFinder: $periodFinder,
calculator: new AverageCalculator(),
);
$this->handler = new RecalculerMoyennesOnNotesPublieesHandler(
tenantContext: $tenantContext,
evaluationRepository: $this->evaluationRepo,
gradeRepository: $this->gradeRepo,
periodFinder: $periodFinder,
service: $service,
);
}
#[Test]
public function itCalculatesEvaluationStatisticsOnPublication(): void
{
$evaluationId = $this->seedEvaluationWithGrades(
grades: [
[self::STUDENT_1, 14.0, GradeStatus::GRADED],
[self::STUDENT_2, 8.0, GradeStatus::GRADED],
],
);
($this->handler)(new NotesPubliees(
evaluationId: $evaluationId,
occurredOn: new DateTimeImmutable(),
));
$stats = $this->evalStatsRepo->findByEvaluation($evaluationId);
self::assertNotNull($stats);
self::assertSame(11.0, $stats->average);
self::assertSame(8.0, $stats->min);
self::assertSame(14.0, $stats->max);
self::assertSame(11.0, $stats->median);
self::assertSame(2, $stats->gradedCount);
}
#[Test]
public function itExcludesAbsentAndDispensedFromStatistics(): void
{
$evaluationId = $this->seedEvaluationWithGrades(
grades: [
[self::STUDENT_1, 16.0, GradeStatus::GRADED],
[self::STUDENT_2, null, GradeStatus::ABSENT],
],
);
($this->handler)(new NotesPubliees(
evaluationId: $evaluationId,
occurredOn: new DateTimeImmutable(),
));
$stats = $this->evalStatsRepo->findByEvaluation($evaluationId);
self::assertNotNull($stats);
self::assertSame(16.0, $stats->average);
self::assertSame(1, $stats->gradedCount);
}
#[Test]
public function itCalculatesSubjectAverageForEachStudent(): void
{
$evaluationId = $this->seedEvaluationWithGrades(
grades: [
[self::STUDENT_1, 15.0, GradeStatus::GRADED],
[self::STUDENT_2, 10.0, GradeStatus::GRADED],
],
);
($this->handler)(new NotesPubliees(
evaluationId: $evaluationId,
occurredOn: new DateTimeImmutable(),
));
$student1Avg = $this->studentAvgRepo->findSubjectAverage(
UserId::fromString(self::STUDENT_1),
SubjectId::fromString(self::SUBJECT_ID),
self::PERIOD_ID,
TenantId::fromString(self::TENANT_ID),
);
self::assertNotNull($student1Avg);
self::assertSame(15.0, $student1Avg['average']);
self::assertSame(1, $student1Avg['gradeCount']);
$student2Avg = $this->studentAvgRepo->findSubjectAverage(
UserId::fromString(self::STUDENT_2),
SubjectId::fromString(self::SUBJECT_ID),
self::PERIOD_ID,
TenantId::fromString(self::TENANT_ID),
);
self::assertNotNull($student2Avg);
self::assertSame(10.0, $student2Avg['average']);
}
#[Test]
public function itCalculatesWeightedSubjectAverageAcrossMultipleEvaluations(): void
{
// Première évaluation publiée (coef 2)
$eval1Id = $this->seedEvaluationWithGrades(
grades: [[self::STUDENT_1, 16.0, GradeStatus::GRADED]],
coefficient: 2.0,
published: true,
);
// Publier la première évaluation d'abord
($this->handler)(new NotesPubliees(
evaluationId: $eval1Id,
occurredOn: new DateTimeImmutable(),
));
// Deuxième évaluation publiée (coef 1)
$eval2Id = $this->seedEvaluationWithGrades(
grades: [[self::STUDENT_1, 10.0, GradeStatus::GRADED]],
coefficient: 1.0,
published: true,
);
($this->handler)(new NotesPubliees(
evaluationId: $eval2Id,
occurredOn: new DateTimeImmutable(),
));
$student1Avg = $this->studentAvgRepo->findSubjectAverage(
UserId::fromString(self::STUDENT_1),
SubjectId::fromString(self::SUBJECT_ID),
self::PERIOD_ID,
TenantId::fromString(self::TENANT_ID),
);
self::assertNotNull($student1Avg);
// (16×2 + 10×1) / (2+1) = 42/3 = 14.0
self::assertSame(14.0, $student1Avg['average']);
self::assertSame(2, $student1Avg['gradeCount']);
}
#[Test]
public function itCalculatesGeneralAverage(): void
{
$evaluationId = $this->seedEvaluationWithGrades(
grades: [[self::STUDENT_1, 14.0, GradeStatus::GRADED]],
);
($this->handler)(new NotesPubliees(
evaluationId: $evaluationId,
occurredOn: new DateTimeImmutable(),
));
$generalAvg = $this->studentAvgRepo->findGeneralAverageForStudent(
UserId::fromString(self::STUDENT_1),
self::PERIOD_ID,
TenantId::fromString(self::TENANT_ID),
);
self::assertSame(14.0, $generalAvg);
}
#[Test]
public function itDoesNothingWhenEvaluationNotFound(): void
{
$unknownId = EvaluationId::generate();
($this->handler)(new NotesPubliees(
evaluationId: $unknownId,
occurredOn: new DateTimeImmutable(),
));
self::assertNull($this->evalStatsRepo->findByEvaluation($unknownId));
}
/**
* @param list<array{0: string, 1: float|null, 2: GradeStatus}> $grades
*/
private function seedEvaluationWithGrades(
array $grades,
float $coefficient = 1.0,
bool $published = true,
): EvaluationId {
$tenantId = TenantId::fromString(self::TENANT_ID);
$now = new DateTimeImmutable();
$evaluation = Evaluation::creer(
tenantId: $tenantId,
classId: ClassId::fromString(self::CLASS_ID),
subjectId: SubjectId::fromString(self::SUBJECT_ID),
teacherId: UserId::fromString(self::TEACHER_ID),
title: 'Test Evaluation',
description: null,
evaluationDate: new DateTimeImmutable('2026-02-15'),
gradeScale: new GradeScale(20),
coefficient: new Coefficient($coefficient),
now: $now,
);
if ($published) {
$evaluation->publierNotes($now);
}
$evaluation->pullDomainEvents();
$this->evaluationRepo->save($evaluation);
foreach ($grades as [$studentId, $value, $status]) {
$grade = Grade::saisir(
tenantId: $tenantId,
evaluationId: $evaluation->id,
studentId: UserId::fromString($studentId),
value: $value !== null ? new GradeValue($value) : null,
status: $status,
gradeScale: new GradeScale(20),
createdBy: UserId::fromString(self::TEACHER_ID),
now: $now,
);
$grade->pullDomainEvents();
$this->gradeRepo->save($grade);
}
return $evaluation->id;
}
}