feat: Permettre aux enseignants de créer et gérer les devoirs
Some checks failed
CI / Backend Tests (push) Has been cancelled
CI / Frontend Tests (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled
CI / Naming Conventions (push) Has been cancelled
CI / Build Check (push) Has been cancelled

Les enseignants avaient besoin d'un outil pour créer des devoirs assignés
à leurs classes, avec filtrage automatique par matière selon la classe
sélectionnée. Le système valide que la date d'échéance tombe un jour
ouvrable (lundi-vendredi) et empêche les dates dans le passé.

Le domaine modélise le devoir comme un agrégat avec pièces jointes,
statut brouillon/publié, et événements métier (création, modification,
suppression). Les handlers de notification écoutent ces événements pour
les futurs envois aux parents et élèves.
This commit is contained in:
2026-03-12 10:11:06 +01:00
parent 56bc808d85
commit e9efb90f59
51 changed files with 4776 additions and 7 deletions

View File

@@ -0,0 +1,428 @@
<?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\Homework\Homework;
use App\Scolarite\Domain\Model\Homework\HomeworkId;
use App\Scolarite\Domain\Model\Homework\HomeworkStatus;
use App\Scolarite\Domain\Repository\HomeworkRepository;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use Doctrine\DBAL\Connection;
use PHPUnit\Framework\Attributes\Test;
/**
* Tests for homework API endpoints.
*
* @see Story 5.1 - Création de Devoirs
*/
final class HomeworkEndpointsTest extends ApiTestCase
{
/**
* Opt-in for API Platform 5.0 behavior where kernel boot is explicit.
*
* @see https://github.com/api-platform/core/issues/6971
*/
protected static ?bool $alwaysBootKernel = true;
private const string TENANT_ID = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';
private const string OWNER_TEACHER_ID = '550e8400-e29b-41d4-a716-446655440000';
private const string OTHER_TEACHER_ID = '660e8400-e29b-41d4-a716-446655440001';
private const string CLASS_ID = '550e8400-e29b-41d4-a716-776655440001';
private const string SUBJECT_ID = '550e8400-e29b-41d4-a716-886655440001';
private const string HOMEWORK_ID = '550e8400-e29b-41d4-a716-996655440001';
private const string DELETED_HOMEWORK_ID = '550e8400-e29b-41d4-a716-aa6655440001';
protected function setUp(): void
{
parent::setUp();
$this->createFixtures();
}
protected function tearDown(): void
{
$container = static::getContainer();
/** @var Connection $connection */
$connection = $container->get(Connection::class);
$connection->executeStatement('DELETE FROM homework WHERE tenant_id = :tid', ['tid' => self::TENANT_ID]);
$connection->executeStatement('DELETE FROM school_classes WHERE id = :id', ['id' => self::CLASS_ID]);
$connection->executeStatement('DELETE FROM subjects WHERE id = :id', ['id' => self::SUBJECT_ID]);
$connection->executeStatement('DELETE FROM users WHERE id IN (:o, :t)', ['o' => self::OWNER_TEACHER_ID, 't' => self::OTHER_TEACHER_ID]);
parent::tearDown();
}
private function createFixtures(): void
{
$container = static::getContainer();
/** @var Connection $connection */
$connection = $container->get(Connection::class);
$schoolId = '550e8400-e29b-41d4-a716-ff6655440001';
$academicYearId = '550e8400-e29b-41d4-a716-ff6655440002';
$connection->executeStatement(
"INSERT INTO users (id, tenant_id, email, hashed_password, first_name, last_name, roles, statut, created_at, updated_at)
VALUES (:id, :tid, 'owner-hw@test.local', '', 'Owner', 'Teacher', '[\"ROLE_PROF\"]', 'active', NOW(), NOW())
ON CONFLICT (id) DO NOTHING",
['id' => self::OWNER_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-hw@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 school_classes (id, tenant_id, school_id, academic_year_id, name, status, created_at, updated_at)
VALUES (:id, :tid, :sid, :ayid, 'Test-HW-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-HW-Subject', 'THW', 'active', NOW(), NOW())
ON CONFLICT (id) DO NOTHING",
['id' => self::SUBJECT_ID, 'tid' => self::TENANT_ID, 'sid' => $schoolId],
);
}
// =========================================================================
// Security - Without tenant (404)
// =========================================================================
#[Test]
public function getHomeworkListReturns404WithoutTenant(): void
{
$client = static::createClient();
$client->request('GET', '/api/homework', [
'headers' => [
'Host' => 'localhost',
'Accept' => 'application/json',
],
]);
self::assertResponseStatusCodeSame(404);
}
#[Test]
public function createHomeworkReturns404WithoutTenant(): void
{
$client = static::createClient();
$client->request('POST', '/api/homework', [
'headers' => [
'Host' => 'localhost',
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
'json' => [
'classId' => self::CLASS_ID,
'subjectId' => self::SUBJECT_ID,
'title' => 'Devoir de maths',
'dueDate' => '2026-06-15',
],
]);
self::assertResponseStatusCodeSame(404);
}
#[Test]
public function updateHomeworkReturns404WithoutTenant(): void
{
$client = static::createClient();
$client->request('PATCH', '/api/homework/' . self::HOMEWORK_ID, [
'headers' => [
'Host' => 'localhost',
'Accept' => 'application/json',
'Content-Type' => 'application/merge-patch+json',
],
'json' => [
'title' => 'Titre modifié',
'dueDate' => '2026-06-20',
],
]);
self::assertResponseStatusCodeSame(404);
}
#[Test]
public function deleteHomeworkReturns404WithoutTenant(): void
{
$client = static::createClient();
$client->request('DELETE', '/api/homework/' . self::HOMEWORK_ID, [
'headers' => [
'Host' => 'localhost',
'Accept' => 'application/json',
],
]);
self::assertResponseStatusCodeSame(404);
}
// =========================================================================
// Security - Without authentication (401)
// =========================================================================
#[Test]
public function getHomeworkListReturns401WithoutAuthentication(): void
{
$client = static::createClient();
$client->request('GET', 'http://ecole-alpha.classeo.local/api/homework', [
'headers' => [
'Accept' => 'application/json',
],
]);
self::assertResponseStatusCodeSame(401);
}
#[Test]
public function createHomeworkReturns401WithoutAuthentication(): void
{
$client = static::createClient();
$client->request('POST', 'http://ecole-alpha.classeo.local/api/homework', [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
'json' => [
'classId' => self::CLASS_ID,
'subjectId' => self::SUBJECT_ID,
'title' => 'Devoir de maths',
'dueDate' => '2026-06-15',
],
]);
self::assertResponseStatusCodeSame(401);
}
#[Test]
public function updateHomeworkReturns401WithoutAuthentication(): void
{
$client = static::createClient();
$client->request('PATCH', 'http://ecole-alpha.classeo.local/api/homework/' . self::HOMEWORK_ID, [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/merge-patch+json',
],
'json' => [
'title' => 'Titre modifié',
'dueDate' => '2026-06-20',
],
]);
self::assertResponseStatusCodeSame(401);
}
#[Test]
public function deleteHomeworkReturns401WithoutAuthentication(): void
{
$client = static::createClient();
$client->request('DELETE', 'http://ecole-alpha.classeo.local/api/homework/' . self::HOMEWORK_ID, [
'headers' => [
'Accept' => 'application/json',
],
]);
self::assertResponseStatusCodeSame(401);
}
// =========================================================================
// 5.1-FUNC-005 (P1) - POST /homework with empty title -> 422
// =========================================================================
#[Test]
public function createHomeworkWithEmptyTitleReturns422(): void
{
$client = $this->createAuthenticatedClient(self::OWNER_TEACHER_ID, ['ROLE_PROF']);
$client->request('POST', 'http://ecole-alpha.classeo.local/api/homework', [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
'json' => [
'classId' => self::CLASS_ID,
'subjectId' => self::SUBJECT_ID,
'title' => '',
'dueDate' => '2026-06-15',
],
]);
self::assertResponseStatusCodeSame(422);
}
// =========================================================================
// 5.1-FUNC-006 (P1) - POST /homework with past due date -> 400
// =========================================================================
#[Test]
public function createHomeworkWithPastDueDateReturns400(): void
{
$client = $this->createAuthenticatedClient(self::OWNER_TEACHER_ID, ['ROLE_PROF']);
$client->request('POST', 'http://ecole-alpha.classeo.local/api/homework', [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
'json' => [
'classId' => self::CLASS_ID,
'subjectId' => self::SUBJECT_ID,
'title' => 'Devoir de maths',
'dueDate' => '2020-01-01',
],
]);
self::assertResponseStatusCodeSame(400);
}
// =========================================================================
// 5.1-FUNC-007 (P0) - PATCH /homework/{id} by non-owner -> 403
// =========================================================================
#[Test]
public function updateHomeworkByNonOwnerReturns403(): void
{
$this->persistHomework(self::HOMEWORK_ID, HomeworkStatus::PUBLISHED);
$client = $this->createAuthenticatedClient(self::OTHER_TEACHER_ID, ['ROLE_PROF']);
$client->request('PATCH', 'http://ecole-alpha.classeo.local/api/homework/' . self::HOMEWORK_ID, [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/merge-patch+json',
],
'json' => [
'title' => 'Titre piraté',
'dueDate' => '2026-06-20',
],
]);
self::assertResponseStatusCodeSame(403);
}
// =========================================================================
// 5.1-FUNC-008 (P1) - PATCH /homework/{id} on deleted homework -> 400
// =========================================================================
#[Test]
public function updateDeletedHomeworkReturns400(): void
{
$this->persistHomework(self::DELETED_HOMEWORK_ID, HomeworkStatus::DELETED);
$client = $this->createAuthenticatedClient(self::OWNER_TEACHER_ID, ['ROLE_PROF']);
$client->request('PATCH', 'http://ecole-alpha.classeo.local/api/homework/' . self::DELETED_HOMEWORK_ID, [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/merge-patch+json',
],
'json' => [
'title' => 'Titre modifié',
'dueDate' => '2026-06-20',
],
]);
self::assertResponseStatusCodeSame(400);
}
// =========================================================================
// 5.1-FUNC-009 (P0) - DELETE /homework/{id} by non-owner -> 403
// =========================================================================
#[Test]
public function deleteHomeworkByNonOwnerReturns403(): void
{
$this->persistHomework(self::HOMEWORK_ID, HomeworkStatus::PUBLISHED);
$client = $this->createAuthenticatedClient(self::OTHER_TEACHER_ID, ['ROLE_PROF']);
$client->request('DELETE', 'http://ecole-alpha.classeo.local/api/homework/' . self::HOMEWORK_ID, [
'headers' => [
'Accept' => 'application/json',
],
]);
self::assertResponseStatusCodeSame(403);
}
// =========================================================================
// 5.1-FUNC-010 (P1) - DELETE /homework/{id} on already deleted -> 400
// =========================================================================
#[Test]
public function deleteAlreadyDeletedHomeworkReturns400(): void
{
$this->persistHomework(self::DELETED_HOMEWORK_ID, HomeworkStatus::DELETED);
$client = $this->createAuthenticatedClient(self::OWNER_TEACHER_ID, ['ROLE_PROF']);
$client->request('DELETE', 'http://ecole-alpha.classeo.local/api/homework/' . self::DELETED_HOMEWORK_ID, [
'headers' => [
'Accept' => 'application/json',
],
]);
self::assertResponseStatusCodeSame(400);
}
// =========================================================================
// Helpers
// =========================================================================
private function createAuthenticatedClient(string $userId, array $roles): \ApiPlatform\Symfony\Bundle\Test\Client
{
$client = static::createClient();
$user = new SecurityUser(
userId: UserId::fromString($userId),
email: 'teacher@classeo.local',
hashedPassword: '',
tenantId: TenantId::fromString(self::TENANT_ID),
roles: $roles,
);
$client->loginUser($user, 'api');
return $client;
}
private function persistHomework(string $homeworkId, HomeworkStatus $status): void
{
$now = new DateTimeImmutable();
$homework = Homework::reconstitute(
id: HomeworkId::fromString($homeworkId),
tenantId: TenantId::fromString(self::TENANT_ID),
classId: ClassId::fromString(self::CLASS_ID),
subjectId: SubjectId::fromString(self::SUBJECT_ID),
teacherId: UserId::fromString(self::OWNER_TEACHER_ID),
title: 'Devoir existant',
description: null,
dueDate: new DateTimeImmutable('2026-06-15'),
status: $status,
createdAt: $now,
updatedAt: $now,
);
/** @var HomeworkRepository $repository */
$repository = static::getContainer()->get(HomeworkRepository::class);
$repository->save($homework);
}
}

