feat: Permettre aux enseignants de créer et gérer les devoirs
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:
@@ -206,6 +206,20 @@ services:
|
||||
App\Scolarite\Domain\Repository\TeacherReplacementRepository:
|
||||
alias: App\Scolarite\Infrastructure\Persistence\Doctrine\DoctrineTeacherReplacementRepository
|
||||
|
||||
# Homework (Story 5.1 - Création de devoirs)
|
||||
App\Scolarite\Domain\Repository\HomeworkRepository:
|
||||
alias: App\Scolarite\Infrastructure\Persistence\Doctrine\DoctrineHomeworkRepository
|
||||
|
||||
App\Scolarite\Domain\Service\DueDateValidator:
|
||||
autowire: true
|
||||
|
||||
App\Scolarite\Application\Port\FileStorage:
|
||||
alias: App\Scolarite\Infrastructure\Storage\LocalFileStorage
|
||||
|
||||
App\Scolarite\Infrastructure\Storage\LocalFileStorage:
|
||||
arguments:
|
||||
$storagePath: '%kernel.project_dir%/var/storage'
|
||||
|
||||
# Schedule (Story 4.1 - Emploi du temps)
|
||||
App\Scolarite\Domain\Repository\ScheduleSlotRepository:
|
||||
alias: App\Scolarite\Infrastructure\Persistence\Doctrine\DoctrineScheduleSlotRepository
|
||||
|
||||
60
backend/migrations/Version20260312002349.php
Normal file
60
backend/migrations/Version20260312002349.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
final class Version20260312002349 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'Create homework and homework_attachments tables for Story 5.1';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->addSql(<<<'SQL'
|
||||
CREATE TABLE homework (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
tenant_id UUID NOT NULL,
|
||||
class_id UUID NOT NULL REFERENCES school_classes(id),
|
||||
subject_id UUID NOT NULL REFERENCES subjects(id),
|
||||
teacher_id UUID NOT NULL REFERENCES users(id),
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
due_date DATE NOT NULL,
|
||||
status VARCHAR(20) DEFAULT 'published' NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
)
|
||||
SQL);
|
||||
|
||||
$this->addSql('CREATE INDEX idx_homework_tenant ON homework(tenant_id)');
|
||||
$this->addSql('CREATE INDEX idx_homework_class ON homework(class_id)');
|
||||
$this->addSql('CREATE INDEX idx_homework_due_date ON homework(due_date)');
|
||||
$this->addSql('CREATE INDEX idx_homework_teacher ON homework(teacher_id)');
|
||||
|
||||
$this->addSql(<<<'SQL'
|
||||
CREATE TABLE homework_attachments (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
homework_id UUID NOT NULL REFERENCES homework(id) ON DELETE CASCADE,
|
||||
filename VARCHAR(255) NOT NULL,
|
||||
file_path VARCHAR(500) NOT NULL,
|
||||
file_size INT NOT NULL,
|
||||
mime_type VARCHAR(100) NOT NULL,
|
||||
uploaded_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
)
|
||||
SQL);
|
||||
|
||||
$this->addSql('CREATE INDEX idx_homework_attachments_homework ON homework_attachments(homework_id)');
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$this->addSql('DROP TABLE IF EXISTS homework_attachments');
|
||||
$this->addSql('DROP TABLE IF EXISTS homework');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Command\CreateHomework;
|
||||
|
||||
final readonly class CreateHomeworkCommand
|
||||
{
|
||||
public function __construct(
|
||||
public string $tenantId,
|
||||
public string $classId,
|
||||
public string $subjectId,
|
||||
public string $teacherId,
|
||||
public string $title,
|
||||
public ?string $description,
|
||||
public string $dueDate,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Command\CreateHomework;
|
||||
|
||||
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\CurrentCalendarProvider;
|
||||
use App\Scolarite\Application\Port\EnseignantAffectationChecker;
|
||||
use App\Scolarite\Domain\Exception\EnseignantNonAffecteException;
|
||||
use App\Scolarite\Domain\Model\Homework\Homework;
|
||||
use App\Scolarite\Domain\Repository\HomeworkRepository;
|
||||
use App\Scolarite\Domain\Service\DueDateValidator;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
|
||||
#[AsMessageHandler(bus: 'command.bus')]
|
||||
final readonly class CreateHomeworkHandler
|
||||
{
|
||||
public function __construct(
|
||||
private HomeworkRepository $homeworkRepository,
|
||||
private EnseignantAffectationChecker $affectationChecker,
|
||||
private CurrentCalendarProvider $calendarProvider,
|
||||
private DueDateValidator $dueDateValidator,
|
||||
private Clock $clock,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(CreateHomeworkCommand $command): Homework
|
||||
{
|
||||
$tenantId = TenantId::fromString($command->tenantId);
|
||||
$classId = ClassId::fromString($command->classId);
|
||||
$subjectId = SubjectId::fromString($command->subjectId);
|
||||
$teacherId = UserId::fromString($command->teacherId);
|
||||
$now = $this->clock->now();
|
||||
|
||||
if (!$this->affectationChecker->estAffecte($teacherId, $classId, $subjectId, $tenantId)) {
|
||||
throw EnseignantNonAffecteException::pourClasseEtMatiere($teacherId, $classId, $subjectId);
|
||||
}
|
||||
|
||||
$calendar = $this->calendarProvider->forCurrentYear($tenantId);
|
||||
$dueDate = new DateTimeImmutable($command->dueDate);
|
||||
$this->dueDateValidator->valider($dueDate, $now, $calendar);
|
||||
|
||||
$homework = Homework::creer(
|
||||
tenantId: $tenantId,
|
||||
classId: $classId,
|
||||
subjectId: $subjectId,
|
||||
teacherId: $teacherId,
|
||||
title: $command->title,
|
||||
description: $command->description,
|
||||
dueDate: $dueDate,
|
||||
now: $now,
|
||||
);
|
||||
|
||||
$this->homeworkRepository->save($homework);
|
||||
|
||||
return $homework;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Command\DeleteHomework;
|
||||
|
||||
final readonly class DeleteHomeworkCommand
|
||||
{
|
||||
public function __construct(
|
||||
public string $tenantId,
|
||||
public string $homeworkId,
|
||||
public string $teacherId,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Command\DeleteHomework;
|
||||
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Domain\Exception\NonProprietaireDuDevoirException;
|
||||
use App\Scolarite\Domain\Model\Homework\Homework;
|
||||
use App\Scolarite\Domain\Model\Homework\HomeworkId;
|
||||
use App\Scolarite\Domain\Repository\HomeworkRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
|
||||
#[AsMessageHandler(bus: 'command.bus')]
|
||||
final readonly class DeleteHomeworkHandler
|
||||
{
|
||||
public function __construct(
|
||||
private HomeworkRepository $homeworkRepository,
|
||||
private Clock $clock,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(DeleteHomeworkCommand $command): Homework
|
||||
{
|
||||
$tenantId = TenantId::fromString($command->tenantId);
|
||||
$homeworkId = HomeworkId::fromString($command->homeworkId);
|
||||
|
||||
$homework = $this->homeworkRepository->get($homeworkId, $tenantId);
|
||||
|
||||
$teacherId = UserId::fromString($command->teacherId);
|
||||
if ((string) $homework->teacherId !== (string) $teacherId) {
|
||||
throw NonProprietaireDuDevoirException::withId($homeworkId);
|
||||
}
|
||||
|
||||
$homework->supprimer($this->clock->now());
|
||||
|
||||
$this->homeworkRepository->save($homework);
|
||||
|
||||
return $homework;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Command\UpdateHomework;
|
||||
|
||||
final readonly class UpdateHomeworkCommand
|
||||
{
|
||||
public function __construct(
|
||||
public string $tenantId,
|
||||
public string $homeworkId,
|
||||
public string $teacherId,
|
||||
public string $title,
|
||||
public ?string $description,
|
||||
public string $dueDate,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Command\UpdateHomework;
|
||||
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Application\Port\CurrentCalendarProvider;
|
||||
use App\Scolarite\Domain\Exception\NonProprietaireDuDevoirException;
|
||||
use App\Scolarite\Domain\Model\Homework\Homework;
|
||||
use App\Scolarite\Domain\Model\Homework\HomeworkId;
|
||||
use App\Scolarite\Domain\Repository\HomeworkRepository;
|
||||
use App\Scolarite\Domain\Service\DueDateValidator;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
|
||||
#[AsMessageHandler(bus: 'command.bus')]
|
||||
final readonly class UpdateHomeworkHandler
|
||||
{
|
||||
public function __construct(
|
||||
private HomeworkRepository $homeworkRepository,
|
||||
private CurrentCalendarProvider $calendarProvider,
|
||||
private DueDateValidator $dueDateValidator,
|
||||
private Clock $clock,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(UpdateHomeworkCommand $command): Homework
|
||||
{
|
||||
$tenantId = TenantId::fromString($command->tenantId);
|
||||
$homeworkId = HomeworkId::fromString($command->homeworkId);
|
||||
$now = $this->clock->now();
|
||||
|
||||
$homework = $this->homeworkRepository->get($homeworkId, $tenantId);
|
||||
|
||||
$teacherId = UserId::fromString($command->teacherId);
|
||||
if ((string) $homework->teacherId !== (string) $teacherId) {
|
||||
throw NonProprietaireDuDevoirException::withId($homeworkId);
|
||||
}
|
||||
|
||||
$calendar = $this->calendarProvider->forCurrentYear($tenantId);
|
||||
$dueDate = new DateTimeImmutable($command->dueDate);
|
||||
$this->dueDateValidator->valider($dueDate, $now, $calendar);
|
||||
|
||||
$homework->modifier(
|
||||
title: $command->title,
|
||||
description: $command->description,
|
||||
dueDate: $dueDate,
|
||||
now: $now,
|
||||
);
|
||||
|
||||
$this->homeworkRepository->save($homework);
|
||||
|
||||
return $homework;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Command\UploadHomeworkAttachment;
|
||||
|
||||
final readonly class UploadHomeworkAttachmentCommand
|
||||
{
|
||||
public function __construct(
|
||||
public string $tenantId,
|
||||
public string $homeworkId,
|
||||
public string $filename,
|
||||
public string $mimeType,
|
||||
public int $fileSize,
|
||||
public string $tempFilePath,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Command\UploadHomeworkAttachment;
|
||||
|
||||
use App\Scolarite\Application\Port\FileStorage;
|
||||
use App\Scolarite\Domain\Exception\PieceJointeInvalideException;
|
||||
use App\Scolarite\Domain\Model\Homework\HomeworkAttachment;
|
||||
use App\Scolarite\Domain\Model\Homework\HomeworkAttachmentId;
|
||||
use App\Scolarite\Domain\Model\Homework\HomeworkId;
|
||||
use App\Scolarite\Domain\Repository\HomeworkRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
|
||||
use function file_get_contents;
|
||||
use function sprintf;
|
||||
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
|
||||
#[AsMessageHandler(bus: 'command.bus')]
|
||||
final readonly class UploadHomeworkAttachmentHandler
|
||||
{
|
||||
public function __construct(
|
||||
private HomeworkRepository $homeworkRepository,
|
||||
private FileStorage $fileStorage,
|
||||
private Clock $clock,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(UploadHomeworkAttachmentCommand $command): HomeworkAttachment
|
||||
{
|
||||
$tenantId = TenantId::fromString($command->tenantId);
|
||||
$homeworkId = HomeworkId::fromString($command->homeworkId);
|
||||
|
||||
// Valider que le devoir existe
|
||||
$this->homeworkRepository->get($homeworkId, $tenantId);
|
||||
|
||||
$attachmentId = HomeworkAttachmentId::generate();
|
||||
$storagePath = sprintf('homework/%s/%s/%s', $command->tenantId, $command->homeworkId, $command->filename);
|
||||
|
||||
$content = file_get_contents($command->tempFilePath);
|
||||
|
||||
if ($content === false) {
|
||||
throw PieceJointeInvalideException::lectureFichierImpossible($command->filename);
|
||||
}
|
||||
|
||||
$this->fileStorage->upload($storagePath, $content, $command->mimeType);
|
||||
|
||||
return new HomeworkAttachment(
|
||||
id: $attachmentId,
|
||||
filename: $command->filename,
|
||||
filePath: $storagePath,
|
||||
fileSize: $command->fileSize,
|
||||
mimeType: $command->mimeType,
|
||||
uploadedAt: $this->clock->now(),
|
||||
);
|
||||
}
|
||||
}
|
||||
15
backend/src/Scolarite/Application/Port/FileStorage.php
Normal file
15
backend/src/Scolarite/Application/Port/FileStorage.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Port;
|
||||
|
||||
interface FileStorage
|
||||
{
|
||||
/**
|
||||
* @param resource|string $content
|
||||
*/
|
||||
public function upload(string $path, mixed $content, string $mimeType): string;
|
||||
|
||||
public function delete(string $path): void;
|
||||
}
|
||||
37
backend/src/Scolarite/Domain/Event/DevoirCree.php
Normal file
37
backend/src/Scolarite/Domain/Event/DevoirCree.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Event;
|
||||
|
||||
use App\Scolarite\Domain\Model\Homework\HomeworkId;
|
||||
use App\Shared\Domain\DomainEvent;
|
||||
use DateTimeImmutable;
|
||||
use Override;
|
||||
use Ramsey\Uuid\UuidInterface;
|
||||
|
||||
final readonly class DevoirCree implements DomainEvent
|
||||
{
|
||||
public function __construct(
|
||||
public HomeworkId $homeworkId,
|
||||
public string $classId,
|
||||
public string $subjectId,
|
||||
public string $teacherId,
|
||||
public string $title,
|
||||
public DateTimeImmutable $dueDate,
|
||||
private DateTimeImmutable $occurredOn,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function occurredOn(): DateTimeImmutable
|
||||
{
|
||||
return $this->occurredOn;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function aggregateId(): UuidInterface
|
||||
{
|
||||
return $this->homeworkId->value;
|
||||
}
|
||||
}
|
||||
34
backend/src/Scolarite/Domain/Event/DevoirModifie.php
Normal file
34
backend/src/Scolarite/Domain/Event/DevoirModifie.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Event;
|
||||
|
||||
use App\Scolarite\Domain\Model\Homework\HomeworkId;
|
||||
use App\Shared\Domain\DomainEvent;
|
||||
use DateTimeImmutable;
|
||||
use Override;
|
||||
use Ramsey\Uuid\UuidInterface;
|
||||
|
||||
final readonly class DevoirModifie implements DomainEvent
|
||||
{
|
||||
public function __construct(
|
||||
public HomeworkId $homeworkId,
|
||||
public string $title,
|
||||
public DateTimeImmutable $dueDate,
|
||||
private DateTimeImmutable $occurredOn,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function occurredOn(): DateTimeImmutable
|
||||
{
|
||||
return $this->occurredOn;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function aggregateId(): UuidInterface
|
||||
{
|
||||
return $this->homeworkId->value;
|
||||
}
|
||||
}
|
||||
32
backend/src/Scolarite/Domain/Event/DevoirSupprime.php
Normal file
32
backend/src/Scolarite/Domain/Event/DevoirSupprime.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Event;
|
||||
|
||||
use App\Scolarite\Domain\Model\Homework\HomeworkId;
|
||||
use App\Shared\Domain\DomainEvent;
|
||||
use DateTimeImmutable;
|
||||
use Override;
|
||||
use Ramsey\Uuid\UuidInterface;
|
||||
|
||||
final readonly class DevoirSupprime implements DomainEvent
|
||||
{
|
||||
public function __construct(
|
||||
public HomeworkId $homeworkId,
|
||||
private DateTimeImmutable $occurredOn,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function occurredOn(): DateTimeImmutable
|
||||
{
|
||||
return $this->occurredOn;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function aggregateId(): UuidInterface
|
||||
{
|
||||
return $this->homeworkId->value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Exception;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use DomainException;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
final class DateEcheanceInvalideException extends DomainException
|
||||
{
|
||||
public static function dansLePasse(DateTimeImmutable $dueDate): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'La date d\'échéance "%s" doit être dans le futur.',
|
||||
$dueDate->format('Y-m-d'),
|
||||
));
|
||||
}
|
||||
|
||||
public static function jourBloque(DateTimeImmutable $dueDate, string $raison): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'La date d\'échéance "%s" est bloquée : %s.',
|
||||
$dueDate->format('Y-m-d'),
|
||||
$raison,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Exception;
|
||||
|
||||
use App\Scolarite\Domain\Model\Homework\HomeworkId;
|
||||
use DomainException;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
final class DevoirDejaSupprimeException extends DomainException
|
||||
{
|
||||
public static function withId(HomeworkId $id): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'Le devoir avec l\'ID "%s" a déjà été supprimé.',
|
||||
$id,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Exception;
|
||||
|
||||
use App\Scolarite\Domain\Model\Homework\HomeworkId;
|
||||
use DomainException;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
final class HomeworkNotFoundException extends DomainException
|
||||
{
|
||||
public static function withId(HomeworkId $id): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'Le devoir avec l\'ID "%s" n\'a pas été trouvé.',
|
||||
$id,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Exception;
|
||||
|
||||
use App\Scolarite\Domain\Model\Homework\HomeworkId;
|
||||
use DomainException;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
final class NonProprietaireDuDevoirException extends DomainException
|
||||
{
|
||||
public static function withId(HomeworkId $id): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'Vous n\'êtes pas le propriétaire du devoir "%s".',
|
||||
$id,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Exception;
|
||||
|
||||
use DomainException;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
final class PieceJointeInvalideException extends DomainException
|
||||
{
|
||||
public static function fichierTropGros(int $fileSize, int $maxSize): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'Le fichier fait %d octets, la taille maximum est de %d octets (10 Mo).',
|
||||
$fileSize,
|
||||
$maxSize,
|
||||
));
|
||||
}
|
||||
|
||||
public static function typeFichierNonAutorise(string $mimeType): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'Le type de fichier "%s" n\'est pas autorisé. Types acceptés : PDF, JPEG, PNG.',
|
||||
$mimeType,
|
||||
));
|
||||
}
|
||||
|
||||
public static function lectureFichierImpossible(string $filename): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'Impossible de lire le fichier "%s".',
|
||||
$filename,
|
||||
));
|
||||
}
|
||||
}
|
||||
144
backend/src/Scolarite/Domain/Model/Homework/Homework.php
Normal file
144
backend/src/Scolarite/Domain/Model/Homework/Homework.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\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\Shared\Domain\AggregateRoot;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
|
||||
final class Homework extends AggregateRoot
|
||||
{
|
||||
public private(set) DateTimeImmutable $updatedAt;
|
||||
|
||||
private function __construct(
|
||||
public private(set) HomeworkId $id,
|
||||
public private(set) TenantId $tenantId,
|
||||
public private(set) ClassId $classId,
|
||||
public private(set) SubjectId $subjectId,
|
||||
public private(set) UserId $teacherId,
|
||||
public private(set) string $title,
|
||||
public private(set) ?string $description,
|
||||
public private(set) DateTimeImmutable $dueDate,
|
||||
public private(set) HomeworkStatus $status,
|
||||
public private(set) DateTimeImmutable $createdAt,
|
||||
) {
|
||||
$this->updatedAt = $createdAt;
|
||||
}
|
||||
|
||||
public static function creer(
|
||||
TenantId $tenantId,
|
||||
ClassId $classId,
|
||||
SubjectId $subjectId,
|
||||
UserId $teacherId,
|
||||
string $title,
|
||||
?string $description,
|
||||
DateTimeImmutable $dueDate,
|
||||
DateTimeImmutable $now,
|
||||
): self {
|
||||
$homework = new self(
|
||||
id: HomeworkId::generate(),
|
||||
tenantId: $tenantId,
|
||||
classId: $classId,
|
||||
subjectId: $subjectId,
|
||||
teacherId: $teacherId,
|
||||
title: $title,
|
||||
description: $description,
|
||||
dueDate: $dueDate,
|
||||
status: HomeworkStatus::PUBLISHED,
|
||||
createdAt: $now,
|
||||
);
|
||||
|
||||
$homework->recordEvent(new DevoirCree(
|
||||
homeworkId: $homework->id,
|
||||
classId: (string) $classId,
|
||||
subjectId: (string) $subjectId,
|
||||
teacherId: (string) $teacherId,
|
||||
title: $title,
|
||||
dueDate: $dueDate,
|
||||
occurredOn: $now,
|
||||
));
|
||||
|
||||
return $homework;
|
||||
}
|
||||
|
||||
public function modifier(
|
||||
string $title,
|
||||
?string $description,
|
||||
DateTimeImmutable $dueDate,
|
||||
DateTimeImmutable $now,
|
||||
): void {
|
||||
if ($this->status === HomeworkStatus::DELETED) {
|
||||
throw DevoirDejaSupprimeException::withId($this->id);
|
||||
}
|
||||
|
||||
$this->title = $title;
|
||||
$this->description = $description;
|
||||
$this->dueDate = $dueDate;
|
||||
$this->updatedAt = $now;
|
||||
|
||||
$this->recordEvent(new DevoirModifie(
|
||||
homeworkId: $this->id,
|
||||
title: $title,
|
||||
dueDate: $dueDate,
|
||||
occurredOn: $now,
|
||||
));
|
||||
}
|
||||
|
||||
public function supprimer(DateTimeImmutable $now): void
|
||||
{
|
||||
if ($this->status === HomeworkStatus::DELETED) {
|
||||
throw DevoirDejaSupprimeException::withId($this->id);
|
||||
}
|
||||
|
||||
$this->status = HomeworkStatus::DELETED;
|
||||
$this->updatedAt = $now;
|
||||
|
||||
$this->recordEvent(new DevoirSupprime(
|
||||
homeworkId: $this->id,
|
||||
occurredOn: $now,
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal Pour usage Infrastructure uniquement
|
||||
*/
|
||||
public static function reconstitute(
|
||||
HomeworkId $id,
|
||||
TenantId $tenantId,
|
||||
ClassId $classId,
|
||||
SubjectId $subjectId,
|
||||
UserId $teacherId,
|
||||
string $title,
|
||||
?string $description,
|
||||
DateTimeImmutable $dueDate,
|
||||
HomeworkStatus $status,
|
||||
DateTimeImmutable $createdAt,
|
||||
DateTimeImmutable $updatedAt,
|
||||
): self {
|
||||
$homework = new self(
|
||||
id: $id,
|
||||
tenantId: $tenantId,
|
||||
classId: $classId,
|
||||
subjectId: $subjectId,
|
||||
teacherId: $teacherId,
|
||||
title: $title,
|
||||
description: $description,
|
||||
dueDate: $dueDate,
|
||||
status: $status,
|
||||
createdAt: $createdAt,
|
||||
);
|
||||
|
||||
$homework->updatedAt = $updatedAt;
|
||||
|
||||
return $homework;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Model\Homework;
|
||||
|
||||
use App\Scolarite\Domain\Exception\PieceJointeInvalideException;
|
||||
use DateTimeImmutable;
|
||||
|
||||
use function in_array;
|
||||
|
||||
final class HomeworkAttachment
|
||||
{
|
||||
private const int MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 Mo
|
||||
|
||||
private const array ALLOWED_MIME_TYPES = [
|
||||
'application/pdf',
|
||||
'image/jpeg',
|
||||
'image/png',
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
public private(set) HomeworkAttachmentId $id,
|
||||
public private(set) string $filename,
|
||||
public private(set) string $filePath,
|
||||
public private(set) int $fileSize {
|
||||
set(int $fileSize) {
|
||||
if ($fileSize > self::MAX_FILE_SIZE) {
|
||||
throw PieceJointeInvalideException::fichierTropGros($fileSize, self::MAX_FILE_SIZE);
|
||||
}
|
||||
$this->fileSize = $fileSize;
|
||||
}
|
||||
},
|
||||
public private(set) string $mimeType {
|
||||
set(string $mimeType) {
|
||||
if (!in_array($mimeType, self::ALLOWED_MIME_TYPES, true)) {
|
||||
throw PieceJointeInvalideException::typeFichierNonAutorise($mimeType);
|
||||
}
|
||||
$this->mimeType = $mimeType;
|
||||
}
|
||||
},
|
||||
public private(set) DateTimeImmutable $uploadedAt,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Model\Homework;
|
||||
|
||||
use App\Shared\Domain\EntityId;
|
||||
|
||||
final readonly class HomeworkAttachmentId extends EntityId
|
||||
{
|
||||
}
|
||||
11
backend/src/Scolarite/Domain/Model/Homework/HomeworkId.php
Normal file
11
backend/src/Scolarite/Domain/Model/Homework/HomeworkId.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Model\Homework;
|
||||
|
||||
use App\Shared\Domain\EntityId;
|
||||
|
||||
final readonly class HomeworkId extends EntityId
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Model\Homework;
|
||||
|
||||
enum HomeworkStatus: string
|
||||
{
|
||||
case PUBLISHED = 'published';
|
||||
case DELETED = 'deleted';
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Repository;
|
||||
|
||||
use App\Administration\Domain\Model\SchoolClass\ClassId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Domain\Exception\HomeworkNotFoundException;
|
||||
use App\Scolarite\Domain\Model\Homework\Homework;
|
||||
use App\Scolarite\Domain\Model\Homework\HomeworkId;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
|
||||
interface HomeworkRepository
|
||||
{
|
||||
public function save(Homework $homework): void;
|
||||
|
||||
/** @throws HomeworkNotFoundException */
|
||||
public function get(HomeworkId $id, TenantId $tenantId): Homework;
|
||||
|
||||
public function findById(HomeworkId $id, TenantId $tenantId): ?Homework;
|
||||
|
||||
/** @return array<Homework> */
|
||||
public function findByTeacher(UserId $teacherId, TenantId $tenantId): array;
|
||||
|
||||
/** @return array<Homework> */
|
||||
public function findByClass(ClassId $classId, TenantId $tenantId): array;
|
||||
|
||||
public function delete(HomeworkId $id, TenantId $tenantId): void;
|
||||
}
|
||||
28
backend/src/Scolarite/Domain/Service/DueDateValidator.php
Normal file
28
backend/src/Scolarite/Domain/Service/DueDateValidator.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Service;
|
||||
|
||||
use App\Administration\Domain\Model\SchoolCalendar\SchoolCalendar;
|
||||
use App\Scolarite\Domain\Exception\DateEcheanceInvalideException;
|
||||
use DateTimeImmutable;
|
||||
|
||||
final readonly class DueDateValidator
|
||||
{
|
||||
public function valider(DateTimeImmutable $dueDate, DateTimeImmutable $now, SchoolCalendar $calendar): void
|
||||
{
|
||||
$tomorrow = $now->modify('+1 day')->setTime(0, 0);
|
||||
|
||||
if ($dueDate < $tomorrow) {
|
||||
throw DateEcheanceInvalideException::dansLePasse($dueDate);
|
||||
}
|
||||
|
||||
if (!$calendar->estJourOuvre($dueDate)) {
|
||||
$entry = $calendar->trouverEntreePourDate($dueDate);
|
||||
$raison = $entry !== null ? $entry->label : 'weekend';
|
||||
|
||||
throw DateEcheanceInvalideException::jourBloque($dueDate, $raison);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Api\Processor;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\Administration\Infrastructure\Security\SecurityUser;
|
||||
use App\Scolarite\Application\Command\CreateHomework\CreateHomeworkCommand;
|
||||
use App\Scolarite\Application\Command\CreateHomework\CreateHomeworkHandler;
|
||||
use App\Scolarite\Domain\Exception\DateEcheanceInvalideException;
|
||||
use App\Scolarite\Domain\Exception\EnseignantNonAffecteException;
|
||||
use App\Scolarite\Infrastructure\Api\Resource\HomeworkResource;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use Override;
|
||||
use Ramsey\Uuid\Exception\InvalidUuidStringException;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
|
||||
/**
|
||||
* @implements ProcessorInterface<HomeworkResource, HomeworkResource>
|
||||
*/
|
||||
final readonly class CreateHomeworkProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
private CreateHomeworkHandler $handler,
|
||||
private TenantContext $tenantContext,
|
||||
private MessageBusInterface $eventBus,
|
||||
private Security $security,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param HomeworkResource $data
|
||||
*/
|
||||
#[Override]
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): HomeworkResource
|
||||
{
|
||||
if (!$this->tenantContext->hasTenant()) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
|
||||
}
|
||||
|
||||
$user = $this->security->getUser();
|
||||
|
||||
if (!$user instanceof SecurityUser) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Authentification requise.');
|
||||
}
|
||||
|
||||
try {
|
||||
$command = new CreateHomeworkCommand(
|
||||
tenantId: (string) $this->tenantContext->getCurrentTenantId(),
|
||||
classId: $data->classId ?? '',
|
||||
subjectId: $data->subjectId ?? '',
|
||||
teacherId: $user->userId(),
|
||||
title: $data->title ?? '',
|
||||
description: $data->description,
|
||||
dueDate: $data->dueDate ?? '',
|
||||
);
|
||||
|
||||
$homework = ($this->handler)($command);
|
||||
|
||||
foreach ($homework->pullDomainEvents() as $event) {
|
||||
$this->eventBus->dispatch($event);
|
||||
}
|
||||
|
||||
return HomeworkResource::fromDomain($homework);
|
||||
} catch (EnseignantNonAffecteException|DateEcheanceInvalideException $e) {
|
||||
throw new BadRequestHttpException($e->getMessage());
|
||||
} catch (InvalidUuidStringException $e) {
|
||||
throw new BadRequestHttpException('UUID invalide : ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Api\Processor;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\Administration\Infrastructure\Security\SecurityUser;
|
||||
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\Infrastructure\Api\Resource\HomeworkResource;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use Override;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
|
||||
/**
|
||||
* @implements ProcessorInterface<HomeworkResource, HomeworkResource>
|
||||
*/
|
||||
final readonly class DeleteHomeworkProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
private DeleteHomeworkHandler $handler,
|
||||
private TenantContext $tenantContext,
|
||||
private MessageBusInterface $eventBus,
|
||||
private Security $security,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param HomeworkResource $data
|
||||
*/
|
||||
#[Override]
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): HomeworkResource
|
||||
{
|
||||
if (!$this->tenantContext->hasTenant()) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
|
||||
}
|
||||
|
||||
$user = $this->security->getUser();
|
||||
|
||||
if (!$user instanceof SecurityUser) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Authentification requise.');
|
||||
}
|
||||
|
||||
/** @var string $id */
|
||||
$id = $uriVariables['id'];
|
||||
|
||||
try {
|
||||
$command = new DeleteHomeworkCommand(
|
||||
tenantId: (string) $this->tenantContext->getCurrentTenantId(),
|
||||
homeworkId: $id,
|
||||
teacherId: $user->userId(),
|
||||
);
|
||||
|
||||
$homework = ($this->handler)($command);
|
||||
|
||||
foreach ($homework->pullDomainEvents() as $event) {
|
||||
$this->eventBus->dispatch($event);
|
||||
}
|
||||
|
||||
return HomeworkResource::fromDomain($homework);
|
||||
} catch (HomeworkNotFoundException $e) {
|
||||
throw new NotFoundHttpException($e->getMessage());
|
||||
} catch (NonProprietaireDuDevoirException $e) {
|
||||
throw new AccessDeniedHttpException($e->getMessage());
|
||||
} catch (DevoirDejaSupprimeException $e) {
|
||||
throw new BadRequestHttpException($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Api\Processor;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\Administration\Infrastructure\Security\SecurityUser;
|
||||
use App\Scolarite\Application\Command\UpdateHomework\UpdateHomeworkCommand;
|
||||
use App\Scolarite\Application\Command\UpdateHomework\UpdateHomeworkHandler;
|
||||
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\Infrastructure\Api\Resource\HomeworkResource;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use Override;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
|
||||
/**
|
||||
* @implements ProcessorInterface<HomeworkResource, HomeworkResource>
|
||||
*/
|
||||
final readonly class UpdateHomeworkProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
private UpdateHomeworkHandler $handler,
|
||||
private TenantContext $tenantContext,
|
||||
private MessageBusInterface $eventBus,
|
||||
private Security $security,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param HomeworkResource $data
|
||||
*/
|
||||
#[Override]
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): HomeworkResource
|
||||
{
|
||||
if (!$this->tenantContext->hasTenant()) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
|
||||
}
|
||||
|
||||
$user = $this->security->getUser();
|
||||
|
||||
if (!$user instanceof SecurityUser) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Authentification requise.');
|
||||
}
|
||||
|
||||
/** @var string $id */
|
||||
$id = $uriVariables['id'];
|
||||
|
||||
try {
|
||||
$command = new UpdateHomeworkCommand(
|
||||
tenantId: (string) $this->tenantContext->getCurrentTenantId(),
|
||||
homeworkId: $id,
|
||||
teacherId: $user->userId(),
|
||||
title: $data->title ?? '',
|
||||
description: $data->description,
|
||||
dueDate: $data->dueDate ?? '',
|
||||
);
|
||||
|
||||
$homework = ($this->handler)($command);
|
||||
|
||||
foreach ($homework->pullDomainEvents() as $event) {
|
||||
$this->eventBus->dispatch($event);
|
||||
}
|
||||
|
||||
return HomeworkResource::fromDomain($homework);
|
||||
} catch (HomeworkNotFoundException $e) {
|
||||
throw new NotFoundHttpException($e->getMessage());
|
||||
} catch (NonProprietaireDuDevoirException $e) {
|
||||
throw new AccessDeniedHttpException($e->getMessage());
|
||||
} catch (DateEcheanceInvalideException|DevoirDejaSupprimeException $e) {
|
||||
throw new BadRequestHttpException($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Api\Provider;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Administration\Domain\Repository\ClassRepository;
|
||||
use App\Administration\Domain\Repository\SubjectRepository;
|
||||
use App\Administration\Infrastructure\Security\SecurityUser;
|
||||
use App\Scolarite\Domain\Model\Homework\Homework;
|
||||
use App\Scolarite\Domain\Repository\HomeworkRepository;
|
||||
use App\Scolarite\Infrastructure\Api\Resource\HomeworkResource;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
|
||||
use function array_map;
|
||||
|
||||
use Override;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
|
||||
/**
|
||||
* @implements ProviderInterface<HomeworkResource>
|
||||
*/
|
||||
final readonly class HomeworkCollectionProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private HomeworkRepository $homeworkRepository,
|
||||
private TenantContext $tenantContext,
|
||||
private Security $security,
|
||||
private ClassRepository $classRepository,
|
||||
private SubjectRepository $subjectRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
/** @return array<HomeworkResource> */
|
||||
#[Override]
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): array
|
||||
{
|
||||
if (!$this->tenantContext->hasTenant()) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
|
||||
}
|
||||
|
||||
$user = $this->security->getUser();
|
||||
|
||||
if (!$user instanceof SecurityUser) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Authentification requise.');
|
||||
}
|
||||
|
||||
$tenantId = $this->tenantContext->getCurrentTenantId();
|
||||
$teacherId = UserId::fromString($user->userId());
|
||||
|
||||
$homeworks = $this->homeworkRepository->findByTeacher($teacherId, $tenantId);
|
||||
|
||||
return array_map(fn (Homework $homework) => HomeworkResource::fromDomain(
|
||||
$homework,
|
||||
$this->resolveClassName($homework),
|
||||
$this->resolveSubjectName($homework),
|
||||
), $homeworks);
|
||||
}
|
||||
|
||||
private function resolveClassName(Homework $homework): ?string
|
||||
{
|
||||
$class = $this->classRepository->findById($homework->classId);
|
||||
|
||||
return $class !== null ? (string) $class->name : null;
|
||||
}
|
||||
|
||||
private function resolveSubjectName(Homework $homework): ?string
|
||||
{
|
||||
$subject = $this->subjectRepository->findById($homework->subjectId);
|
||||
|
||||
return $subject !== null ? (string) $subject->name : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Api\Provider;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\Administration\Domain\Repository\ClassRepository;
|
||||
use App\Administration\Domain\Repository\SubjectRepository;
|
||||
use App\Administration\Infrastructure\Security\SecurityUser;
|
||||
use App\Scolarite\Domain\Model\Homework\HomeworkId;
|
||||
use App\Scolarite\Domain\Repository\HomeworkRepository;
|
||||
use App\Scolarite\Infrastructure\Api\Resource\HomeworkResource;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use Override;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
|
||||
/**
|
||||
* @implements ProviderInterface<HomeworkResource>
|
||||
*/
|
||||
final readonly class HomeworkItemProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private HomeworkRepository $homeworkRepository,
|
||||
private TenantContext $tenantContext,
|
||||
private Security $security,
|
||||
private ClassRepository $classRepository,
|
||||
private SubjectRepository $subjectRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): HomeworkResource
|
||||
{
|
||||
if (!$this->tenantContext->hasTenant()) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
|
||||
}
|
||||
|
||||
$user = $this->security->getUser();
|
||||
|
||||
if (!$user instanceof SecurityUser) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Authentification requise.');
|
||||
}
|
||||
|
||||
/** @var string $id */
|
||||
$id = $uriVariables['id'];
|
||||
|
||||
$homework = $this->homeworkRepository->findById(
|
||||
HomeworkId::fromString($id),
|
||||
$this->tenantContext->getCurrentTenantId(),
|
||||
);
|
||||
|
||||
if ($homework === null) {
|
||||
throw new NotFoundHttpException('Devoir non trouvé.');
|
||||
}
|
||||
|
||||
if ((string) $homework->teacherId !== $user->userId()) {
|
||||
throw new AccessDeniedHttpException('Vous n\'êtes pas le propriétaire de ce devoir.');
|
||||
}
|
||||
|
||||
$class = $this->classRepository->findById($homework->classId);
|
||||
$subject = $this->subjectRepository->findById($homework->subjectId);
|
||||
|
||||
return HomeworkResource::fromDomain(
|
||||
$homework,
|
||||
$class !== null ? (string) $class->name : null,
|
||||
$subject !== null ? (string) $subject->name : null,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Api\Resource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiProperty;
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Delete;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use ApiPlatform\Metadata\GetCollection;
|
||||
use ApiPlatform\Metadata\Patch;
|
||||
use ApiPlatform\Metadata\Post;
|
||||
use App\Scolarite\Domain\Model\Homework\Homework;
|
||||
use App\Scolarite\Infrastructure\Api\Processor\CreateHomeworkProcessor;
|
||||
use App\Scolarite\Infrastructure\Api\Processor\DeleteHomeworkProcessor;
|
||||
use App\Scolarite\Infrastructure\Api\Processor\UpdateHomeworkProcessor;
|
||||
use App\Scolarite\Infrastructure\Api\Provider\HomeworkCollectionProvider;
|
||||
use App\Scolarite\Infrastructure\Api\Provider\HomeworkItemProvider;
|
||||
use DateTimeImmutable;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
#[ApiResource(
|
||||
shortName: 'Homework',
|
||||
operations: [
|
||||
new GetCollection(
|
||||
uriTemplate: '/homework',
|
||||
provider: HomeworkCollectionProvider::class,
|
||||
name: 'get_homework_list',
|
||||
),
|
||||
new Get(
|
||||
uriTemplate: '/homework/{id}',
|
||||
provider: HomeworkItemProvider::class,
|
||||
name: 'get_homework',
|
||||
),
|
||||
new Post(
|
||||
uriTemplate: '/homework',
|
||||
processor: CreateHomeworkProcessor::class,
|
||||
validationContext: ['groups' => ['Default', 'create']],
|
||||
name: 'create_homework',
|
||||
),
|
||||
new Patch(
|
||||
uriTemplate: '/homework/{id}',
|
||||
provider: HomeworkItemProvider::class,
|
||||
processor: UpdateHomeworkProcessor::class,
|
||||
validationContext: ['groups' => ['Default', 'update']],
|
||||
name: 'update_homework',
|
||||
),
|
||||
new Delete(
|
||||
uriTemplate: '/homework/{id}',
|
||||
provider: HomeworkItemProvider::class,
|
||||
processor: DeleteHomeworkProcessor::class,
|
||||
name: 'delete_homework',
|
||||
),
|
||||
],
|
||||
)]
|
||||
final class HomeworkResource
|
||||
{
|
||||
#[ApiProperty(identifier: true)]
|
||||
public ?string $id = null;
|
||||
|
||||
#[Assert\NotBlank(message: 'La classe est requise.', groups: ['create'])]
|
||||
#[Assert\Uuid(message: 'L\'identifiant de la classe doit être un UUID valide.', groups: ['create'])]
|
||||
public ?string $classId = null;
|
||||
|
||||
#[Assert\NotBlank(message: 'La matière est requise.', groups: ['create'])]
|
||||
#[Assert\Uuid(message: 'L\'identifiant de la matière doit être un UUID valide.', groups: ['create'])]
|
||||
public ?string $subjectId = null;
|
||||
|
||||
public ?string $teacherId = null;
|
||||
|
||||
#[Assert\NotBlank(message: 'Le titre est requis.', groups: ['create', 'update'])]
|
||||
#[Assert\Length(max: 255, maxMessage: 'Le titre ne peut pas dépasser 255 caractères.')]
|
||||
public ?string $title = null;
|
||||
|
||||
public ?string $description = null;
|
||||
|
||||
#[Assert\NotBlank(message: 'La date d\'échéance est requise.', groups: ['create', 'update'])]
|
||||
public ?string $dueDate = null;
|
||||
|
||||
public ?string $status = null;
|
||||
|
||||
public ?string $className = null;
|
||||
|
||||
public ?string $subjectName = null;
|
||||
|
||||
public ?DateTimeImmutable $createdAt = null;
|
||||
|
||||
public ?DateTimeImmutable $updatedAt = null;
|
||||
|
||||
public static function fromDomain(
|
||||
Homework $homework,
|
||||
?string $className = null,
|
||||
?string $subjectName = null,
|
||||
): self {
|
||||
$resource = new self();
|
||||
$resource->id = (string) $homework->id;
|
||||
$resource->classId = (string) $homework->classId;
|
||||
$resource->subjectId = (string) $homework->subjectId;
|
||||
$resource->teacherId = (string) $homework->teacherId;
|
||||
$resource->title = $homework->title;
|
||||
$resource->description = $homework->description;
|
||||
$resource->dueDate = $homework->dueDate->format('Y-m-d');
|
||||
$resource->status = $homework->status->value;
|
||||
$resource->className = $className;
|
||||
$resource->subjectName = $subjectName;
|
||||
$resource->createdAt = $homework->createdAt;
|
||||
$resource->updatedAt = $homework->updatedAt;
|
||||
|
||||
return $resource;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Messaging;
|
||||
|
||||
use App\Scolarite\Domain\Event\DevoirCree;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
|
||||
/**
|
||||
* Réagit à la création d'un devoir.
|
||||
*
|
||||
* @todo #0 Implémenter la notification push/email aux élèves et parents (dépend de Epic 9)
|
||||
*/
|
||||
#[AsMessageHandler(bus: 'event.bus')]
|
||||
final readonly class OnDevoirCreeHandler
|
||||
{
|
||||
public function __construct(
|
||||
private LoggerInterface $logger,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(DevoirCree $event): void
|
||||
{
|
||||
$this->logger->info('Nouveau devoir créé — notification prévue', [
|
||||
'homework_id' => (string) $event->homeworkId,
|
||||
'class_id' => $event->classId,
|
||||
'subject_id' => $event->subjectId,
|
||||
'teacher_id' => $event->teacherId,
|
||||
'title' => $event->title,
|
||||
'due_date' => $event->dueDate->format('Y-m-d'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Messaging;
|
||||
|
||||
use App\Scolarite\Domain\Event\DevoirModifie;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
|
||||
/**
|
||||
* Réagit à la modification d'un devoir publié.
|
||||
*
|
||||
* @todo #0 Implémenter la notification push/email aux concernés (dépend de Epic 9)
|
||||
*/
|
||||
#[AsMessageHandler(bus: 'event.bus')]
|
||||
final readonly class OnDevoirModifieHandler
|
||||
{
|
||||
public function __construct(
|
||||
private LoggerInterface $logger,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(DevoirModifie $event): void
|
||||
{
|
||||
$this->logger->info('Devoir modifié — notification prévue', [
|
||||
'homework_id' => (string) $event->homeworkId,
|
||||
'title' => $event->title,
|
||||
'due_date' => $event->dueDate->format('Y-m-d'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Persistence\Doctrine;
|
||||
|
||||
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\Exception\HomeworkNotFoundException;
|
||||
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 function array_map;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Override;
|
||||
|
||||
final readonly class DoctrineHomeworkRepository implements HomeworkRepository
|
||||
{
|
||||
public function __construct(
|
||||
private Connection $connection,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function save(Homework $homework): void
|
||||
{
|
||||
$this->connection->executeStatement(
|
||||
'INSERT INTO homework (id, tenant_id, class_id, subject_id, teacher_id, title, description, due_date, status, created_at, updated_at)
|
||||
VALUES (:id, :tenant_id, :class_id, :subject_id, :teacher_id, :title, :description, :due_date, :status, :created_at, :updated_at)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
title = EXCLUDED.title,
|
||||
description = EXCLUDED.description,
|
||||
due_date = EXCLUDED.due_date,
|
||||
status = EXCLUDED.status,
|
||||
updated_at = EXCLUDED.updated_at',
|
||||
[
|
||||
'id' => (string) $homework->id,
|
||||
'tenant_id' => (string) $homework->tenantId,
|
||||
'class_id' => (string) $homework->classId,
|
||||
'subject_id' => (string) $homework->subjectId,
|
||||
'teacher_id' => (string) $homework->teacherId,
|
||||
'title' => $homework->title,
|
||||
'description' => $homework->description,
|
||||
'due_date' => $homework->dueDate->format('Y-m-d'),
|
||||
'status' => $homework->status->value,
|
||||
'created_at' => $homework->createdAt->format(DateTimeImmutable::ATOM),
|
||||
'updated_at' => $homework->updatedAt->format(DateTimeImmutable::ATOM),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function get(HomeworkId $id, TenantId $tenantId): Homework
|
||||
{
|
||||
$homework = $this->findById($id, $tenantId);
|
||||
|
||||
if ($homework === null) {
|
||||
throw HomeworkNotFoundException::withId($id);
|
||||
}
|
||||
|
||||
return $homework;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findById(HomeworkId $id, TenantId $tenantId): ?Homework
|
||||
{
|
||||
$row = $this->connection->fetchAssociative(
|
||||
'SELECT * FROM homework WHERE id = :id AND tenant_id = :tenant_id',
|
||||
['id' => (string) $id, 'tenant_id' => (string) $tenantId],
|
||||
);
|
||||
|
||||
if ($row === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->hydrate($row);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findByTeacher(UserId $teacherId, TenantId $tenantId): array
|
||||
{
|
||||
$rows = $this->connection->fetchAllAssociative(
|
||||
'SELECT * FROM homework
|
||||
WHERE teacher_id = :teacher_id
|
||||
AND tenant_id = :tenant_id
|
||||
AND status != :deleted
|
||||
ORDER BY due_date ASC',
|
||||
[
|
||||
'teacher_id' => (string) $teacherId,
|
||||
'tenant_id' => (string) $tenantId,
|
||||
'deleted' => HomeworkStatus::DELETED->value,
|
||||
],
|
||||
);
|
||||
|
||||
return array_map($this->hydrate(...), $rows);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findByClass(ClassId $classId, TenantId $tenantId): array
|
||||
{
|
||||
$rows = $this->connection->fetchAllAssociative(
|
||||
'SELECT * FROM homework
|
||||
WHERE class_id = :class_id
|
||||
AND tenant_id = :tenant_id
|
||||
AND status != :deleted
|
||||
ORDER BY due_date ASC',
|
||||
[
|
||||
'class_id' => (string) $classId,
|
||||
'tenant_id' => (string) $tenantId,
|
||||
'deleted' => HomeworkStatus::DELETED->value,
|
||||
],
|
||||
);
|
||||
|
||||
return array_map($this->hydrate(...), $rows);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function delete(HomeworkId $id, TenantId $tenantId): void
|
||||
{
|
||||
$this->connection->executeStatement(
|
||||
'DELETE FROM homework WHERE id = :id AND tenant_id = :tenant_id',
|
||||
['id' => (string) $id, 'tenant_id' => (string) $tenantId],
|
||||
);
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $row */
|
||||
private function hydrate(array $row): Homework
|
||||
{
|
||||
/** @var string $id */
|
||||
$id = $row['id'];
|
||||
/** @var string $tenantId */
|
||||
$tenantId = $row['tenant_id'];
|
||||
/** @var string $classId */
|
||||
$classId = $row['class_id'];
|
||||
/** @var string $subjectId */
|
||||
$subjectId = $row['subject_id'];
|
||||
/** @var string $teacherId */
|
||||
$teacherId = $row['teacher_id'];
|
||||
/** @var string $title */
|
||||
$title = $row['title'];
|
||||
/** @var string|null $description */
|
||||
$description = $row['description'];
|
||||
/** @var string $dueDate */
|
||||
$dueDate = $row['due_date'];
|
||||
/** @var string $status */
|
||||
$status = $row['status'];
|
||||
/** @var string $createdAt */
|
||||
$createdAt = $row['created_at'];
|
||||
/** @var string $updatedAt */
|
||||
$updatedAt = $row['updated_at'];
|
||||
|
||||
return Homework::reconstitute(
|
||||
id: HomeworkId::fromString($id),
|
||||
tenantId: TenantId::fromString($tenantId),
|
||||
classId: ClassId::fromString($classId),
|
||||
subjectId: SubjectId::fromString($subjectId),
|
||||
teacherId: UserId::fromString($teacherId),
|
||||
title: $title,
|
||||
description: $description,
|
||||
dueDate: new DateTimeImmutable($dueDate),
|
||||
status: HomeworkStatus::from($status),
|
||||
createdAt: new DateTimeImmutable($createdAt),
|
||||
updatedAt: new DateTimeImmutable($updatedAt),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Persistence\InMemory;
|
||||
|
||||
use App\Administration\Domain\Model\SchoolClass\ClassId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Domain\Exception\HomeworkNotFoundException;
|
||||
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 function array_filter;
|
||||
use function array_values;
|
||||
|
||||
use Override;
|
||||
|
||||
final class InMemoryHomeworkRepository implements HomeworkRepository
|
||||
{
|
||||
/** @var array<string, Homework> */
|
||||
private array $byId = [];
|
||||
|
||||
#[Override]
|
||||
public function save(Homework $homework): void
|
||||
{
|
||||
$this->byId[(string) $homework->id] = $homework;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function get(HomeworkId $id, TenantId $tenantId): Homework
|
||||
{
|
||||
$homework = $this->findById($id, $tenantId);
|
||||
|
||||
if ($homework === null) {
|
||||
throw HomeworkNotFoundException::withId($id);
|
||||
}
|
||||
|
||||
return $homework;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findById(HomeworkId $id, TenantId $tenantId): ?Homework
|
||||
{
|
||||
$homework = $this->byId[(string) $id] ?? null;
|
||||
|
||||
if ($homework === null || !$homework->tenantId->equals($tenantId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $homework;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findByTeacher(UserId $teacherId, TenantId $tenantId): array
|
||||
{
|
||||
return array_values(array_filter(
|
||||
$this->byId,
|
||||
static fn (Homework $h): bool => $h->teacherId->equals($teacherId)
|
||||
&& $h->tenantId->equals($tenantId)
|
||||
&& $h->status !== HomeworkStatus::DELETED,
|
||||
));
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findByClass(ClassId $classId, TenantId $tenantId): array
|
||||
{
|
||||
return array_values(array_filter(
|
||||
$this->byId,
|
||||
static fn (Homework $h): bool => $h->classId->equals($classId)
|
||||
&& $h->tenantId->equals($tenantId)
|
||||
&& $h->status !== HomeworkStatus::DELETED,
|
||||
));
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function delete(HomeworkId $id, TenantId $tenantId): void
|
||||
{
|
||||
$key = (string) $id;
|
||||
|
||||
if (isset($this->byId[$key]) && $this->byId[$key]->tenantId->equals($tenantId)) {
|
||||
unset($this->byId[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Storage;
|
||||
|
||||
use App\Scolarite\Application\Port\FileStorage;
|
||||
|
||||
use function dirname;
|
||||
use function file_put_contents;
|
||||
use function is_dir;
|
||||
use function is_file;
|
||||
use function is_string;
|
||||
use function mkdir;
|
||||
|
||||
use Override;
|
||||
|
||||
use function unlink;
|
||||
|
||||
final readonly class LocalFileStorage implements FileStorage
|
||||
{
|
||||
public function __construct(
|
||||
private string $storagePath,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function upload(string $path, mixed $content, string $mimeType): string
|
||||
{
|
||||
$fullPath = $this->storagePath . '/' . $path;
|
||||
$dir = dirname($fullPath);
|
||||
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0o755, true);
|
||||
}
|
||||
|
||||
$data = is_string($content) ? $content : '';
|
||||
|
||||
file_put_contents($fullPath, $data);
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function delete(string $path): void
|
||||
{
|
||||
$fullPath = $this->storagePath . '/' . $path;
|
||||
|
||||
if (is_file($fullPath)) {
|
||||
unlink($fullPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
428
backend/tests/Functional/Scolarite/Api/HomeworkEndpointsTest.php
Normal file
428
backend/tests/Functional/Scolarite/Api/HomeworkEndpointsTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user