feat: Calculer automatiquement les moyennes après chaque saisie de notes
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:
@@ -0,0 +1,894 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Functional\Scolarite\Api;
|
||||
|
||||
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
|
||||
use ApiPlatform\Symfony\Bundle\Test\Client;
|
||||
use App\Administration\Domain\Model\SchoolClass\ClassId;
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Administration\Infrastructure\Security\SecurityUser;
|
||||
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\Repository\EvaluationRepository;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\DBAL\Connection;
|
||||
|
||||
use const JSON_THROW_ON_ERROR;
|
||||
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
/**
|
||||
* Functional tests for competency mode endpoints (Story 6.5).
|
||||
*
|
||||
* Covers: competencies listing, competency levels, evaluation-competency linking,
|
||||
* competency result grids, saving results, and student progress tracking.
|
||||
*/
|
||||
final class CompetencyEndpointsTest extends ApiTestCase
|
||||
{
|
||||
protected static ?bool $alwaysBootKernel = true;
|
||||
|
||||
private const string TENANT_ID = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';
|
||||
private const string TEACHER_ID = 'cc444444-4444-4444-4444-444444444444';
|
||||
private const string OTHER_TEACHER_ID = 'cc444444-4444-4444-4444-444444444445';
|
||||
private const string STUDENT_ID = 'cc222222-2222-2222-2222-222222222222';
|
||||
private const string STUDENT2_ID = 'cc222222-2222-2222-2222-222222222223';
|
||||
private const string CLASS_ID = 'cc555555-5555-5555-5555-555555555555';
|
||||
private const string SUBJECT_ID = 'cc666666-6666-6666-6666-666666666666';
|
||||
private const string FRAMEWORK_ID = 'cc777777-7777-7777-7777-777777777777';
|
||||
private const string COMPETENCY1_ID = 'cc888888-8888-8888-8888-888888888881';
|
||||
private const string COMPETENCY2_ID = 'cc888888-8888-8888-8888-888888888882';
|
||||
private const string BASE_URL = 'http://ecole-alpha.classeo.local/api';
|
||||
|
||||
private ?EvaluationId $evaluationId = null;
|
||||
private ?string $competencyEvaluation1Id = null;
|
||||
private ?string $competencyEvaluation2Id = null;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->seedFixtures();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
/** @var Connection $connection */
|
||||
$connection = static::getContainer()->get(Connection::class);
|
||||
|
||||
$connection->executeStatement(
|
||||
'DELETE FROM student_competency_results WHERE competency_evaluation_id IN (SELECT id FROM competency_evaluations WHERE evaluation_id IN (SELECT id FROM evaluations WHERE tenant_id = :tid))',
|
||||
['tid' => self::TENANT_ID],
|
||||
);
|
||||
$connection->executeStatement(
|
||||
'DELETE FROM competency_evaluations WHERE evaluation_id IN (SELECT id FROM evaluations WHERE tenant_id = :tid)',
|
||||
['tid' => self::TENANT_ID],
|
||||
);
|
||||
$connection->executeStatement(
|
||||
'DELETE FROM competencies WHERE framework_id IN (SELECT id FROM competency_frameworks WHERE tenant_id = :tid)',
|
||||
['tid' => self::TENANT_ID],
|
||||
);
|
||||
$connection->executeStatement(
|
||||
'DELETE FROM competency_frameworks WHERE tenant_id = :tid',
|
||||
['tid' => self::TENANT_ID],
|
||||
);
|
||||
$connection->executeStatement(
|
||||
'DELETE FROM evaluations WHERE tenant_id = :tid',
|
||||
['tid' => self::TENANT_ID],
|
||||
);
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GET /competencies — Auth & Happy path
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function getCompetenciesReturns401WithoutAuthentication(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('GET', self::BASE_URL . '/competencies', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(401);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getCompetenciesReturns200ForAuthenticatedUser(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('GET', self::BASE_URL . '/competencies', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
|
||||
/** @var string $content */
|
||||
$content = $client->getResponse()->getContent();
|
||||
/** @var array<array{id: string, code: string, name: string}> $data */
|
||||
$data = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
self::assertCount(2, $data);
|
||||
self::assertSame(self::COMPETENCY1_ID, $data[0]['id']);
|
||||
self::assertSame('C1', $data[0]['code']);
|
||||
self::assertSame('Lire et comprendre', $data[0]['name']);
|
||||
self::assertSame(self::COMPETENCY2_ID, $data[1]['id']);
|
||||
self::assertSame('C2', $data[1]['code']);
|
||||
self::assertSame('Ecrire', $data[1]['name']);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GET /competency-levels — Auth & Happy path
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function getCompetencyLevelsReturns401WithoutAuthentication(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('GET', self::BASE_URL . '/competency-levels', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(401);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getCompetencyLevelsReturns200WithStandard4Levels(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('GET', self::BASE_URL . '/competency-levels', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
|
||||
/** @var string $content */
|
||||
$content = $client->getResponse()->getContent();
|
||||
/** @var array<array{code: string, name: string}> $data */
|
||||
$data = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
self::assertCount(4, $data);
|
||||
self::assertSame('not_acquired', $data[0]['code']);
|
||||
self::assertSame('Non acquis', $data[0]['name']);
|
||||
self::assertSame('in_progress', $data[1]['code']);
|
||||
self::assertSame('acquired', $data[2]['code']);
|
||||
self::assertSame('exceeded', $data[3]['code']);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GET /evaluations/{id}/competencies — Auth & edge cases
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function getEvaluationCompetenciesReturns401WithoutAuthentication(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('GET', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competencies', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(401);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getEvaluationCompetenciesReturns403ForNonOwner(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::OTHER_TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('GET', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competencies', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(403);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getEvaluationCompetenciesReturns404ForUnknownEvaluation(): void
|
||||
{
|
||||
$unknownId = (string) EvaluationId::generate();
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('GET', self::BASE_URL . '/evaluations/' . $unknownId . '/competencies', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(404);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getEvaluationCompetenciesReturns200ForOwner(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('GET', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competencies', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
|
||||
/** @var string $content */
|
||||
$content = $client->getResponse()->getContent();
|
||||
/** @var array<array{id: string, competencyId: string, competencyCode: string}> $data */
|
||||
$data = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
self::assertCount(2, $data);
|
||||
self::assertSame((string) $this->evaluationId, $data[0]['evaluationId']);
|
||||
self::assertSame(self::COMPETENCY1_ID, $data[0]['competencyId']);
|
||||
self::assertSame('C1', $data[0]['competencyCode']);
|
||||
self::assertSame(self::COMPETENCY2_ID, $data[1]['competencyId']);
|
||||
self::assertSame('C2', $data[1]['competencyCode']);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GET /evaluations/{id}/competency-results — Auth & Happy path
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function getCompetencyResultsReturns401WithoutAuthentication(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('GET', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competency-results', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(401);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getCompetencyResultsReturns403ForNonOwner(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::OTHER_TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('GET', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competency-results', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(403);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getCompetencyResultsReturns200ForOwnerWithGrid(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('GET', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competency-results', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
|
||||
/** @var string $content */
|
||||
$content = $client->getResponse()->getContent();
|
||||
/** @var array<array{studentId: string, competencyEvaluationId: string, levelCode: string|null}> $data */
|
||||
$data = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
// 2 students x 2 competencies = 4 rows in the result matrix
|
||||
self::assertCount(4, $data);
|
||||
|
||||
// Find the seeded result (student1 + competency1 = acquired)
|
||||
$seededResult = null;
|
||||
foreach ($data as $row) {
|
||||
if ($row['studentId'] === self::STUDENT_ID && $row['competencyEvaluationId'] === $this->competencyEvaluation1Id) {
|
||||
$seededResult = $row;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
self::assertNotNull($seededResult);
|
||||
self::assertSame('acquired', $seededResult['levelCode']);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// PUT /evaluations/{id}/competency-results — Auth & Happy path
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function putCompetencyResultsReturns401WithoutAuthentication(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('PUT', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competency-results', [
|
||||
'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
|
||||
'json' => [
|
||||
'results' => [],
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(401);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function putCompetencyResultsReturns403ForNonOwner(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::OTHER_TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('PUT', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competency-results', [
|
||||
'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
|
||||
'json' => [
|
||||
'results' => [],
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(403);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function putCompetencyResultsReturns200AndSavesResults(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('PUT', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competency-results', [
|
||||
'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
|
||||
'json' => [
|
||||
'results' => [
|
||||
[
|
||||
'studentId' => self::STUDENT2_ID,
|
||||
'competencyEvaluationId' => $this->competencyEvaluation1Id,
|
||||
'levelCode' => 'in_progress',
|
||||
],
|
||||
[
|
||||
'studentId' => self::STUDENT2_ID,
|
||||
'competencyEvaluationId' => $this->competencyEvaluation2Id,
|
||||
'levelCode' => 'not_acquired',
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
|
||||
/** @var string $content */
|
||||
$content = $client->getResponse()->getContent();
|
||||
/** @var array<array{studentId: string, levelCode: string}> $data */
|
||||
$data = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
self::assertCount(2, $data);
|
||||
self::assertSame(self::STUDENT2_ID, $data[0]['studentId']);
|
||||
self::assertSame('in_progress', $data[0]['levelCode']);
|
||||
self::assertSame(self::STUDENT2_ID, $data[1]['studentId']);
|
||||
self::assertSame('not_acquired', $data[1]['levelCode']);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// POST /evaluations/{id}/competencies — Auth & Happy path
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function postLinkCompetenciesReturns401WithoutAuthentication(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('POST', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competencies', [
|
||||
'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
|
||||
'json' => [
|
||||
'competencyIds' => [self::COMPETENCY1_ID],
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(401);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function postLinkCompetenciesReturns403ForNonOwner(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::OTHER_TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('POST', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competencies', [
|
||||
'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
|
||||
'json' => [
|
||||
'competencyIds' => [self::COMPETENCY1_ID],
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(403);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function postLinkCompetenciesReturns200AndLinksCompetencies(): void
|
||||
{
|
||||
// First, remove existing links to test fresh linking
|
||||
/** @var Connection $connection */
|
||||
$connection = static::getContainer()->get(Connection::class);
|
||||
$connection->executeStatement(
|
||||
'DELETE FROM student_competency_results WHERE competency_evaluation_id IN (SELECT id FROM competency_evaluations WHERE evaluation_id = :eid)',
|
||||
['eid' => (string) $this->evaluationId],
|
||||
);
|
||||
$connection->executeStatement(
|
||||
'DELETE FROM competency_evaluations WHERE evaluation_id = :eid',
|
||||
['eid' => (string) $this->evaluationId],
|
||||
);
|
||||
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('POST', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competencies', [
|
||||
'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
|
||||
'json' => [
|
||||
'competencyIds' => [self::COMPETENCY1_ID, self::COMPETENCY2_ID],
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
|
||||
/** @var string $content */
|
||||
$content = $client->getResponse()->getContent();
|
||||
/** @var array<array{evaluationId: string, competencyId: string}> $data */
|
||||
$data = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
self::assertCount(2, $data);
|
||||
self::assertSame((string) $this->evaluationId, $data[0]['evaluationId']);
|
||||
self::assertSame(self::COMPETENCY1_ID, $data[0]['competencyId']);
|
||||
self::assertSame(self::COMPETENCY2_ID, $data[1]['competencyId']);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// PUT /evaluations/{id}/competency-results — Validation (error paths)
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function putCompetencyResultsReturns400ForInvalidLevelCode(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('PUT', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competency-results', [
|
||||
'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
|
||||
'json' => [
|
||||
'results' => [
|
||||
[
|
||||
'studentId' => self::STUDENT_ID,
|
||||
'competencyEvaluationId' => $this->competencyEvaluation1Id,
|
||||
'levelCode' => 'INVALID_CODE',
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(400);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function putCompetencyResultsReturns400ForStudentNotInClass(): void
|
||||
{
|
||||
$outsiderId = 'cc999999-9999-9999-9999-999999999999';
|
||||
|
||||
/** @var Connection $connection */
|
||||
$connection = static::getContainer()->get(Connection::class);
|
||||
$connection->executeStatement(
|
||||
"INSERT INTO users (id, tenant_id, email, hashed_password, first_name, last_name, roles, statut, created_at, updated_at)
|
||||
VALUES (:id, :tid, 'outsider-comp@test.local', '', 'Outsider', 'User', '[\"ROLE_ELEVE\"]', 'active', NOW(), NOW())
|
||||
ON CONFLICT (id) DO NOTHING",
|
||||
['id' => $outsiderId, 'tid' => self::TENANT_ID],
|
||||
);
|
||||
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('PUT', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competency-results', [
|
||||
'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
|
||||
'json' => [
|
||||
'results' => [
|
||||
[
|
||||
'studentId' => $outsiderId,
|
||||
'competencyEvaluationId' => $this->competencyEvaluation1Id,
|
||||
'levelCode' => 'acquired',
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(400);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function putCompetencyResultsReturns400ForInvalidCompetencyEvaluationId(): void
|
||||
{
|
||||
$fakeCeId = (string) Uuid::uuid4();
|
||||
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('PUT', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competency-results', [
|
||||
'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
|
||||
'json' => [
|
||||
'results' => [
|
||||
[
|
||||
'studentId' => self::STUDENT_ID,
|
||||
'competencyEvaluationId' => $fakeCeId,
|
||||
'levelCode' => 'acquired',
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(400);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function putCompetencyResultsDeletesResultWhenLevelCodeIsNull(): void
|
||||
{
|
||||
// Verify the seeded result exists via the results grid
|
||||
$getClient = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$getClient->request('GET', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competency-results', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
|
||||
/** @var string $content */
|
||||
$content = $getClient->getResponse()->getContent();
|
||||
/** @var array<array{studentId: string, competencyEvaluationId: string, levelCode: string|null}> $before */
|
||||
$before = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
|
||||
$seededBefore = null;
|
||||
foreach ($before as $row) {
|
||||
if ($row['studentId'] === self::STUDENT_ID && $row['competencyEvaluationId'] === $this->competencyEvaluation1Id) {
|
||||
$seededBefore = $row;
|
||||
break;
|
||||
}
|
||||
}
|
||||
self::assertNotNull($seededBefore);
|
||||
self::assertSame('acquired', $seededBefore['levelCode']);
|
||||
|
||||
// Send null levelCode to delete the result
|
||||
$putClient = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$putClient->request('PUT', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competency-results', [
|
||||
'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
|
||||
'json' => [
|
||||
'results' => [
|
||||
[
|
||||
'studentId' => self::STUDENT_ID,
|
||||
'competencyEvaluationId' => $this->competencyEvaluation1Id,
|
||||
'levelCode' => null,
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
|
||||
// Verify the result is now null in the grid
|
||||
$verifyClient = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$verifyClient->request('GET', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competency-results', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
/** @var string $afterContent */
|
||||
$afterContent = $verifyClient->getResponse()->getContent();
|
||||
/** @var array<array{studentId: string, competencyEvaluationId: string, levelCode: string|null}> $after */
|
||||
$after = json_decode($afterContent, true, 512, JSON_THROW_ON_ERROR);
|
||||
$seededAfter = null;
|
||||
foreach ($after as $row) {
|
||||
if ($row['studentId'] === self::STUDENT_ID && $row['competencyEvaluationId'] === $this->competencyEvaluation1Id) {
|
||||
$seededAfter = $row;
|
||||
break;
|
||||
}
|
||||
}
|
||||
self::assertNotNull($seededAfter);
|
||||
self::assertNull($seededAfter['levelCode'] ?? null);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// POST /evaluations/{id}/competencies — Validation (error paths)
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function postLinkCompetenciesReturns400ForUnknownCompetencyId(): void
|
||||
{
|
||||
$fakeCompetencyId = (string) Uuid::uuid4();
|
||||
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('POST', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competencies', [
|
||||
'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
|
||||
'json' => [
|
||||
'competencyIds' => [$fakeCompetencyId],
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(400);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function postLinkCompetenciesIsIdempotentForAlreadyLinkedCompetency(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
|
||||
// Link the same competencies that are already linked
|
||||
$client->request('POST', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competencies', [
|
||||
'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
|
||||
'json' => [
|
||||
'competencyIds' => [self::COMPETENCY1_ID],
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
|
||||
/** @var string $content */
|
||||
$content = $client->getResponse()->getContent();
|
||||
/** @var array<array{competencyId: string}> $data */
|
||||
$data = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
self::assertCount(1, $data);
|
||||
self::assertSame(self::COMPETENCY1_ID, $data[0]['competencyId']);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// AC6: Mode mixte — Competency results don't create grades
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function competencyResultsDoNotCreateGradeEntries(): void
|
||||
{
|
||||
/** @var Connection $connection */
|
||||
$connection = static::getContainer()->get(Connection::class);
|
||||
|
||||
// Count grades before saving competency results
|
||||
/** @var string $countBefore */
|
||||
$countBefore = $connection->fetchOne(
|
||||
'SELECT COUNT(*) FROM grades WHERE evaluation_id = :eid',
|
||||
['eid' => (string) $this->evaluationId],
|
||||
);
|
||||
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('PUT', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/competency-results', [
|
||||
'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
|
||||
'json' => [
|
||||
'results' => [
|
||||
[
|
||||
'studentId' => self::STUDENT_ID,
|
||||
'competencyEvaluationId' => $this->competencyEvaluation1Id,
|
||||
'levelCode' => 'exceeded',
|
||||
],
|
||||
[
|
||||
'studentId' => self::STUDENT2_ID,
|
||||
'competencyEvaluationId' => $this->competencyEvaluation2Id,
|
||||
'levelCode' => 'in_progress',
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
|
||||
// Count grades after — should be unchanged (competency results don't create grades)
|
||||
/** @var string $countAfter */
|
||||
$countAfter = $connection->fetchOne(
|
||||
'SELECT COUNT(*) FROM grades WHERE evaluation_id = :eid',
|
||||
['eid' => (string) $this->evaluationId],
|
||||
);
|
||||
|
||||
self::assertSame($countBefore, $countAfter, 'Saving competency results must not create grade entries');
|
||||
|
||||
// Also verify no student_averages were created for this evaluation
|
||||
/** @var string $avgCount */
|
||||
$avgCount = $connection->fetchOne(
|
||||
'SELECT COUNT(*) FROM student_averages WHERE tenant_id = :tid AND subject_id = :sid',
|
||||
['tid' => self::TENANT_ID, 'sid' => self::SUBJECT_ID],
|
||||
);
|
||||
|
||||
self::assertSame(0, (int) $avgCount, 'Competency results must not trigger average calculation');
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GET /students/{id}/competency-progress — Auth & Happy path
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function getStudentCompetencyProgressReturns401WithoutAuthentication(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('GET', self::BASE_URL . '/students/' . self::STUDENT_ID . '/competency-progress', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(401);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getStudentCompetencyProgressReturns403ForOtherStudent(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::STUDENT2_ID, ['ROLE_ELEVE']);
|
||||
$client->request('GET', self::BASE_URL . '/students/' . self::STUDENT_ID . '/competency-progress', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(403);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getStudentCompetencyProgressReturns200ForOwnData(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::STUDENT_ID, ['ROLE_ELEVE']);
|
||||
$client->request('GET', self::BASE_URL . '/students/' . self::STUDENT_ID . '/competency-progress', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
|
||||
/** @var string $content */
|
||||
$content = $client->getResponse()->getContent();
|
||||
/** @var array<array{competencyId: string, currentLevelCode: string}> $data */
|
||||
$data = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
self::assertCount(1, $data);
|
||||
self::assertSame(self::COMPETENCY1_ID, $data[0]['competencyId']);
|
||||
self::assertSame('acquired', $data[0]['currentLevelCode']);
|
||||
self::assertSame('Acquis', $data[0]['currentLevelName']);
|
||||
self::assertNotEmpty($data[0]['history']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getStudentCompetencyProgressReturns200ForTeacher(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('GET', self::BASE_URL . '/students/' . self::STUDENT_ID . '/competency-progress', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
|
||||
/** @var string $content */
|
||||
$content = $client->getResponse()->getContent();
|
||||
/** @var array<array{competencyId: string}> $data */
|
||||
$data = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
self::assertCount(1, $data);
|
||||
self::assertSame(self::COMPETENCY1_ID, $data[0]['competencyId']);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Helpers
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @param list<string> $roles
|
||||
*/
|
||||
private function createAuthenticatedClient(string $userId, array $roles): Client
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
$user = new SecurityUser(
|
||||
userId: UserId::fromString($userId),
|
||||
email: 'test-comp@classeo.local',
|
||||
hashedPassword: '',
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
roles: $roles,
|
||||
);
|
||||
|
||||
$client->loginUser($user, 'api');
|
||||
|
||||
return $client;
|
||||
}
|
||||
|
||||
private function seedFixtures(): void
|
||||
{
|
||||
$container = static::getContainer();
|
||||
/** @var Connection $connection */
|
||||
$connection = $container->get(Connection::class);
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$now = new DateTimeImmutable();
|
||||
|
||||
$schoolId = '550e8400-e29b-41d4-a716-ff6655440001';
|
||||
$academicYearId = '550e8400-e29b-41d4-a716-ff6655440002';
|
||||
|
||||
// --- Users ---
|
||||
$connection->executeStatement(
|
||||
"INSERT INTO users (id, tenant_id, email, hashed_password, first_name, last_name, roles, statut, created_at, updated_at)
|
||||
VALUES (:id, :tid, 'teacher-comp@test.local', '', 'Test', 'Teacher', '[\"ROLE_PROF\"]', 'active', NOW(), NOW())
|
||||
ON CONFLICT (id) DO NOTHING",
|
||||
['id' => self::TEACHER_ID, 'tid' => self::TENANT_ID],
|
||||
);
|
||||
$connection->executeStatement(
|
||||
"INSERT INTO users (id, tenant_id, email, hashed_password, first_name, last_name, roles, statut, created_at, updated_at)
|
||||
VALUES (:id, :tid, 'other-teacher-comp@test.local', '', 'Other', 'Teacher', '[\"ROLE_PROF\"]', 'active', NOW(), NOW())
|
||||
ON CONFLICT (id) DO NOTHING",
|
||||
['id' => self::OTHER_TEACHER_ID, 'tid' => self::TENANT_ID],
|
||||
);
|
||||
$connection->executeStatement(
|
||||
"INSERT INTO users (id, tenant_id, email, hashed_password, first_name, last_name, roles, statut, created_at, updated_at)
|
||||
VALUES (:id, :tid, 'student1-comp@test.local', '', 'Alice', 'Dupont', '[\"ROLE_ELEVE\"]', 'active', NOW(), NOW())
|
||||
ON CONFLICT (id) DO NOTHING",
|
||||
['id' => self::STUDENT_ID, 'tid' => self::TENANT_ID],
|
||||
);
|
||||
$connection->executeStatement(
|
||||
"INSERT INTO users (id, tenant_id, email, hashed_password, first_name, last_name, roles, statut, created_at, updated_at)
|
||||
VALUES (:id, :tid, 'student2-comp@test.local', '', 'Bob', 'Martin', '[\"ROLE_ELEVE\"]', 'active', NOW(), NOW())
|
||||
ON CONFLICT (id) DO NOTHING",
|
||||
['id' => self::STUDENT2_ID, 'tid' => self::TENANT_ID],
|
||||
);
|
||||
|
||||
// --- School class ---
|
||||
$connection->executeStatement(
|
||||
"INSERT INTO school_classes (id, tenant_id, school_id, academic_year_id, name, status, created_at, updated_at)
|
||||
VALUES (:id, :tid, :sid, :ayid, 'Test-Comp-Class', 'active', NOW(), NOW())
|
||||
ON CONFLICT (id) DO NOTHING",
|
||||
['id' => self::CLASS_ID, 'tid' => self::TENANT_ID, 'sid' => $schoolId, 'ayid' => $academicYearId],
|
||||
);
|
||||
|
||||
// --- Subject ---
|
||||
$connection->executeStatement(
|
||||
"INSERT INTO subjects (id, tenant_id, school_id, name, code, status, created_at, updated_at)
|
||||
VALUES (:id, :tid, :sid, 'Test-Comp-Subject', 'TCOMP', 'active', NOW(), NOW())
|
||||
ON CONFLICT (id) DO NOTHING",
|
||||
['id' => self::SUBJECT_ID, 'tid' => self::TENANT_ID, 'sid' => $schoolId],
|
||||
);
|
||||
|
||||
// --- Class assignments (students assigned to class) ---
|
||||
$classAssignment1Id = (string) Uuid::uuid4();
|
||||
$connection->executeStatement(
|
||||
'INSERT INTO class_assignments (id, tenant_id, user_id, school_class_id, academic_year_id, assigned_at, created_at, updated_at)
|
||||
VALUES (:id, :tid, :uid, :cid, :ayid, NOW(), NOW(), NOW())
|
||||
ON CONFLICT (user_id, academic_year_id) DO NOTHING',
|
||||
['id' => $classAssignment1Id, 'tid' => self::TENANT_ID, 'uid' => self::STUDENT_ID, 'cid' => self::CLASS_ID, 'ayid' => $academicYearId],
|
||||
);
|
||||
$classAssignment2Id = (string) Uuid::uuid4();
|
||||
$connection->executeStatement(
|
||||
'INSERT INTO class_assignments (id, tenant_id, user_id, school_class_id, academic_year_id, assigned_at, created_at, updated_at)
|
||||
VALUES (:id, :tid, :uid, :cid, :ayid, NOW(), NOW(), NOW())
|
||||
ON CONFLICT (user_id, academic_year_id) DO NOTHING',
|
||||
['id' => $classAssignment2Id, 'tid' => self::TENANT_ID, 'uid' => self::STUDENT2_ID, 'cid' => self::CLASS_ID, 'ayid' => $academicYearId],
|
||||
);
|
||||
|
||||
// --- Evaluation (owned by teacher) ---
|
||||
$evaluation = Evaluation::creer(
|
||||
tenantId: $tenantId,
|
||||
classId: ClassId::fromString(self::CLASS_ID),
|
||||
subjectId: SubjectId::fromString(self::SUBJECT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Evaluation Competences',
|
||||
description: null,
|
||||
evaluationDate: new DateTimeImmutable('2026-03-15'),
|
||||
gradeScale: new GradeScale(20),
|
||||
coefficient: new Coefficient(1.0),
|
||||
now: $now,
|
||||
);
|
||||
$evaluation->pullDomainEvents();
|
||||
|
||||
/** @var EvaluationRepository $evalRepo */
|
||||
$evalRepo = $container->get(EvaluationRepository::class);
|
||||
$evalRepo->save($evaluation);
|
||||
|
||||
$this->evaluationId = $evaluation->id;
|
||||
|
||||
// --- Competency framework (default for tenant) ---
|
||||
$connection->executeStatement(
|
||||
'INSERT INTO competency_frameworks (id, tenant_id, name, is_default, created_at)
|
||||
VALUES (:id, :tid, :name, true, NOW())
|
||||
ON CONFLICT (id) DO NOTHING',
|
||||
['id' => self::FRAMEWORK_ID, 'tid' => self::TENANT_ID, 'name' => 'Socle commun'],
|
||||
);
|
||||
|
||||
// --- Competencies ---
|
||||
$connection->executeStatement(
|
||||
'INSERT INTO competencies (id, framework_id, code, name, description, parent_id, sort_order)
|
||||
VALUES (:id, :fid, :code, :name, :desc, NULL, :sort)
|
||||
ON CONFLICT (id) DO NOTHING',
|
||||
['id' => self::COMPETENCY1_ID, 'fid' => self::FRAMEWORK_ID, 'code' => 'C1', 'name' => 'Lire et comprendre', 'desc' => 'Capacite de lecture', 'sort' => 1],
|
||||
);
|
||||
$connection->executeStatement(
|
||||
'INSERT INTO competencies (id, framework_id, code, name, description, parent_id, sort_order)
|
||||
VALUES (:id, :fid, :code, :name, :desc, NULL, :sort)
|
||||
ON CONFLICT (id) DO NOTHING',
|
||||
['id' => self::COMPETENCY2_ID, 'fid' => self::FRAMEWORK_ID, 'code' => 'C2', 'name' => 'Ecrire', 'desc' => 'Capacite d\'ecriture', 'sort' => 2],
|
||||
);
|
||||
|
||||
// --- Competency evaluations (link competencies to evaluation) ---
|
||||
$this->competencyEvaluation1Id = (string) Uuid::uuid4();
|
||||
$connection->executeStatement(
|
||||
'INSERT INTO competency_evaluations (id, evaluation_id, competency_id)
|
||||
VALUES (:id, :eid, :cid)
|
||||
ON CONFLICT (evaluation_id, competency_id) DO NOTHING',
|
||||
['id' => $this->competencyEvaluation1Id, 'eid' => (string) $this->evaluationId, 'cid' => self::COMPETENCY1_ID],
|
||||
);
|
||||
$this->competencyEvaluation2Id = (string) Uuid::uuid4();
|
||||
$connection->executeStatement(
|
||||
'INSERT INTO competency_evaluations (id, evaluation_id, competency_id)
|
||||
VALUES (:id, :eid, :cid)
|
||||
ON CONFLICT (evaluation_id, competency_id) DO NOTHING',
|
||||
['id' => $this->competencyEvaluation2Id, 'eid' => (string) $this->evaluationId, 'cid' => self::COMPETENCY2_ID],
|
||||
);
|
||||
|
||||
// --- Student competency result (student1 + competency1 = acquired) ---
|
||||
$resultId = (string) Uuid::uuid4();
|
||||
$connection->executeStatement(
|
||||
'INSERT INTO student_competency_results (id, tenant_id, competency_evaluation_id, student_id, level_code, created_at, updated_at)
|
||||
VALUES (:id, :tid, :ceid, :sid, :level, NOW(), NOW())
|
||||
ON CONFLICT (competency_evaluation_id, student_id) DO NOTHING',
|
||||
[
|
||||
'id' => $resultId,
|
||||
'tid' => self::TENANT_ID,
|
||||
'ceid' => $this->competencyEvaluation1Id,
|
||||
'sid' => self::STUDENT_ID,
|
||||
'level' => 'acquired',
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
394
backend/tests/Functional/Scolarite/Api/MoyennesEndpointsTest.php
Normal file
394
backend/tests/Functional/Scolarite/Api/MoyennesEndpointsTest.php
Normal file
@@ -0,0 +1,394 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Functional\Scolarite\Api;
|
||||
|
||||
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
|
||||
use App\Administration\Domain\Model\SchoolClass\ClassId;
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Administration\Infrastructure\Security\SecurityUser;
|
||||
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\Repository\EvaluationRepository;
|
||||
use App\Scolarite\Domain\Repository\EvaluationStatisticsRepository;
|
||||
use App\Scolarite\Domain\Repository\GradeRepository;
|
||||
use App\Scolarite\Domain\Repository\StudentAverageRepository;
|
||||
use App\Scolarite\Domain\Service\AverageCalculator;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
|
||||
final class MoyennesEndpointsTest extends ApiTestCase
|
||||
{
|
||||
protected static ?bool $alwaysBootKernel = true;
|
||||
|
||||
private const string TENANT_ID = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';
|
||||
private const string TEACHER_ID = '44444444-4444-4444-4444-444444444444';
|
||||
private const string OTHER_TEACHER_ID = '44444444-4444-4444-4444-444444444445';
|
||||
private const string STUDENT_ID = '22222222-2222-2222-2222-222222222222';
|
||||
private const string CLASS_ID = '55555555-5555-5555-5555-555555555555';
|
||||
private const string SUBJECT_ID = '66666666-6666-6666-6666-666666666666';
|
||||
private const string PERIOD_ID = '11111111-1111-1111-1111-111111111111';
|
||||
private const string BASE_URL = 'http://ecole-alpha.classeo.local/api';
|
||||
|
||||
private ?EvaluationId $evaluationId = null;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->seedFixtures();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
/** @var Connection $connection */
|
||||
$connection = static::getContainer()->get(Connection::class);
|
||||
$connection->executeStatement('DELETE FROM evaluation_statistics WHERE evaluation_id IN (SELECT id FROM evaluations WHERE tenant_id = :tid)', ['tid' => self::TENANT_ID]);
|
||||
$connection->executeStatement('DELETE FROM student_general_averages WHERE tenant_id = :tid', ['tid' => self::TENANT_ID]);
|
||||
$connection->executeStatement('DELETE FROM student_averages WHERE tenant_id = :tid', ['tid' => self::TENANT_ID]);
|
||||
$connection->executeStatement('DELETE FROM grades WHERE tenant_id = :tid', ['tid' => self::TENANT_ID]);
|
||||
$connection->executeStatement('DELETE FROM evaluations WHERE tenant_id = :tid', ['tid' => self::TENANT_ID]);
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GET /evaluations/{id}/statistics — Auth
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function getEvaluationStatisticsReturns401WithoutAuthentication(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('GET', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/statistics', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(401);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getEvaluationStatisticsReturns403ForNonOwner(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::OTHER_TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('GET', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/statistics', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(403);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GET /evaluations/{id}/statistics — Happy path
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function getEvaluationStatisticsReturnsStatsForOwner(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('GET', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/statistics', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
self::assertJsonContains([
|
||||
'evaluationId' => (string) $this->evaluationId,
|
||||
'gradedCount' => 2,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getEvaluationStatisticsReturns404ForUnknownEvaluation(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$unknownId = (string) EvaluationId::generate();
|
||||
$client->request('GET', self::BASE_URL . '/evaluations/' . $unknownId . '/statistics', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(404);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GET /students/{id}/averages — Auth
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function getStudentAveragesReturns401WithoutAuthentication(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('GET', self::BASE_URL . '/students/' . self::STUDENT_ID . '/averages?periodId=' . self::PERIOD_ID, [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(401);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getStudentAveragesReturns403ForUnrelatedParent(): void
|
||||
{
|
||||
$parentId = '88888888-8888-8888-8888-888888888888';
|
||||
$client = $this->createAuthenticatedClient($parentId, ['ROLE_PARENT']);
|
||||
$client->request('GET', self::BASE_URL . '/students/' . self::STUDENT_ID . '/averages?periodId=' . self::PERIOD_ID, [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(403);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GET /students/{id}/averages — Happy path
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function getStudentAveragesReturnsDataForStaff(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('GET', self::BASE_URL . '/students/' . self::STUDENT_ID . '/averages?periodId=' . self::PERIOD_ID, [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
self::assertJsonContains([
|
||||
'studentId' => self::STUDENT_ID,
|
||||
'periodId' => self::PERIOD_ID,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getStudentAveragesReturnsOwnDataForStudent(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::STUDENT_ID, ['ROLE_ELEVE']);
|
||||
$client->request('GET', self::BASE_URL . '/students/' . self::STUDENT_ID . '/averages?periodId=' . self::PERIOD_ID, [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
self::assertJsonContains([
|
||||
'studentId' => self::STUDENT_ID,
|
||||
]);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GET /classes/{id}/statistics — Auth
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function getClassStatisticsReturns401WithoutAuthentication(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('GET', self::BASE_URL . '/classes/' . self::CLASS_ID . '/statistics', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(401);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getClassStatisticsReturns403ForStudent(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::STUDENT_ID, ['ROLE_ELEVE']);
|
||||
$client->request('GET', self::BASE_URL . '/classes/' . self::CLASS_ID . '/statistics', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(403);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getClassStatisticsReturns403ForParent(): void
|
||||
{
|
||||
$parentId = '88888888-8888-8888-8888-888888888888';
|
||||
$client = $this->createAuthenticatedClient($parentId, ['ROLE_PARENT']);
|
||||
$client->request('GET', self::BASE_URL . '/classes/' . self::CLASS_ID . '/statistics', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(403);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GET /classes/{id}/statistics — Happy path
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function getClassStatisticsReturnsDataForTeacher(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('GET', self::BASE_URL . '/classes/' . self::CLASS_ID . '/statistics', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
self::assertJsonContains([
|
||||
'classId' => self::CLASS_ID,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getClassStatisticsReturnsDataForAdmin(): void
|
||||
{
|
||||
$adminId = '99999999-9999-9999-9999-999999999999';
|
||||
$client = $this->createAuthenticatedClient($adminId, ['ROLE_ADMIN']);
|
||||
$client->request('GET', self::BASE_URL . '/classes/' . self::CLASS_ID . '/statistics', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Helpers
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @param list<string> $roles
|
||||
*/
|
||||
private function createAuthenticatedClient(string $userId, array $roles): \ApiPlatform\Symfony\Bundle\Test\Client
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
$user = new SecurityUser(
|
||||
userId: UserId::fromString($userId),
|
||||
email: 'test@classeo.local',
|
||||
hashedPassword: '',
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
roles: $roles,
|
||||
);
|
||||
|
||||
$client->loginUser($user, 'api');
|
||||
|
||||
return $client;
|
||||
}
|
||||
|
||||
private function seedFixtures(): void
|
||||
{
|
||||
$container = static::getContainer();
|
||||
/** @var Connection $connection */
|
||||
$connection = $container->get(Connection::class);
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$now = new DateTimeImmutable();
|
||||
|
||||
$schoolId = '550e8400-e29b-41d4-a716-ff6655440001';
|
||||
$academicYearId = '550e8400-e29b-41d4-a716-ff6655440002';
|
||||
|
||||
// Seed parent tables
|
||||
$connection->executeStatement(
|
||||
"INSERT INTO users (id, tenant_id, email, hashed_password, first_name, last_name, roles, statut, created_at, updated_at)
|
||||
VALUES (:id, :tid, 'teacher-moy@test.local', '', 'Test', 'Teacher', '[\"ROLE_PROF\"]', 'active', NOW(), NOW())
|
||||
ON CONFLICT (id) DO NOTHING",
|
||||
['id' => self::TEACHER_ID, 'tid' => self::TENANT_ID],
|
||||
);
|
||||
$connection->executeStatement(
|
||||
"INSERT INTO users (id, tenant_id, email, hashed_password, first_name, last_name, roles, statut, created_at, updated_at)
|
||||
VALUES (:id, :tid, 'student-moy@test.local', '', 'Test', 'Student', '[\"ROLE_ELEVE\"]', 'active', NOW(), NOW())
|
||||
ON CONFLICT (id) DO NOTHING",
|
||||
['id' => self::STUDENT_ID, 'tid' => self::TENANT_ID],
|
||||
);
|
||||
$connection->executeStatement(
|
||||
"INSERT INTO users (id, tenant_id, email, hashed_password, first_name, last_name, roles, statut, created_at, updated_at)
|
||||
VALUES (:id, :tid, 'student2-moy@test.local', '', 'Test', 'Student2', '[\"ROLE_ELEVE\"]', 'active', NOW(), NOW())
|
||||
ON CONFLICT (id) DO NOTHING",
|
||||
['id' => '33333333-3333-3333-3333-333333333333', 'tid' => self::TENANT_ID],
|
||||
);
|
||||
$connection->executeStatement(
|
||||
"INSERT INTO school_classes (id, tenant_id, school_id, academic_year_id, name, status, created_at, updated_at)
|
||||
VALUES (:id, :tid, :sid, :ayid, 'Test-Moy-Class', 'active', NOW(), NOW())
|
||||
ON CONFLICT (id) DO NOTHING",
|
||||
['id' => self::CLASS_ID, 'tid' => self::TENANT_ID, 'sid' => $schoolId, 'ayid' => $academicYearId],
|
||||
);
|
||||
$connection->executeStatement(
|
||||
"INSERT INTO subjects (id, tenant_id, school_id, name, code, status, created_at, updated_at)
|
||||
VALUES (:id, :tid, :sid, 'Test-Moy-Subject', 'TMOY', 'active', NOW(), NOW())
|
||||
ON CONFLICT (id) DO NOTHING",
|
||||
['id' => self::SUBJECT_ID, 'tid' => self::TENANT_ID, 'sid' => $schoolId],
|
||||
);
|
||||
$connection->executeStatement(
|
||||
"INSERT INTO academic_periods (id, tenant_id, academic_year_id, period_type, sequence, label, start_date, end_date)
|
||||
VALUES (:id, :tid, :ayid, 'trimester', 2, 'Trimestre 2', '2026-01-01', '2026-03-31')
|
||||
ON CONFLICT (id) DO NOTHING",
|
||||
['id' => self::PERIOD_ID, 'tid' => self::TENANT_ID, 'ayid' => $academicYearId],
|
||||
);
|
||||
|
||||
// Créer une évaluation publiée avec 2 notes
|
||||
$evaluation = Evaluation::creer(
|
||||
tenantId: $tenantId,
|
||||
classId: ClassId::fromString(self::CLASS_ID),
|
||||
subjectId: SubjectId::fromString(self::SUBJECT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'DS Mathématiques',
|
||||
description: null,
|
||||
evaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
gradeScale: new GradeScale(20),
|
||||
coefficient: new Coefficient(1.0),
|
||||
now: $now,
|
||||
);
|
||||
$evaluation->publierNotes($now);
|
||||
$evaluation->pullDomainEvents();
|
||||
|
||||
/** @var EvaluationRepository $evalRepo */
|
||||
$evalRepo = $container->get(EvaluationRepository::class);
|
||||
$evalRepo->save($evaluation);
|
||||
|
||||
$this->evaluationId = $evaluation->id;
|
||||
|
||||
/** @var GradeRepository $gradeRepo */
|
||||
$gradeRepo = $container->get(GradeRepository::class);
|
||||
|
||||
$student2Id = '33333333-3333-3333-3333-333333333333';
|
||||
|
||||
foreach ([
|
||||
[self::STUDENT_ID, 16.0],
|
||||
[$student2Id, 12.0],
|
||||
] as [$studentId, $value]) {
|
||||
$grade = Grade::saisir(
|
||||
tenantId: $tenantId,
|
||||
evaluationId: $evaluation->id,
|
||||
studentId: UserId::fromString($studentId),
|
||||
value: new GradeValue($value),
|
||||
status: GradeStatus::GRADED,
|
||||
gradeScale: new GradeScale(20),
|
||||
createdBy: UserId::fromString(self::TEACHER_ID),
|
||||
now: $now,
|
||||
);
|
||||
$grade->pullDomainEvents();
|
||||
$gradeRepo->save($grade);
|
||||
}
|
||||
|
||||
// Calculer et sauvegarder les statistiques
|
||||
/** @var AverageCalculator $calculator */
|
||||
$calculator = $container->get(AverageCalculator::class);
|
||||
|
||||
$stats = $calculator->calculateClassStatistics([16.0, 12.0]);
|
||||
|
||||
/** @var EvaluationStatisticsRepository $statsRepo */
|
||||
$statsRepo = $container->get(EvaluationStatisticsRepository::class);
|
||||
$statsRepo->save($evaluation->id, $stats);
|
||||
|
||||
// Sauvegarder une moyenne élève
|
||||
/** @var StudentAverageRepository $avgRepo */
|
||||
$avgRepo = $container->get(StudentAverageRepository::class);
|
||||
$avgRepo->saveSubjectAverage(
|
||||
$tenantId,
|
||||
UserId::fromString(self::STUDENT_ID),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
self::PERIOD_ID,
|
||||
16.0,
|
||||
1,
|
||||
);
|
||||
$avgRepo->saveGeneralAverage(
|
||||
$tenantId,
|
||||
UserId::fromString(self::STUDENT_ID),
|
||||
self::PERIOD_ID,
|
||||
16.0,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Application\Command\CreateAppreciationTemplate;
|
||||
|
||||
use App\Scolarite\Application\Command\CreateAppreciationTemplate\CreateAppreciationTemplateCommand;
|
||||
use App\Scolarite\Application\Command\CreateAppreciationTemplate\CreateAppreciationTemplateHandler;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationCategory;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryAppreciationTemplateRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class CreateAppreciationTemplateHandlerTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
|
||||
|
||||
private InMemoryAppreciationTemplateRepository $templateRepository;
|
||||
private Clock $clock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->templateRepository = new InMemoryAppreciationTemplateRepository();
|
||||
$this->clock = new class implements Clock {
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable('2026-03-31 10:00:00');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itCreatesTemplate(): void
|
||||
{
|
||||
$handler = new CreateAppreciationTemplateHandler($this->templateRepository, $this->clock);
|
||||
|
||||
$template = $handler(new CreateAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
title: 'Très bon travail',
|
||||
content: 'Très bon travail, continuez ainsi !',
|
||||
category: 'positive',
|
||||
));
|
||||
|
||||
self::assertSame('Très bon travail', $template->title);
|
||||
self::assertSame('Très bon travail, continuez ainsi !', $template->content);
|
||||
self::assertSame(AppreciationCategory::POSITIVE, $template->category);
|
||||
self::assertSame(0, $template->usageCount);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itPersistsTemplate(): void
|
||||
{
|
||||
$handler = new CreateAppreciationTemplateHandler($this->templateRepository, $this->clock);
|
||||
|
||||
$template = $handler(new CreateAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
title: 'Test',
|
||||
content: 'Contenu test',
|
||||
category: null,
|
||||
));
|
||||
|
||||
$found = $this->templateRepository->findById(
|
||||
$template->id,
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertNotNull($found);
|
||||
self::assertSame('Test', $found->title);
|
||||
self::assertNull($found->category);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itCreatesTemplateWithNullCategory(): void
|
||||
{
|
||||
$handler = new CreateAppreciationTemplateHandler($this->templateRepository, $this->clock);
|
||||
|
||||
$template = $handler(new CreateAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
title: 'Sans catégorie',
|
||||
content: 'Contenu',
|
||||
category: null,
|
||||
));
|
||||
|
||||
self::assertNull($template->category);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Application\Command\DeleteAppreciationTemplate;
|
||||
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Application\Command\DeleteAppreciationTemplate\DeleteAppreciationTemplateCommand;
|
||||
use App\Scolarite\Application\Command\DeleteAppreciationTemplate\DeleteAppreciationTemplateHandler;
|
||||
use App\Scolarite\Domain\Exception\AppreciationTemplateNonTrouveeException;
|
||||
use App\Scolarite\Domain\Exception\NonProprietaireDuModeleException;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationCategory;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplate;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryAppreciationTemplateRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class DeleteAppreciationTemplateHandlerTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
|
||||
|
||||
private InMemoryAppreciationTemplateRepository $templateRepository;
|
||||
private Clock $clock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->templateRepository = new InMemoryAppreciationTemplateRepository();
|
||||
$this->clock = new class implements Clock {
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable('2026-03-31 10:00:00');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itDeletesTemplate(): void
|
||||
{
|
||||
$template = $this->seedTemplate();
|
||||
$handler = new DeleteAppreciationTemplateHandler($this->templateRepository);
|
||||
|
||||
$handler(new DeleteAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
templateId: (string) $template->id,
|
||||
));
|
||||
|
||||
$found = $this->templateRepository->findById(
|
||||
$template->id,
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertNull($found);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenTemplateNotFound(): void
|
||||
{
|
||||
$handler = new DeleteAppreciationTemplateHandler($this->templateRepository);
|
||||
|
||||
$this->expectException(AppreciationTemplateNonTrouveeException::class);
|
||||
|
||||
$handler(new DeleteAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
templateId: '550e8400-e29b-41d4-a716-446655440099',
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenTeacherNotOwner(): void
|
||||
{
|
||||
$template = $this->seedTemplate();
|
||||
$handler = new DeleteAppreciationTemplateHandler($this->templateRepository);
|
||||
|
||||
$this->expectException(NonProprietaireDuModeleException::class);
|
||||
|
||||
$handler(new DeleteAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: '550e8400-e29b-41d4-a716-446655440099',
|
||||
templateId: (string) $template->id,
|
||||
));
|
||||
}
|
||||
|
||||
private function seedTemplate(): AppreciationTemplate
|
||||
{
|
||||
$template = AppreciationTemplate::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Template test',
|
||||
content: 'Contenu test',
|
||||
category: AppreciationCategory::POSITIVE,
|
||||
now: $this->clock->now(),
|
||||
);
|
||||
|
||||
$this->templateRepository->save($template);
|
||||
|
||||
return $template;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Application\Command\SaveAppreciation;
|
||||
|
||||
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\Command\SaveAppreciation\SaveAppreciationCommand;
|
||||
use App\Scolarite\Application\Command\SaveAppreciation\SaveAppreciationHandler;
|
||||
use App\Scolarite\Domain\Exception\AppreciationTropLongueException;
|
||||
use App\Scolarite\Domain\Exception\GradeNotFoundException;
|
||||
use App\Scolarite\Domain\Exception\NonProprietaireDeLEvaluationException;
|
||||
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\EvaluationStatus;
|
||||
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\Infrastructure\Persistence\InMemory\InMemoryEvaluationRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryGradeRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use function str_repeat;
|
||||
|
||||
final class SaveAppreciationHandlerTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
private const string EVALUATION_ID = '550e8400-e29b-41d4-a716-446655440040';
|
||||
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
|
||||
private const string STUDENT_ID = '550e8400-e29b-41d4-a716-446655440050';
|
||||
|
||||
private InMemoryEvaluationRepository $evaluationRepository;
|
||||
private InMemoryGradeRepository $gradeRepository;
|
||||
private Clock $clock;
|
||||
private string $gradeId;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->evaluationRepository = new InMemoryEvaluationRepository();
|
||||
$this->gradeRepository = new InMemoryGradeRepository();
|
||||
$this->clock = new class implements Clock {
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable('2026-03-31 10:00:00');
|
||||
}
|
||||
};
|
||||
|
||||
$this->seedEvaluationAndGrade();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itSavesAppreciation(): void
|
||||
{
|
||||
$handler = $this->createHandler();
|
||||
|
||||
$grade = $handler(new SaveAppreciationCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
gradeId: $this->gradeId,
|
||||
teacherId: self::TEACHER_ID,
|
||||
appreciation: 'Très bon travail',
|
||||
));
|
||||
|
||||
self::assertSame('Très bon travail', $grade->appreciation);
|
||||
self::assertNotNull($grade->appreciationUpdatedAt);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itClearsAppreciation(): void
|
||||
{
|
||||
$handler = $this->createHandler();
|
||||
|
||||
$handler(new SaveAppreciationCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
gradeId: $this->gradeId,
|
||||
teacherId: self::TEACHER_ID,
|
||||
appreciation: 'Bon travail',
|
||||
));
|
||||
|
||||
$grade = $handler(new SaveAppreciationCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
gradeId: $this->gradeId,
|
||||
teacherId: self::TEACHER_ID,
|
||||
appreciation: null,
|
||||
));
|
||||
|
||||
self::assertNull($grade->appreciation);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenTeacherNotOwner(): void
|
||||
{
|
||||
$handler = $this->createHandler();
|
||||
|
||||
$this->expectException(NonProprietaireDeLEvaluationException::class);
|
||||
|
||||
$handler(new SaveAppreciationCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
gradeId: $this->gradeId,
|
||||
teacherId: '550e8400-e29b-41d4-a716-446655440099',
|
||||
appreciation: 'Test',
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenGradeNotFound(): void
|
||||
{
|
||||
$handler = $this->createHandler();
|
||||
|
||||
$this->expectException(GradeNotFoundException::class);
|
||||
|
||||
$handler(new SaveAppreciationCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
gradeId: '550e8400-e29b-41d4-a716-446655440099',
|
||||
teacherId: self::TEACHER_ID,
|
||||
appreciation: 'Test',
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenAppreciationTooLong(): void
|
||||
{
|
||||
$handler = $this->createHandler();
|
||||
|
||||
$this->expectException(AppreciationTropLongueException::class);
|
||||
|
||||
$handler(new SaveAppreciationCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
gradeId: $this->gradeId,
|
||||
teacherId: self::TEACHER_ID,
|
||||
appreciation: str_repeat('a', 501),
|
||||
));
|
||||
}
|
||||
|
||||
private function createHandler(): SaveAppreciationHandler
|
||||
{
|
||||
return new SaveAppreciationHandler(
|
||||
$this->evaluationRepository,
|
||||
$this->gradeRepository,
|
||||
$this->clock,
|
||||
);
|
||||
}
|
||||
|
||||
private function seedEvaluationAndGrade(): void
|
||||
{
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
|
||||
$evaluation = Evaluation::reconstitute(
|
||||
id: EvaluationId::fromString(self::EVALUATION_ID),
|
||||
tenantId: $tenantId,
|
||||
classId: ClassId::fromString('550e8400-e29b-41d4-a716-446655440020'),
|
||||
subjectId: SubjectId::fromString('550e8400-e29b-41d4-a716-446655440030'),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Contrôle',
|
||||
description: null,
|
||||
evaluationDate: new DateTimeImmutable('2026-04-15'),
|
||||
gradeScale: new GradeScale(20),
|
||||
coefficient: new Coefficient(1.0),
|
||||
status: EvaluationStatus::PUBLISHED,
|
||||
createdAt: new DateTimeImmutable('2026-03-12 10:00:00'),
|
||||
updatedAt: new DateTimeImmutable('2026-03-12 10:00:00'),
|
||||
);
|
||||
|
||||
$this->evaluationRepository->save($evaluation);
|
||||
|
||||
$grade = Grade::saisir(
|
||||
tenantId: $tenantId,
|
||||
evaluationId: EvaluationId::fromString(self::EVALUATION_ID),
|
||||
studentId: UserId::fromString(self::STUDENT_ID),
|
||||
value: new GradeValue(15.5),
|
||||
status: GradeStatus::GRADED,
|
||||
gradeScale: new GradeScale(20),
|
||||
createdBy: UserId::fromString(self::TEACHER_ID),
|
||||
now: new DateTimeImmutable('2026-03-27 10:00:00'),
|
||||
);
|
||||
|
||||
$this->gradeRepository->save($grade);
|
||||
$this->gradeId = (string) $grade->id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Application\Command\UpdateAppreciationTemplate;
|
||||
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Application\Command\UpdateAppreciationTemplate\UpdateAppreciationTemplateCommand;
|
||||
use App\Scolarite\Application\Command\UpdateAppreciationTemplate\UpdateAppreciationTemplateHandler;
|
||||
use App\Scolarite\Domain\Exception\AppreciationTemplateNonTrouveeException;
|
||||
use App\Scolarite\Domain\Exception\CategorieAppreciationInvalideException;
|
||||
use App\Scolarite\Domain\Exception\NonProprietaireDuModeleException;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationCategory;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplate;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryAppreciationTemplateRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class UpdateAppreciationTemplateHandlerTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
|
||||
|
||||
private InMemoryAppreciationTemplateRepository $templateRepository;
|
||||
private Clock $clock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->templateRepository = new InMemoryAppreciationTemplateRepository();
|
||||
$this->clock = new class implements Clock {
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable('2026-03-31 10:00:00');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itUpdatesTemplate(): void
|
||||
{
|
||||
$template = $this->seedTemplate();
|
||||
$handler = new UpdateAppreciationTemplateHandler($this->templateRepository, $this->clock);
|
||||
|
||||
$updated = $handler(new UpdateAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
templateId: (string) $template->id,
|
||||
title: 'Titre modifié',
|
||||
content: 'Contenu modifié',
|
||||
category: 'improvement',
|
||||
));
|
||||
|
||||
self::assertSame('Titre modifié', $updated->title);
|
||||
self::assertSame('Contenu modifié', $updated->content);
|
||||
self::assertSame(AppreciationCategory::IMPROVEMENT, $updated->category);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itPersistsUpdatedTemplate(): void
|
||||
{
|
||||
$template = $this->seedTemplate();
|
||||
$handler = new UpdateAppreciationTemplateHandler($this->templateRepository, $this->clock);
|
||||
|
||||
$handler(new UpdateAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
templateId: (string) $template->id,
|
||||
title: 'Persisté',
|
||||
content: 'Contenu persisté',
|
||||
category: null,
|
||||
));
|
||||
|
||||
$found = $this->templateRepository->findById(
|
||||
$template->id,
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertNotNull($found);
|
||||
self::assertSame('Persisté', $found->title);
|
||||
self::assertNull($found->category);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenTemplateNotFound(): void
|
||||
{
|
||||
$handler = new UpdateAppreciationTemplateHandler($this->templateRepository, $this->clock);
|
||||
|
||||
$this->expectException(AppreciationTemplateNonTrouveeException::class);
|
||||
|
||||
$handler(new UpdateAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
templateId: '550e8400-e29b-41d4-a716-446655440099',
|
||||
title: 'Test',
|
||||
content: 'Contenu',
|
||||
category: null,
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenTeacherNotOwner(): void
|
||||
{
|
||||
$template = $this->seedTemplate();
|
||||
$handler = new UpdateAppreciationTemplateHandler($this->templateRepository, $this->clock);
|
||||
|
||||
$this->expectException(NonProprietaireDuModeleException::class);
|
||||
|
||||
$handler(new UpdateAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: '550e8400-e29b-41d4-a716-446655440099',
|
||||
templateId: (string) $template->id,
|
||||
title: 'Hijack',
|
||||
content: 'Contenu',
|
||||
category: null,
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenCategoryInvalid(): void
|
||||
{
|
||||
$template = $this->seedTemplate();
|
||||
$handler = new UpdateAppreciationTemplateHandler($this->templateRepository, $this->clock);
|
||||
|
||||
$this->expectException(CategorieAppreciationInvalideException::class);
|
||||
|
||||
$handler(new UpdateAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
templateId: (string) $template->id,
|
||||
title: 'Test',
|
||||
content: 'Contenu',
|
||||
category: 'invalid_category',
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itUpdatesTemplateWithNullCategory(): void
|
||||
{
|
||||
$template = $this->seedTemplate();
|
||||
$handler = new UpdateAppreciationTemplateHandler($this->templateRepository, $this->clock);
|
||||
|
||||
$updated = $handler(new UpdateAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
templateId: (string) $template->id,
|
||||
title: 'Sans catégorie',
|
||||
content: 'Contenu mis à jour',
|
||||
category: null,
|
||||
));
|
||||
|
||||
self::assertNull($updated->category);
|
||||
self::assertSame('Sans catégorie', $updated->title);
|
||||
}
|
||||
|
||||
private function seedTemplate(): AppreciationTemplate
|
||||
{
|
||||
$template = AppreciationTemplate::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Template original',
|
||||
content: 'Contenu original',
|
||||
category: AppreciationCategory::POSITIVE,
|
||||
now: $this->clock->now(),
|
||||
);
|
||||
|
||||
$this->templateRepository->save($template);
|
||||
|
||||
return $template;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Domain\Model\Competency;
|
||||
|
||||
use App\Scolarite\Domain\Model\Competency\CompetencyEvaluation;
|
||||
use App\Scolarite\Domain\Model\Competency\CompetencyEvaluationId;
|
||||
use App\Scolarite\Domain\Model\Competency\CompetencyId;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class CompetencyEvaluationTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function creerSetsAllProperties(): void
|
||||
{
|
||||
$evaluationId = EvaluationId::generate();
|
||||
$competencyId = CompetencyId::generate();
|
||||
|
||||
$ce = CompetencyEvaluation::creer(
|
||||
evaluationId: $evaluationId,
|
||||
competencyId: $competencyId,
|
||||
);
|
||||
|
||||
self::assertTrue($ce->evaluationId->equals($evaluationId));
|
||||
self::assertTrue($ce->competencyId->equals($competencyId));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function reconstituteRestoresAllProperties(): void
|
||||
{
|
||||
$id = CompetencyEvaluationId::generate();
|
||||
$evaluationId = EvaluationId::generate();
|
||||
$competencyId = CompetencyId::generate();
|
||||
|
||||
$ce = CompetencyEvaluation::reconstitute(
|
||||
id: $id,
|
||||
evaluationId: $evaluationId,
|
||||
competencyId: $competencyId,
|
||||
);
|
||||
|
||||
self::assertTrue($ce->id->equals($id));
|
||||
self::assertTrue($ce->evaluationId->equals($evaluationId));
|
||||
self::assertTrue($ce->competencyId->equals($competencyId));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Domain\Model\Competency;
|
||||
|
||||
use App\Scolarite\Domain\Model\Competency\CompetencyFramework;
|
||||
use App\Scolarite\Domain\Model\Competency\CompetencyFrameworkId;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class CompetencyFrameworkTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
|
||||
#[Test]
|
||||
public function creerSetsAllProperties(): void
|
||||
{
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$now = new DateTimeImmutable('2026-04-01 10:00:00');
|
||||
|
||||
$framework = CompetencyFramework::creer(
|
||||
tenantId: $tenantId,
|
||||
name: 'Socle commun',
|
||||
isDefault: true,
|
||||
now: $now,
|
||||
);
|
||||
|
||||
self::assertTrue($framework->tenantId->equals($tenantId));
|
||||
self::assertSame('Socle commun', $framework->name);
|
||||
self::assertTrue($framework->isDefault);
|
||||
self::assertEquals($now, $framework->createdAt);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function reconstituteRestoresAllProperties(): void
|
||||
{
|
||||
$id = CompetencyFrameworkId::generate();
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$createdAt = new DateTimeImmutable('2026-04-01 10:00:00');
|
||||
|
||||
$framework = CompetencyFramework::reconstitute(
|
||||
id: $id,
|
||||
tenantId: $tenantId,
|
||||
name: 'Référentiel personnalisé',
|
||||
isDefault: false,
|
||||
createdAt: $createdAt,
|
||||
);
|
||||
|
||||
self::assertTrue($framework->id->equals($id));
|
||||
self::assertTrue($framework->tenantId->equals($tenantId));
|
||||
self::assertSame('Référentiel personnalisé', $framework->name);
|
||||
self::assertFalse($framework->isDefault);
|
||||
self::assertEquals($createdAt, $framework->createdAt);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Domain\Model\Competency;
|
||||
|
||||
use App\Scolarite\Domain\Model\Competency\CompetencyLevel;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class CompetencyLevelTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function hasFourLevels(): void
|
||||
{
|
||||
self::assertCount(4, CompetencyLevel::cases());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function levelsHaveCorrectValues(): void
|
||||
{
|
||||
self::assertSame('not_acquired', CompetencyLevel::NOT_ACQUIRED->value);
|
||||
self::assertSame('in_progress', CompetencyLevel::IN_PROGRESS->value);
|
||||
self::assertSame('acquired', CompetencyLevel::ACQUIRED->value);
|
||||
self::assertSame('exceeded', CompetencyLevel::EXCEEDED->value);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function labelsAreInFrench(): void
|
||||
{
|
||||
self::assertSame('Non acquis', CompetencyLevel::NOT_ACQUIRED->label());
|
||||
self::assertSame('En cours d\'acquisition', CompetencyLevel::IN_PROGRESS->label());
|
||||
self::assertSame('Acquis', CompetencyLevel::ACQUIRED->label());
|
||||
self::assertSame('Dépassé', CompetencyLevel::EXCEEDED->label());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function sortOrderIsAscending(): void
|
||||
{
|
||||
self::assertSame(1, CompetencyLevel::NOT_ACQUIRED->sortOrder());
|
||||
self::assertSame(2, CompetencyLevel::IN_PROGRESS->sortOrder());
|
||||
self::assertSame(3, CompetencyLevel::ACQUIRED->sortOrder());
|
||||
self::assertSame(4, CompetencyLevel::EXCEEDED->sortOrder());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function colorsAreValid(): void
|
||||
{
|
||||
foreach (CompetencyLevel::cases() as $level) {
|
||||
self::assertMatchesRegularExpression('/^#[0-9a-f]{6}$/', $level->color());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Domain\Model\Competency;
|
||||
|
||||
use App\Scolarite\Domain\Model\Competency\Competency;
|
||||
use App\Scolarite\Domain\Model\Competency\CompetencyFrameworkId;
|
||||
use App\Scolarite\Domain\Model\Competency\CompetencyId;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class CompetencyTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function creerSetsAllProperties(): void
|
||||
{
|
||||
$frameworkId = CompetencyFrameworkId::generate();
|
||||
|
||||
$competency = Competency::creer(
|
||||
frameworkId: $frameworkId,
|
||||
code: 'D1.1',
|
||||
name: 'Comprendre, s\'exprimer en utilisant la langue française',
|
||||
description: 'Domaine 1 - Langages',
|
||||
parentId: null,
|
||||
sortOrder: 1,
|
||||
);
|
||||
|
||||
self::assertTrue($competency->frameworkId->equals($frameworkId));
|
||||
self::assertSame('D1.1', $competency->code);
|
||||
self::assertSame('Comprendre, s\'exprimer en utilisant la langue française', $competency->name);
|
||||
self::assertSame('Domaine 1 - Langages', $competency->description);
|
||||
self::assertNull($competency->parentId);
|
||||
self::assertSame(1, $competency->sortOrder);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function creerWithParentId(): void
|
||||
{
|
||||
$frameworkId = CompetencyFrameworkId::generate();
|
||||
$parentId = CompetencyId::generate();
|
||||
|
||||
$competency = Competency::creer(
|
||||
frameworkId: $frameworkId,
|
||||
code: 'D1.1.1',
|
||||
name: 'Lire',
|
||||
description: null,
|
||||
parentId: $parentId,
|
||||
sortOrder: 1,
|
||||
);
|
||||
|
||||
self::assertNotNull($competency->parentId);
|
||||
self::assertTrue($competency->parentId->equals($parentId));
|
||||
self::assertNull($competency->description);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function reconstituteRestoresAllProperties(): void
|
||||
{
|
||||
$id = CompetencyId::generate();
|
||||
$frameworkId = CompetencyFrameworkId::generate();
|
||||
$parentId = CompetencyId::generate();
|
||||
|
||||
$competency = Competency::reconstitute(
|
||||
id: $id,
|
||||
frameworkId: $frameworkId,
|
||||
code: 'D2',
|
||||
name: 'Méthodes et outils pour apprendre',
|
||||
description: 'Domaine 2',
|
||||
parentId: $parentId,
|
||||
sortOrder: 2,
|
||||
);
|
||||
|
||||
self::assertTrue($competency->id->equals($id));
|
||||
self::assertTrue($competency->frameworkId->equals($frameworkId));
|
||||
self::assertSame('D2', $competency->code);
|
||||
self::assertSame('Méthodes et outils pour apprendre', $competency->name);
|
||||
self::assertSame('Domaine 2', $competency->description);
|
||||
self::assertNotNull($competency->parentId);
|
||||
self::assertTrue($competency->parentId->equals($parentId));
|
||||
self::assertSame(2, $competency->sortOrder);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Domain\Model\Competency;
|
||||
|
||||
use App\Scolarite\Domain\Model\Competency\CustomCompetencyLevel;
|
||||
use App\Scolarite\Domain\Model\Competency\CustomCompetencyLevelId;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class CustomCompetencyLevelTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
|
||||
#[Test]
|
||||
public function creerSetsAllProperties(): void
|
||||
{
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
|
||||
$level = CustomCompetencyLevel::creer(
|
||||
tenantId: $tenantId,
|
||||
code: 'excellent',
|
||||
name: 'Excellent',
|
||||
color: '#9b59b6',
|
||||
sortOrder: 5,
|
||||
);
|
||||
|
||||
self::assertTrue($level->tenantId->equals($tenantId));
|
||||
self::assertSame('excellent', $level->code);
|
||||
self::assertSame('Excellent', $level->name);
|
||||
self::assertSame('#9b59b6', $level->color);
|
||||
self::assertSame(5, $level->sortOrder);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function creerAllowsNullColor(): void
|
||||
{
|
||||
$level = CustomCompetencyLevel::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
code: 'basic',
|
||||
name: 'Basique',
|
||||
color: null,
|
||||
sortOrder: 1,
|
||||
);
|
||||
|
||||
self::assertNull($level->color);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function reconstituteRestoresAllProperties(): void
|
||||
{
|
||||
$id = CustomCompetencyLevelId::generate();
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
|
||||
$level = CustomCompetencyLevel::reconstitute(
|
||||
id: $id,
|
||||
tenantId: $tenantId,
|
||||
code: 'progressing',
|
||||
name: 'En progression',
|
||||
color: '#f1c40f',
|
||||
sortOrder: 2,
|
||||
);
|
||||
|
||||
self::assertTrue($level->id->equals($id));
|
||||
self::assertTrue($level->tenantId->equals($tenantId));
|
||||
self::assertSame('progressing', $level->code);
|
||||
self::assertSame('En progression', $level->name);
|
||||
self::assertSame('#f1c40f', $level->color);
|
||||
self::assertSame(2, $level->sortOrder);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Domain\Model\Competency;
|
||||
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Domain\Event\ResultatCompetenceModifie;
|
||||
use App\Scolarite\Domain\Event\ResultatCompetenceSaisi;
|
||||
use App\Scolarite\Domain\Model\Competency\CompetencyEvaluationId;
|
||||
use App\Scolarite\Domain\Model\Competency\CompetencyLevel;
|
||||
use App\Scolarite\Domain\Model\Competency\StudentCompetencyResult;
|
||||
use App\Scolarite\Domain\Model\Competency\StudentCompetencyResultId;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class StudentCompetencyResultTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
private const string STUDENT_ID = '550e8400-e29b-41d4-a716-446655440050';
|
||||
|
||||
#[Test]
|
||||
public function saisirCreatesResultWithCorrectProperties(): void
|
||||
{
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$ceId = CompetencyEvaluationId::generate();
|
||||
$studentId = UserId::fromString(self::STUDENT_ID);
|
||||
$now = new DateTimeImmutable('2026-04-01 10:00:00');
|
||||
|
||||
$result = StudentCompetencyResult::saisir(
|
||||
tenantId: $tenantId,
|
||||
competencyEvaluationId: $ceId,
|
||||
studentId: $studentId,
|
||||
levelCode: CompetencyLevel::ACQUIRED->value,
|
||||
now: $now,
|
||||
);
|
||||
|
||||
self::assertTrue($result->tenantId->equals($tenantId));
|
||||
self::assertTrue($result->competencyEvaluationId->equals($ceId));
|
||||
self::assertTrue($result->studentId->equals($studentId));
|
||||
self::assertSame('acquired', $result->levelCode);
|
||||
self::assertEquals($now, $result->createdAt);
|
||||
self::assertEquals($now, $result->updatedAt);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function saisirRecordsEvent(): void
|
||||
{
|
||||
$result = $this->createResult();
|
||||
|
||||
$events = $result->pullDomainEvents();
|
||||
|
||||
self::assertCount(1, $events);
|
||||
self::assertInstanceOf(ResultatCompetenceSaisi::class, $events[0]);
|
||||
self::assertSame($result->id, $events[0]->resultId);
|
||||
self::assertSame('acquired', $events[0]->levelCode);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function modifierDoesNothingWhenLevelUnchanged(): void
|
||||
{
|
||||
$result = $this->createResult();
|
||||
$result->pullDomainEvents();
|
||||
$createdAt = $result->updatedAt;
|
||||
|
||||
$result->modifier(
|
||||
levelCode: CompetencyLevel::ACQUIRED->value,
|
||||
now: new DateTimeImmutable('2026-04-02 14:00:00'),
|
||||
);
|
||||
|
||||
self::assertSame('acquired', $result->levelCode);
|
||||
self::assertEquals($createdAt, $result->updatedAt);
|
||||
self::assertEmpty($result->pullDomainEvents());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function modifierUpdatesLevelAndRecordsEvent(): void
|
||||
{
|
||||
$result = $this->createResult();
|
||||
$result->pullDomainEvents();
|
||||
$modifiedAt = new DateTimeImmutable('2026-04-02 14:00:00');
|
||||
|
||||
$result->modifier(
|
||||
levelCode: CompetencyLevel::EXCEEDED->value,
|
||||
now: $modifiedAt,
|
||||
);
|
||||
|
||||
self::assertSame('exceeded', $result->levelCode);
|
||||
self::assertEquals($modifiedAt, $result->updatedAt);
|
||||
|
||||
$events = $result->pullDomainEvents();
|
||||
self::assertCount(1, $events);
|
||||
self::assertInstanceOf(ResultatCompetenceModifie::class, $events[0]);
|
||||
self::assertSame('acquired', $events[0]->oldLevelCode);
|
||||
self::assertSame('exceeded', $events[0]->newLevelCode);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function reconstituteRestoresAllProperties(): void
|
||||
{
|
||||
$id = StudentCompetencyResultId::generate();
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$ceId = CompetencyEvaluationId::generate();
|
||||
$studentId = UserId::fromString(self::STUDENT_ID);
|
||||
$createdAt = new DateTimeImmutable('2026-04-01 10:00:00');
|
||||
$updatedAt = new DateTimeImmutable('2026-04-02 14:00:00');
|
||||
|
||||
$result = StudentCompetencyResult::reconstitute(
|
||||
id: $id,
|
||||
tenantId: $tenantId,
|
||||
competencyEvaluationId: $ceId,
|
||||
studentId: $studentId,
|
||||
levelCode: CompetencyLevel::IN_PROGRESS->value,
|
||||
createdAt: $createdAt,
|
||||
updatedAt: $updatedAt,
|
||||
);
|
||||
|
||||
self::assertTrue($result->id->equals($id));
|
||||
self::assertTrue($result->tenantId->equals($tenantId));
|
||||
self::assertTrue($result->competencyEvaluationId->equals($ceId));
|
||||
self::assertTrue($result->studentId->equals($studentId));
|
||||
self::assertSame('in_progress', $result->levelCode);
|
||||
self::assertEquals($createdAt, $result->createdAt);
|
||||
self::assertEquals($updatedAt, $result->updatedAt);
|
||||
self::assertEmpty($result->pullDomainEvents());
|
||||
}
|
||||
|
||||
private function createResult(): StudentCompetencyResult
|
||||
{
|
||||
return StudentCompetencyResult::saisir(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
competencyEvaluationId: CompetencyEvaluationId::generate(),
|
||||
studentId: UserId::fromString(self::STUDENT_ID),
|
||||
levelCode: CompetencyLevel::ACQUIRED->value,
|
||||
now: new DateTimeImmutable('2026-04-01 10:00:00'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Domain\Model\Grade;
|
||||
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationCategory;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplate;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplateId;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class AppreciationTemplateTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
|
||||
|
||||
#[Test]
|
||||
public function creerSetsAllProperties(): void
|
||||
{
|
||||
$now = new DateTimeImmutable('2026-03-31 10:00:00');
|
||||
|
||||
$template = AppreciationTemplate::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Très bon travail',
|
||||
content: 'Très bon travail, continuez ainsi !',
|
||||
category: AppreciationCategory::POSITIVE,
|
||||
now: $now,
|
||||
);
|
||||
|
||||
self::assertSame('Très bon travail', $template->title);
|
||||
self::assertSame('Très bon travail, continuez ainsi !', $template->content);
|
||||
self::assertSame(AppreciationCategory::POSITIVE, $template->category);
|
||||
self::assertSame(0, $template->usageCount);
|
||||
self::assertEquals($now, $template->createdAt);
|
||||
self::assertEquals($now, $template->updatedAt);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function creerAcceptsNullCategory(): void
|
||||
{
|
||||
$template = $this->createTemplate(category: null);
|
||||
|
||||
self::assertNull($template->category);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function modifierUpdatesProperties(): void
|
||||
{
|
||||
$template = $this->createTemplate();
|
||||
$modifiedAt = new DateTimeImmutable('2026-03-31 14:00:00');
|
||||
|
||||
$template->modifier(
|
||||
title: 'Nouveau titre',
|
||||
content: 'Nouveau contenu',
|
||||
category: AppreciationCategory::IMPROVEMENT,
|
||||
now: $modifiedAt,
|
||||
);
|
||||
|
||||
self::assertSame('Nouveau titre', $template->title);
|
||||
self::assertSame('Nouveau contenu', $template->content);
|
||||
self::assertSame(AppreciationCategory::IMPROVEMENT, $template->category);
|
||||
self::assertEquals($modifiedAt, $template->updatedAt);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function incrementerUtilisationIncreasesCount(): void
|
||||
{
|
||||
$template = $this->createTemplate();
|
||||
|
||||
$template->incrementerUtilisation();
|
||||
self::assertSame(1, $template->usageCount);
|
||||
|
||||
$template->incrementerUtilisation();
|
||||
self::assertSame(2, $template->usageCount);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function reconstituteRestoresAllProperties(): void
|
||||
{
|
||||
$id = AppreciationTemplateId::generate();
|
||||
$createdAt = new DateTimeImmutable('2026-03-31 10:00:00');
|
||||
$updatedAt = new DateTimeImmutable('2026-03-31 14:00:00');
|
||||
|
||||
$template = AppreciationTemplate::reconstitute(
|
||||
id: $id,
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Titre',
|
||||
content: 'Contenu',
|
||||
category: AppreciationCategory::NEUTRAL,
|
||||
usageCount: 5,
|
||||
createdAt: $createdAt,
|
||||
updatedAt: $updatedAt,
|
||||
);
|
||||
|
||||
self::assertTrue($template->id->equals($id));
|
||||
self::assertSame('Titre', $template->title);
|
||||
self::assertSame('Contenu', $template->content);
|
||||
self::assertSame(AppreciationCategory::NEUTRAL, $template->category);
|
||||
self::assertSame(5, $template->usageCount);
|
||||
self::assertEquals($createdAt, $template->createdAt);
|
||||
self::assertEquals($updatedAt, $template->updatedAt);
|
||||
}
|
||||
|
||||
private function createTemplate(?AppreciationCategory $category = AppreciationCategory::POSITIVE): AppreciationTemplate
|
||||
{
|
||||
return AppreciationTemplate::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Très bon travail',
|
||||
content: 'Très bon travail, continuez ainsi !',
|
||||
category: $category,
|
||||
now: new DateTimeImmutable('2026-03-31 10:00:00'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ namespace App\Tests\Unit\Scolarite\Domain\Model\Grade;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Domain\Event\NoteModifiee;
|
||||
use App\Scolarite\Domain\Event\NoteSaisie;
|
||||
use App\Scolarite\Domain\Exception\AppreciationTropLongueException;
|
||||
use App\Scolarite\Domain\Exception\NoteRequiseException;
|
||||
use App\Scolarite\Domain\Exception\ValeurNoteInvalideException;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
@@ -20,6 +21,8 @@ use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use function str_repeat;
|
||||
|
||||
final class GradeTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
@@ -269,6 +272,96 @@ final class GradeTest extends TestCase
|
||||
self::assertEmpty($grade->pullDomainEvents());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function saisirAppreciationSetsAppreciation(): void
|
||||
{
|
||||
$grade = $this->createGrade();
|
||||
$now = new DateTimeImmutable('2026-03-31 10:00:00');
|
||||
|
||||
$grade->saisirAppreciation('Très bon travail', $now);
|
||||
|
||||
self::assertSame('Très bon travail', $grade->appreciation);
|
||||
self::assertEquals($now, $grade->appreciationUpdatedAt);
|
||||
self::assertEquals($now, $grade->updatedAt);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function saisirAppreciationAcceptsNull(): void
|
||||
{
|
||||
$grade = $this->createGrade();
|
||||
$grade->saisirAppreciation('Temporaire', new DateTimeImmutable('2026-03-31 09:00:00'));
|
||||
|
||||
$grade->saisirAppreciation(null, new DateTimeImmutable('2026-03-31 10:00:00'));
|
||||
|
||||
self::assertNull($grade->appreciation);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function saisirAppreciationAcceptsEmptyString(): void
|
||||
{
|
||||
$grade = $this->createGrade();
|
||||
$grade->saisirAppreciation('Temporaire', new DateTimeImmutable('2026-03-31 09:00:00'));
|
||||
|
||||
$grade->saisirAppreciation('', new DateTimeImmutable('2026-03-31 10:00:00'));
|
||||
|
||||
self::assertNull($grade->appreciation);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function saisirAppreciationThrowsWhenTooLong(): void
|
||||
{
|
||||
$grade = $this->createGrade();
|
||||
|
||||
$this->expectException(AppreciationTropLongueException::class);
|
||||
|
||||
$grade->saisirAppreciation(str_repeat('a', 501), new DateTimeImmutable('2026-03-31 10:00:00'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function saisirAppreciationAcceptsMaxLength(): void
|
||||
{
|
||||
$grade = $this->createGrade();
|
||||
|
||||
$grade->saisirAppreciation(str_repeat('a', 500), new DateTimeImmutable('2026-03-31 10:00:00'));
|
||||
|
||||
self::assertSame(500, mb_strlen($grade->appreciation ?? ''));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function newGradeHasNullAppreciation(): void
|
||||
{
|
||||
$grade = $this->createGrade();
|
||||
|
||||
self::assertNull($grade->appreciation);
|
||||
self::assertNull($grade->appreciationUpdatedAt);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function reconstituteRestoresAppreciation(): void
|
||||
{
|
||||
$gradeId = GradeId::generate();
|
||||
$createdAt = new DateTimeImmutable('2026-03-27 10:00:00');
|
||||
$updatedAt = new DateTimeImmutable('2026-03-27 14:00:00');
|
||||
$appreciationUpdatedAt = new DateTimeImmutable('2026-03-31 10:00:00');
|
||||
|
||||
$grade = Grade::reconstitute(
|
||||
id: $gradeId,
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
evaluationId: EvaluationId::fromString(self::EVALUATION_ID),
|
||||
studentId: UserId::fromString(self::STUDENT_ID),
|
||||
value: new GradeValue(15.5),
|
||||
status: GradeStatus::GRADED,
|
||||
createdBy: UserId::fromString(self::TEACHER_ID),
|
||||
createdAt: $createdAt,
|
||||
updatedAt: $updatedAt,
|
||||
appreciation: 'Bon travail',
|
||||
appreciationUpdatedAt: $appreciationUpdatedAt,
|
||||
);
|
||||
|
||||
self::assertSame('Bon travail', $grade->appreciation);
|
||||
self::assertEquals($appreciationUpdatedAt, $grade->appreciationUpdatedAt);
|
||||
}
|
||||
|
||||
private function createGrade(): Grade
|
||||
{
|
||||
return Grade::saisir(
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Domain\Service;
|
||||
|
||||
use App\Scolarite\Domain\Model\Evaluation\Coefficient;
|
||||
use App\Scolarite\Domain\Model\Evaluation\GradeScale;
|
||||
use App\Scolarite\Domain\Service\AverageCalculator;
|
||||
use App\Scolarite\Domain\Service\GradeEntry;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class AverageCalculatorTest extends TestCase
|
||||
{
|
||||
private AverageCalculator $calculator;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->calculator = new AverageCalculator();
|
||||
}
|
||||
|
||||
// --- Subject Average ---
|
||||
|
||||
#[Test]
|
||||
public function subjectAverageReturnsNullWhenNoGrades(): void
|
||||
{
|
||||
self::assertNull($this->calculator->calculateSubjectAverage([]));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function subjectAverageWithSingleGrade(): void
|
||||
{
|
||||
$grades = [
|
||||
new GradeEntry(
|
||||
value: 15.0,
|
||||
gradeScale: new GradeScale(20),
|
||||
coefficient: new Coefficient(1.0),
|
||||
),
|
||||
];
|
||||
|
||||
self::assertSame(15.0, $this->calculator->calculateSubjectAverage($grades));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function subjectAverageWithEqualCoefficients(): void
|
||||
{
|
||||
$grades = [
|
||||
new GradeEntry(value: 12.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(1.0)),
|
||||
new GradeEntry(value: 16.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(1.0)),
|
||||
new GradeEntry(value: 8.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(1.0)),
|
||||
];
|
||||
|
||||
// (12 + 16 + 8) / 3 = 12.0
|
||||
self::assertSame(12.0, $this->calculator->calculateSubjectAverage($grades));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function subjectAverageWithDifferentCoefficients(): void
|
||||
{
|
||||
$grades = [
|
||||
new GradeEntry(value: 14.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(2.0)),
|
||||
new GradeEntry(value: 8.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(1.0)),
|
||||
];
|
||||
|
||||
// (14×2 + 8×1) / (2+1) = 36/3 = 12.0
|
||||
self::assertSame(12.0, $this->calculator->calculateSubjectAverage($grades));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function subjectAverageNormalizesToScale20(): void
|
||||
{
|
||||
$grades = [
|
||||
new GradeEntry(value: 8.0, gradeScale: new GradeScale(10), coefficient: new Coefficient(1.0)),
|
||||
];
|
||||
|
||||
// 8/10 × 20 = 16.0
|
||||
self::assertSame(16.0, $this->calculator->calculateSubjectAverage($grades));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function subjectAverageWithMixedScales(): void
|
||||
{
|
||||
$grades = [
|
||||
new GradeEntry(value: 15.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(1.0)),
|
||||
new GradeEntry(value: 40.0, gradeScale: new GradeScale(100), coefficient: new Coefficient(1.0)),
|
||||
];
|
||||
|
||||
// 15/20×20=15 et 40/100×20=8 → (15+8)/2 = 11.5
|
||||
self::assertSame(11.5, $this->calculator->calculateSubjectAverage($grades));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function subjectAverageWithMixedScalesAndCoefficients(): void
|
||||
{
|
||||
$grades = [
|
||||
new GradeEntry(value: 16.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(3.0)),
|
||||
new GradeEntry(value: 7.0, gradeScale: new GradeScale(10), coefficient: new Coefficient(2.0)),
|
||||
];
|
||||
|
||||
// 16/20×20=16 (coef 3), 7/10×20=14 (coef 2)
|
||||
// (16×3 + 14×2) / (3+2) = (48+28)/5 = 76/5 = 15.2
|
||||
self::assertSame(15.2, $this->calculator->calculateSubjectAverage($grades));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function subjectAverageRoundsToTwoDecimals(): void
|
||||
{
|
||||
$grades = [
|
||||
new GradeEntry(value: 13.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(1.0)),
|
||||
new GradeEntry(value: 7.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(1.0)),
|
||||
new GradeEntry(value: 11.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(1.0)),
|
||||
];
|
||||
|
||||
// (13+7+11)/3 = 31/3 = 10.333... → 10.33
|
||||
self::assertSame(10.33, $this->calculator->calculateSubjectAverage($grades));
|
||||
}
|
||||
|
||||
// --- General Average ---
|
||||
|
||||
#[Test]
|
||||
public function generalAverageReturnsNullWhenNoSubjects(): void
|
||||
{
|
||||
self::assertNull($this->calculator->calculateGeneralAverage([]));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function generalAverageWithSingleSubject(): void
|
||||
{
|
||||
self::assertSame(14.5, $this->calculator->calculateGeneralAverage([14.5]));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function generalAverageIsArithmeticMean(): void
|
||||
{
|
||||
// (12.0 + 15.0 + 9.0) / 3 = 12.0
|
||||
self::assertSame(12.0, $this->calculator->calculateGeneralAverage([12.0, 15.0, 9.0]));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function generalAverageRoundsToTwoDecimals(): void
|
||||
{
|
||||
// (14.0 + 13.0 + 11.0) / 3 = 38/3 = 12.666... → 12.67
|
||||
self::assertSame(12.67, $this->calculator->calculateGeneralAverage([14.0, 13.0, 11.0]));
|
||||
}
|
||||
|
||||
// --- Class Statistics ---
|
||||
|
||||
#[Test]
|
||||
public function classStatisticsReturnsEmptyWhenNoGrades(): void
|
||||
{
|
||||
$stats = $this->calculator->calculateClassStatistics([]);
|
||||
|
||||
self::assertNull($stats->average);
|
||||
self::assertNull($stats->min);
|
||||
self::assertNull($stats->max);
|
||||
self::assertNull($stats->median);
|
||||
self::assertSame(0, $stats->gradedCount);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function classStatisticsWithSingleGrade(): void
|
||||
{
|
||||
$stats = $this->calculator->calculateClassStatistics([15.0]);
|
||||
|
||||
self::assertSame(15.0, $stats->average);
|
||||
self::assertSame(15.0, $stats->min);
|
||||
self::assertSame(15.0, $stats->max);
|
||||
self::assertSame(15.0, $stats->median);
|
||||
self::assertSame(1, $stats->gradedCount);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function classStatisticsWithOddNumberOfGrades(): void
|
||||
{
|
||||
$stats = $this->calculator->calculateClassStatistics([8.0, 15.0, 12.0]);
|
||||
|
||||
// Sorted: 8, 12, 15
|
||||
self::assertSame(11.67, $stats->average); // 35/3
|
||||
self::assertSame(8.0, $stats->min);
|
||||
self::assertSame(15.0, $stats->max);
|
||||
self::assertSame(12.0, $stats->median); // middle element
|
||||
self::assertSame(3, $stats->gradedCount);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function classStatisticsWithEvenNumberOfGrades(): void
|
||||
{
|
||||
$stats = $this->calculator->calculateClassStatistics([7.0, 12.0, 14.0, 18.0]);
|
||||
|
||||
// Sorted: 7, 12, 14, 18
|
||||
self::assertSame(12.75, $stats->average); // 51/4
|
||||
self::assertSame(7.0, $stats->min);
|
||||
self::assertSame(18.0, $stats->max);
|
||||
self::assertSame(13.0, $stats->median); // (12+14)/2
|
||||
self::assertSame(4, $stats->gradedCount);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function classStatisticsSortsInputValues(): void
|
||||
{
|
||||
// Input not sorted
|
||||
$stats = $this->calculator->calculateClassStatistics([18.0, 7.0, 14.0, 12.0]);
|
||||
|
||||
self::assertSame(7.0, $stats->min);
|
||||
self::assertSame(18.0, $stats->max);
|
||||
self::assertSame(13.0, $stats->median); // (12+14)/2
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function classStatisticsWithIdenticalGrades(): void
|
||||
{
|
||||
$stats = $this->calculator->calculateClassStatistics([10.0, 10.0, 10.0]);
|
||||
|
||||
self::assertSame(10.0, $stats->average);
|
||||
self::assertSame(10.0, $stats->min);
|
||||
self::assertSame(10.0, $stats->max);
|
||||
self::assertSame(10.0, $stats->median);
|
||||
self::assertSame(3, $stats->gradedCount);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function classStatisticsWithTwoGrades(): void
|
||||
{
|
||||
$stats = $this->calculator->calculateClassStatistics([6.0, 16.0]);
|
||||
|
||||
self::assertSame(11.0, $stats->average);
|
||||
self::assertSame(6.0, $stats->min);
|
||||
self::assertSame(16.0, $stats->max);
|
||||
self::assertSame(11.0, $stats->median); // (6+16)/2
|
||||
self::assertSame(2, $stats->gradedCount);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user