View File

@@ -0,0 +1,157 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Scolarite\Application\Command\CreateHomework;
use App\Administration\Domain\Model\SchoolCalendar\SchoolCalendar;
use App\Administration\Domain\Model\SchoolClass\AcademicYearId;
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\CreateHomework\CreateHomeworkCommand;
use App\Scolarite\Application\Command\CreateHomework\CreateHomeworkHandler;
use App\Scolarite\Application\Port\CurrentCalendarProvider;
use App\Scolarite\Application\Port\EnseignantAffectationChecker;
use App\Scolarite\Domain\Exception\DateEcheanceInvalideException;
use App\Scolarite\Domain\Exception\EnseignantNonAffecteException;
use App\Scolarite\Domain\Model\Homework\HomeworkId;
use App\Scolarite\Domain\Model\Homework\HomeworkStatus;
use App\Scolarite\Domain\Service\DueDateValidator;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryHomeworkRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class CreateHomeworkHandlerTest extends TestCase
{
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
private const string CLASS_ID = '550e8400-e29b-41d4-a716-446655440020';
private const string SUBJECT_ID = '550e8400-e29b-41d4-a716-446655440030';
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
private InMemoryHomeworkRepository $homeworkRepository;
private Clock $clock;
protected function setUp(): void
{
$this->homeworkRepository = new InMemoryHomeworkRepository();
$this->clock = new class implements Clock {
public function now(): DateTimeImmutable
{
return new DateTimeImmutable('2026-03-12 10:00:00');
}
};
}
#[Test]
public function itCreatesHomeworkSuccessfully(): void
{
$handler = $this->createHandler(affecte: true);
$command = $this->createCommand();
$homework = $handler($command);
self::assertNotEmpty((string) $homework->id);
self::assertSame(HomeworkStatus::PUBLISHED, $homework->status);
self::assertSame('Exercices chapitre 5', $homework->title);
}
#[Test]
public function itPersistsHomeworkInRepository(): void
{
$handler = $this->createHandler(affecte: true);
$command = $this->createCommand();
$created = $handler($command);
$homework = $this->homeworkRepository->get(
HomeworkId::fromString((string) $created->id),
TenantId::fromString(self::TENANT_ID),
);
self::assertSame('Exercices chapitre 5', $homework->title);
}
#[Test]
public function itThrowsWhenTeacherNotAffected(): void
{
$handler = $this->createHandler(affecte: false);
$this->expectException(EnseignantNonAffecteException::class);
$handler($this->createCommand());
}
#[Test]
public function itThrowsWhenDueDateIsInvalid(): void
{
$handler = $this->createHandler(affecte: true);
$this->expectException(DateEcheanceInvalideException::class);
$handler($this->createCommand(dueDate: '2026-03-11'));
}
#[Test]
public function itAllowsNullDescription(): void
{
$handler = $this->createHandler(affecte: true);
$command = $this->createCommand(description: null);
$homework = $handler($command);
self::assertNull($homework->description);
}
private function createHandler(bool $affecte): CreateHomeworkHandler
{
$affectationChecker = new class($affecte) implements EnseignantAffectationChecker {
public function __construct(private readonly bool $affecte)
{
}
public function estAffecte(UserId $teacherId, ClassId $classId, SubjectId $subjectId, TenantId $tenantId): bool
{
return $this->affecte;
}
};
$calendarProvider = new class implements CurrentCalendarProvider {
public function forCurrentYear(TenantId $tenantId): SchoolCalendar
{
return SchoolCalendar::reconstitute(
tenantId: $tenantId,
academicYearId: AcademicYearId::fromString('550e8400-e29b-41d4-a716-446655440002'),
zone: null,
entries: [],
);
}
};
return new CreateHomeworkHandler(
$this->homeworkRepository,
$affectationChecker,
$calendarProvider,
new DueDateValidator(),
$this->clock,
);
}
private function createCommand(
?string $dueDate = null,
mixed $description = 'Faire les exercices 1 à 10',
): CreateHomeworkCommand {
return new CreateHomeworkCommand(
tenantId: self::TENANT_ID,
classId: self::CLASS_ID,
subjectId: self::SUBJECT_ID,
teacherId: self::TEACHER_ID,
title: 'Exercices chapitre 5',
description: $description,
dueDate: $dueDate ?? '2026-04-15',
);
}
}

