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,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,
);
}
}