View File

@@ -0,0 +1,130 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Scolarite\Application\Command\DeleteHomework;
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\DeleteHomework\DeleteHomeworkCommand;
use App\Scolarite\Application\Command\DeleteHomework\DeleteHomeworkHandler;
use App\Scolarite\Domain\Exception\DevoirDejaSupprimeException;
use App\Scolarite\Domain\Exception\HomeworkNotFoundException;
use App\Scolarite\Domain\Exception\NonProprietaireDuDevoirException;
use App\Scolarite\Domain\Model\Homework\Homework;
use App\Scolarite\Domain\Model\Homework\HomeworkId;
use App\Scolarite\Domain\Model\Homework\HomeworkStatus;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryHomeworkRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class DeleteHomeworkHandlerTest extends TestCase
{
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
private InMemoryHomeworkRepository $homeworkRepository;
private Clock $clock;
private HomeworkId $existingHomeworkId;
protected function setUp(): void
{
$this->homeworkRepository = new InMemoryHomeworkRepository();
$this->clock = new class implements Clock {
public function now(): DateTimeImmutable
{
return new DateTimeImmutable('2026-03-12 10:00:00');
}
};
$this->seedHomework();
}
#[Test]
public function itDeletesHomeworkSuccessfully(): void
{
$handler = $this->createHandler();
$homework = $handler(new DeleteHomeworkCommand(
tenantId: self::TENANT_ID,
homeworkId: (string) $this->existingHomeworkId,
teacherId: '550e8400-e29b-41d4-a716-446655440010',
));
self::assertSame(HomeworkStatus::DELETED, $homework->status);
}
#[Test]
public function itThrowsWhenHomeworkNotFound(): void
{
$handler = $this->createHandler();
$this->expectException(HomeworkNotFoundException::class);
$handler(new DeleteHomeworkCommand(
tenantId: self::TENANT_ID,
homeworkId: (string) HomeworkId::generate(),
teacherId: '550e8400-e29b-41d4-a716-446655440010',
));
}
#[Test]
public function itThrowsWhenHomeworkAlreadyDeleted(): void
{
$homework = $this->homeworkRepository->get($this->existingHomeworkId, TenantId::fromString(self::TENANT_ID));
$homework->supprimer(new DateTimeImmutable('2026-03-12'));
$this->homeworkRepository->save($homework);
$handler = $this->createHandler();
$this->expectException(DevoirDejaSupprimeException::class);
$handler(new DeleteHomeworkCommand(
tenantId: self::TENANT_ID,
homeworkId: (string) $this->existingHomeworkId,
teacherId: '550e8400-e29b-41d4-a716-446655440010',
));
}
#[Test]
public function itThrowsWhenNotOwner(): void
{
$handler = $this->createHandler();
$this->expectException(NonProprietaireDuDevoirException::class);
$handler(new DeleteHomeworkCommand(
tenantId: self::TENANT_ID,
homeworkId: (string) $this->existingHomeworkId,
teacherId: '550e8400-e29b-41d4-a716-446655440099',
));
}
private function seedHomework(): void
{
$homework = Homework::creer(
tenantId: TenantId::fromString(self::TENANT_ID),
classId: ClassId::fromString('550e8400-e29b-41d4-a716-446655440020'),
subjectId: SubjectId::fromString('550e8400-e29b-41d4-a716-446655440030'),
teacherId: UserId::fromString('550e8400-e29b-41d4-a716-446655440010'),
title: 'Exercices',
description: 'Description',
dueDate: new DateTimeImmutable('2026-04-15'),
now: new DateTimeImmutable('2026-03-10 10:00:00'),
);
$this->existingHomeworkId = $homework->id;
$this->homeworkRepository->save($homework);
}
private function createHandler(): DeleteHomeworkHandler
{
return new DeleteHomeworkHandler(
$this->homeworkRepository,
$this->clock,
);
}
}

View File

@@ -0,0 +1,179 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Scolarite\Application\Command\UpdateHomework;
use App\Administration\Domain\Model\SchoolCalendar\SchoolCalendar;
use App\Administration\Domain\Model\SchoolClass\AcademicYearId;
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\UpdateHomework\UpdateHomeworkCommand;
use App\Scolarite\Application\Command\UpdateHomework\UpdateHomeworkHandler;
use App\Scolarite\Application\Port\CurrentCalendarProvider;
use App\Scolarite\Domain\Exception\DateEcheanceInvalideException;
use App\Scolarite\Domain\Exception\DevoirDejaSupprimeException;
use App\Scolarite\Domain\Exception\HomeworkNotFoundException;
use App\Scolarite\Domain\Exception\NonProprietaireDuDevoirException;
use App\Scolarite\Domain\Model\Homework\Homework;
use App\Scolarite\Domain\Model\Homework\HomeworkId;
use App\Scolarite\Domain\Service\DueDateValidator;
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryHomeworkRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class UpdateHomeworkHandlerTest extends TestCase
{
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
private InMemoryHomeworkRepository $homeworkRepository;
private Clock $clock;
private HomeworkId $existingHomeworkId;
protected function setUp(): void
{
$this->homeworkRepository = new InMemoryHomeworkRepository();
$this->clock = new class implements Clock {
public function now(): DateTimeImmutable
{
return new DateTimeImmutable('2026-03-12 10:00:00');
}
};
$this->seedHomework();
}
#[Test]
public function itUpdatesHomeworkSuccessfully(): void
{
$handler = $this->createHandler();
$command = new UpdateHomeworkCommand(
tenantId: self::TENANT_ID,
homeworkId: (string) $this->existingHomeworkId,
teacherId: '550e8400-e29b-41d4-a716-446655440010',
title: 'Titre modifié',
description: 'Nouvelle description',
dueDate: '2026-04-20',
);
$homework = $handler($command);
self::assertSame('Titre modifié', $homework->title);
self::assertSame('Nouvelle description', $homework->description);
}
#[Test]
public function itThrowsWhenHomeworkNotFound(): void
{
$handler = $this->createHandler();
$this->expectException(HomeworkNotFoundException::class);
$handler(new UpdateHomeworkCommand(
tenantId: self::TENANT_ID,
homeworkId: (string) HomeworkId::generate(),
teacherId: '550e8400-e29b-41d4-a716-446655440010',
title: 'Test',
description: null,
dueDate: '2026-04-20',
));
}
#[Test]
public function itThrowsWhenDueDateInvalid(): void
{
$handler = $this->createHandler();
$this->expectException(DateEcheanceInvalideException::class);
$handler(new UpdateHomeworkCommand(
tenantId: self::TENANT_ID,
homeworkId: (string) $this->existingHomeworkId,
teacherId: '550e8400-e29b-41d4-a716-446655440010',
title: 'Test',
description: null,
dueDate: '2026-03-11',
));
}
#[Test]
public function itThrowsWhenHomeworkDeleted(): void
{
$homework = $this->homeworkRepository->get($this->existingHomeworkId, TenantId::fromString(self::TENANT_ID));
$homework->supprimer(new DateTimeImmutable('2026-03-12'));
$this->homeworkRepository->save($homework);
$handler = $this->createHandler();
$this->expectException(DevoirDejaSupprimeException::class);
$handler(new UpdateHomeworkCommand(
tenantId: self::TENANT_ID,
homeworkId: (string) $this->existingHomeworkId,
teacherId: '550e8400-e29b-41d4-a716-446655440010',
title: 'Test',
description: null,
dueDate: '2026-04-20',
));
}
#[Test]
public function itThrowsWhenNotOwner(): void
{
$handler = $this->createHandler();
$this->expectException(NonProprietaireDuDevoirException::class);
$handler(new UpdateHomeworkCommand(
tenantId: self::TENANT_ID,
homeworkId: (string) $this->existingHomeworkId,
teacherId: '550e8400-e29b-41d4-a716-446655440099',
title: 'Test',
description: null,
dueDate: '2026-04-20',
));
}
private function seedHomework(): void
{
$homework = Homework::creer(
tenantId: TenantId::fromString(self::TENANT_ID),
classId: ClassId::fromString('550e8400-e29b-41d4-a716-446655440020'),
subjectId: SubjectId::fromString('550e8400-e29b-41d4-a716-446655440030'),
teacherId: UserId::fromString('550e8400-e29b-41d4-a716-446655440010'),
title: 'Exercices',
description: 'Description',
dueDate: new DateTimeImmutable('2026-04-15'),
now: new DateTimeImmutable('2026-03-10 10:00:00'),
);
$this->existingHomeworkId = $homework->id;
$this->homeworkRepository->save($homework);
}
private function createHandler(): UpdateHomeworkHandler
{
$calendarProvider = new class implements CurrentCalendarProvider {
public function forCurrentYear(TenantId $tenantId): SchoolCalendar
{
return SchoolCalendar::reconstitute(
tenantId: $tenantId,
academicYearId: AcademicYearId::fromString('550e8400-e29b-41d4-a716-446655440002'),
zone: null,
entries: [],
);
}
};
return new UpdateHomeworkHandler(
$this->homeworkRepository,
$calendarProvider,
new DueDateValidator(),
$this->clock,
);
}
}

View File

@@ -0,0 +1,106 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Scolarite\Domain\Model\Homework;
use App\Scolarite\Domain\Exception\PieceJointeInvalideException;
use App\Scolarite\Domain\Model\Homework\HomeworkAttachment;
use App\Scolarite\Domain\Model\Homework\HomeworkAttachmentId;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class HomeworkAttachmentTest extends TestCase
{
#[Test]
public function createsValidAttachment(): void
{
$id = HomeworkAttachmentId::generate();
$uploadedAt = new DateTimeImmutable('2026-03-12 10:00:00');
$attachment = new HomeworkAttachment(
id: $id,
filename: 'exercices.pdf',
filePath: 'homework/abc123/exercices.pdf',
fileSize: 500_000,
mimeType: 'application/pdf',
uploadedAt: $uploadedAt,
);
self::assertTrue($attachment->id->equals($id));
self::assertSame('exercices.pdf', $attachment->filename);
self::assertSame('homework/abc123/exercices.pdf', $attachment->filePath);
self::assertSame(500_000, $attachment->fileSize);
self::assertSame('application/pdf', $attachment->mimeType);
self::assertEquals($uploadedAt, $attachment->uploadedAt);
}
#[Test]
public function acceptsJpegMimeType(): void
{
$attachment = $this->createAttachmentWithMimeType('image/jpeg');
self::assertSame('image/jpeg', $attachment->mimeType);
}
#[Test]
public function acceptsPngMimeType(): void
{
$attachment = $this->createAttachmentWithMimeType('image/png');
self::assertSame('image/png', $attachment->mimeType);
}
#[Test]
public function rejectsInvalidMimeType(): void
{
$this->expectException(PieceJointeInvalideException::class);
$this->expectExceptionMessageMatches('/text\/plain/');
$this->createAttachmentWithMimeType('text/plain');
}
#[Test]
public function rejectsFileTooLarge(): void
{
$this->expectException(PieceJointeInvalideException::class);
$this->expectExceptionMessageMatches('/taille maximum/');
new HomeworkAttachment(
id: HomeworkAttachmentId::generate(),
filename: 'big.pdf',
filePath: 'homework/abc/big.pdf',
fileSize: 11 * 1024 * 1024, // 11 Mo
mimeType: 'application/pdf',
uploadedAt: new DateTimeImmutable(),
);
}
#[Test]
public function acceptsExactMaxSize(): void
{
$attachment = new HomeworkAttachment(
id: HomeworkAttachmentId::generate(),
filename: 'max.pdf',
filePath: 'homework/abc/max.pdf',
fileSize: 10 * 1024 * 1024, // Exactement 10 Mo
mimeType: 'application/pdf',
uploadedAt: new DateTimeImmutable(),
);
self::assertSame(10 * 1024 * 1024, $attachment->fileSize);
}
private function createAttachmentWithMimeType(string $mimeType): HomeworkAttachment
{
return new HomeworkAttachment(
id: HomeworkAttachmentId::generate(),
filename: 'test.file',
filePath: 'homework/abc/test.file',
fileSize: 1000,
mimeType: $mimeType,
uploadedAt: new DateTimeImmutable(),
);
}
}

View File

@@ -0,0 +1,223 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Scolarite\Domain\Model\Homework;
use App\Administration\Domain\Model\SchoolClass\ClassId;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\User\UserId;
use App\Scolarite\Domain\Event\DevoirCree;
use App\Scolarite\Domain\Event\DevoirModifie;
use App\Scolarite\Domain\Event\DevoirSupprime;
use App\Scolarite\Domain\Exception\DevoirDejaSupprimeException;
use App\Scolarite\Domain\Model\Homework\Homework;
use App\Scolarite\Domain\Model\Homework\HomeworkId;
use App\Scolarite\Domain\Model\Homework\HomeworkStatus;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class HomeworkTest extends TestCase
{
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
private const string CLASS_ID = '550e8400-e29b-41d4-a716-446655440020';
private const string SUBJECT_ID = '550e8400-e29b-41d4-a716-446655440030';
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
#[Test]
public function creerCreatesPublishedHomework(): void
{
$homework = $this->createHomework();
self::assertSame(HomeworkStatus::PUBLISHED, $homework->status);
}
#[Test]
public function creerRecordsDevoirCreeEvent(): void
{
$homework = $this->createHomework();
$events = $homework->pullDomainEvents();
self::assertCount(1, $events);
self::assertInstanceOf(DevoirCree::class, $events[0]);
self::assertSame($homework->id, $events[0]->homeworkId);
}
#[Test]
public function creerSetsAllProperties(): void
{
$tenantId = TenantId::fromString(self::TENANT_ID);
$classId = ClassId::fromString(self::CLASS_ID);
$subjectId = SubjectId::fromString(self::SUBJECT_ID);
$teacherId = UserId::fromString(self::TEACHER_ID);
$dueDate = new DateTimeImmutable('2026-04-15');
$now = new DateTimeImmutable('2026-03-12 10:00:00');
$homework = Homework::creer(
tenantId: $tenantId,
classId: $classId,
subjectId: $subjectId,
teacherId: $teacherId,
title: 'Exercices chapitre 5',
description: 'Faire les exercices 1 à 10',
dueDate: $dueDate,
now: $now,
);
self::assertTrue($homework->tenantId->equals($tenantId));
self::assertTrue($homework->classId->equals($classId));
self::assertTrue($homework->subjectId->equals($subjectId));
self::assertTrue($homework->teacherId->equals($teacherId));
self::assertSame('Exercices chapitre 5', $homework->title);
self::assertSame('Faire les exercices 1 à 10', $homework->description);
self::assertEquals($dueDate, $homework->dueDate);
self::assertSame(HomeworkStatus::PUBLISHED, $homework->status);
self::assertEquals($now, $homework->createdAt);
self::assertEquals($now, $homework->updatedAt);
}
#[Test]
public function creerAllowsNullDescription(): void
{
$homework = Homework::creer(
tenantId: TenantId::fromString(self::TENANT_ID),
classId: ClassId::fromString(self::CLASS_ID),
subjectId: SubjectId::fromString(self::SUBJECT_ID),
teacherId: UserId::fromString(self::TEACHER_ID),
title: 'Devoir sans description',
description: null,
dueDate: new DateTimeImmutable('2026-04-15'),
now: new DateTimeImmutable('2026-03-12 10:00:00'),
);
self::assertNull($homework->description);
}
#[Test]
public function modifierUpdatesFieldsAndRecordsEvent(): void
{
$homework = $this->createHomework();
$homework->pullDomainEvents();
$modifiedAt = new DateTimeImmutable('2026-03-13 14:00:00');
$newDueDate = new DateTimeImmutable('2026-04-20');
$homework->modifier(
title: 'Titre modifié',
description: 'Nouvelle description',
dueDate: $newDueDate,
now: $modifiedAt,
);
self::assertSame('Titre modifié', $homework->title);
self::assertSame('Nouvelle description', $homework->description);
self::assertEquals($newDueDate, $homework->dueDate);
self::assertEquals($modifiedAt, $homework->updatedAt);
$events = $homework->pullDomainEvents();
self::assertCount(1, $events);
self::assertInstanceOf(DevoirModifie::class, $events[0]);
self::assertSame($homework->id, $events[0]->homeworkId);
}
#[Test]
public function modifierThrowsWhenDeleted(): void
{
$homework = $this->createHomework();
$homework->supprimer(new DateTimeImmutable('2026-03-13'));
$this->expectException(DevoirDejaSupprimeException::class);
$homework->modifier(
title: 'Titre',
description: null,
dueDate: new DateTimeImmutable('2026-04-20'),
now: new DateTimeImmutable('2026-03-14'),
);
}
#[Test]
public function supprimerChangesStatusAndRecordsEvent(): void
{
$homework = $this->createHomework();
$homework->pullDomainEvents();
$deletedAt = new DateTimeImmutable('2026-03-14 08:00:00');
$homework->supprimer($deletedAt);
self::assertSame(HomeworkStatus::DELETED, $homework->status);
self::assertEquals($deletedAt, $homework->updatedAt);
$events = $homework->pullDomainEvents();
self::assertCount(1, $events);
self::assertInstanceOf(DevoirSupprime::class, $events[0]);
self::assertSame($homework->id, $events[0]->homeworkId);
}
#[Test]
public function supprimerThrowsWhenAlreadyDeleted(): void
{
$homework = $this->createHomework();
$homework->supprimer(new DateTimeImmutable('2026-03-14'));
$this->expectException(DevoirDejaSupprimeException::class);
$homework->supprimer(new DateTimeImmutable('2026-03-15'));
}
#[Test]
public function reconstituteRestoresAllPropertiesWithoutEvents(): void
{
$id = HomeworkId::generate();
$tenantId = TenantId::fromString(self::TENANT_ID);
$classId = ClassId::fromString(self::CLASS_ID);
$subjectId = SubjectId::fromString(self::SUBJECT_ID);
$teacherId = UserId::fromString(self::TEACHER_ID);
$dueDate = new DateTimeImmutable('2026-04-15');
$createdAt = new DateTimeImmutable('2026-03-12 10:00:00');
$updatedAt = new DateTimeImmutable('2026-03-13 14:00:00');
$homework = Homework::reconstitute(
id: $id,
tenantId: $tenantId,
classId: $classId,
subjectId: $subjectId,
teacherId: $teacherId,
title: 'Exercices chapitre 5',
description: 'Faire les exercices',
dueDate: $dueDate,
status: HomeworkStatus::PUBLISHED,
createdAt: $createdAt,
updatedAt: $updatedAt,
);
self::assertTrue($homework->id->equals($id));
self::assertTrue($homework->tenantId->equals($tenantId));
self::assertTrue($homework->classId->equals($classId));
self::assertTrue($homework->subjectId->equals($subjectId));
self::assertTrue($homework->teacherId->equals($teacherId));
self::assertSame('Exercices chapitre 5', $homework->title);
self::assertSame('Faire les exercices', $homework->description);
self::assertEquals($dueDate, $homework->dueDate);
self::assertSame(HomeworkStatus::PUBLISHED, $homework->status);
self::assertEquals($createdAt, $homework->createdAt);
self::assertEquals($updatedAt, $homework->updatedAt);
self::assertEmpty($homework->pullDomainEvents());
}
private function createHomework(): Homework
{
return Homework::creer(
tenantId: TenantId::fromString(self::TENANT_ID),
classId: ClassId::fromString(self::CLASS_ID),
subjectId: SubjectId::fromString(self::SUBJECT_ID),
teacherId: UserId::fromString(self::TEACHER_ID),
title: 'Exercices chapitre 5',
description: 'Faire les exercices 1 à 10',
dueDate: new DateTimeImmutable('2026-04-15'),
now: new DateTimeImmutable('2026-03-12 10:00:00'),
);
}
}

View File

@@ -0,0 +1,138 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Scolarite\Domain\Service;
use App\Administration\Domain\Model\SchoolCalendar\CalendarEntry;
use App\Administration\Domain\Model\SchoolCalendar\CalendarEntryId;
use App\Administration\Domain\Model\SchoolCalendar\CalendarEntryType;
use App\Administration\Domain\Model\SchoolCalendar\SchoolCalendar;
use App\Administration\Domain\Model\SchoolClass\AcademicYearId;
use App\Scolarite\Domain\Exception\DateEcheanceInvalideException;
use App\Scolarite\Domain\Service\DueDateValidator;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class DueDateValidatorTest extends TestCase
{
private DueDateValidator $validator;
protected function setUp(): void
{
$this->validator = new DueDateValidator();
}
#[Test]
public function acceptsValidFutureSchoolDay(): void
{
$now = new DateTimeImmutable('2026-03-12 10:00:00'); // Jeudi
$dueDate = new DateTimeImmutable('2026-03-16'); // Lundi
$calendar = $this->createEmptyCalendar();
$this->validator->valider($dueDate, $now, $calendar);
$this->addToAssertionCount(1);
}
#[Test]
public function rejectsPastDate(): void
{
$now = new DateTimeImmutable('2026-03-12 10:00:00');
$dueDate = new DateTimeImmutable('2026-03-11');
$calendar = $this->createEmptyCalendar();
$this->expectException(DateEcheanceInvalideException::class);
$this->expectExceptionMessageMatches('/futur/');
$this->validator->valider($dueDate, $now, $calendar);
}
#[Test]
public function rejectsTodayDate(): void
{
$now = new DateTimeImmutable('2026-03-12 10:00:00');
$dueDate = new DateTimeImmutable('2026-03-12');
$calendar = $this->createEmptyCalendar();
$this->expectException(DateEcheanceInvalideException::class);
$this->expectExceptionMessageMatches('/futur/');
$this->validator->valider($dueDate, $now, $calendar);
}
#[Test]
public function acceptsTomorrowDate(): void
{
$now = new DateTimeImmutable('2026-03-12 10:00:00'); // Jeudi
$dueDate = new DateTimeImmutable('2026-03-13'); // Vendredi
$calendar = $this->createEmptyCalendar();
$this->validator->valider($dueDate, $now, $calendar);
$this->addToAssertionCount(1);
}
#[Test]
public function rejectsWeekendDate(): void
{
$now = new DateTimeImmutable('2026-03-12 10:00:00');
$dueDate = new DateTimeImmutable('2026-03-14'); // Samedi
$calendar = $this->createEmptyCalendar();
$this->expectException(DateEcheanceInvalideException::class);
$this->expectExceptionMessageMatches('/weekend/');
$this->validator->valider($dueDate, $now, $calendar);
}
#[Test]
public function rejectsHolidayDate(): void
{
$now = new DateTimeImmutable('2026-03-12 10:00:00');
$dueDate = new DateTimeImmutable('2026-04-06'); // Lundi
$calendar = $this->createCalendarWithVacation(
new DateTimeImmutable('2026-04-04'),
new DateTimeImmutable('2026-04-19'),
'Vacances de printemps',
);
$this->expectException(DateEcheanceInvalideException::class);
$this->expectExceptionMessageMatches('/Vacances de printemps/');
$this->validator->valider($dueDate, $now, $calendar);
}
private function createEmptyCalendar(): SchoolCalendar
{
return SchoolCalendar::reconstitute(
tenantId: TenantId::fromString('550e8400-e29b-41d4-a716-446655440001'),
academicYearId: AcademicYearId::fromString('550e8400-e29b-41d4-a716-446655440002'),
zone: null,
entries: [],
);
}
private function createCalendarWithVacation(
DateTimeImmutable $start,
DateTimeImmutable $end,
string $label,
): SchoolCalendar {
return SchoolCalendar::reconstitute(
tenantId: TenantId::fromString('550e8400-e29b-41d4-a716-446655440001'),
academicYearId: AcademicYearId::fromString('550e8400-e29b-41d4-a716-446655440002'),
zone: null,
entries: [
new CalendarEntry(
id: CalendarEntryId::generate(),
type: CalendarEntryType::VACATION,
startDate: $start,
endDate: $end,
label: $label,
),
],
);
}
}

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Scolarite\Infrastructure\Storage;
use App\Scolarite\Application\Port\FileStorage;
use function is_string;
use Override;
final class InMemoryFileStorage implements FileStorage
{
/** @var array<string, string> */
private array $files = [];
#[Override]
public function upload(string $path, mixed $content, string $mimeType): string
{
$this->files[$path] = is_string($content) ? $content : '';
return $path;
}
#[Override]
public function delete(string $path): void
{
unset($this->files[$path]);
}
public function has(string $path): bool
{
return isset($this->files[$path]);
}
public function get(string $path): ?string
{
return $this->files[$path] ?? null;
}
}