feat: Provisionner automatiquement un nouvel établissement
Lorsqu'un super-admin crée un établissement via l'interface, le système doit automatiquement créer la base tenant, exécuter les migrations, créer le premier utilisateur admin et envoyer l'invitation — le tout de manière asynchrone pour ne pas bloquer la réponse HTTP. Ce mécanisme rend chaque établissement opérationnel dès sa création sans intervention manuelle sur l'infrastructure.
This commit is contained in:
@@ -9,6 +9,8 @@ use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Application\Command\PublishGrades\PublishGradesCommand;
|
||||
use App\Scolarite\Application\Command\PublishGrades\PublishGradesHandler;
|
||||
use App\Scolarite\Application\Port\EnseignantAffectationChecker;
|
||||
use App\Scolarite\Application\Service\AutorisationSaisieNotesChecker;
|
||||
use App\Scolarite\Domain\Exception\AucuneNoteSaisieException;
|
||||
use App\Scolarite\Domain\Exception\NonProprietaireDeLEvaluationException;
|
||||
use App\Scolarite\Domain\Exception\NotesDejaPublieesException;
|
||||
@@ -22,6 +24,7 @@ use App\Scolarite\Domain\Model\Grade\GradeStatus;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeValue;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryGradeRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryTeacherReplacementRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
@@ -37,12 +40,17 @@ final class PublishGradesHandlerTest extends TestCase
|
||||
|
||||
private InMemoryEvaluationRepository $evaluationRepository;
|
||||
private InMemoryGradeRepository $gradeRepository;
|
||||
private InMemoryTeacherReplacementRepository $replacementRepository;
|
||||
private Clock $clock;
|
||||
|
||||
/** @var array<string, bool> */
|
||||
private array $affectationResults = [];
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->evaluationRepository = new InMemoryEvaluationRepository();
|
||||
$this->gradeRepository = new InMemoryGradeRepository();
|
||||
$this->replacementRepository = new InMemoryTeacherReplacementRepository();
|
||||
$this->clock = new class implements Clock {
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
@@ -50,9 +58,16 @@ final class PublishGradesHandlerTest extends TestCase
|
||||
}
|
||||
};
|
||||
|
||||
$this->affectationResults = [];
|
||||
$this->affectationResults[self::TEACHER_ID] = true;
|
||||
$this->seedEvaluation();
|
||||
}
|
||||
|
||||
public function isTeacherAffecte(string $teacherId): bool
|
||||
{
|
||||
return $this->affectationResults[$teacherId] ?? false;
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itPublishesGradesWhenGradesExist(): void
|
||||
{
|
||||
@@ -107,18 +122,18 @@ final class PublishGradesHandlerTest extends TestCase
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenTeacherNotOwner(): void
|
||||
public function itThrowsWhenTeacherNotAssigned(): void
|
||||
{
|
||||
$this->seedGrade();
|
||||
$handler = $this->createHandler();
|
||||
$otherTeacher = '550e8400-e29b-41d4-a716-446655440099';
|
||||
$unassignedTeacher = '550e8400-e29b-41d4-a716-446655440099';
|
||||
|
||||
$this->expectException(NonProprietaireDeLEvaluationException::class);
|
||||
|
||||
$handler(new PublishGradesCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
evaluationId: self::EVALUATION_ID,
|
||||
teacherId: $otherTeacher,
|
||||
teacherId: $unassignedTeacher,
|
||||
));
|
||||
}
|
||||
|
||||
@@ -145,10 +160,35 @@ final class PublishGradesHandlerTest extends TestCase
|
||||
return new PublishGradesHandler(
|
||||
$this->evaluationRepository,
|
||||
$this->gradeRepository,
|
||||
$this->createAutorisationChecker(),
|
||||
$this->clock,
|
||||
);
|
||||
}
|
||||
|
||||
private function createAutorisationChecker(): AutorisationSaisieNotesChecker
|
||||
{
|
||||
$test = $this;
|
||||
$affectationChecker = new class($test) implements EnseignantAffectationChecker {
|
||||
public function __construct(private readonly PublishGradesHandlerTest $test)
|
||||
{
|
||||
}
|
||||
|
||||
public function estAffecte(
|
||||
UserId $teacherId,
|
||||
ClassId $classId,
|
||||
SubjectId $subjectId,
|
||||
TenantId $tenantId,
|
||||
): bool {
|
||||
return $this->test->isTeacherAffecte((string) $teacherId);
|
||||
}
|
||||
};
|
||||
|
||||
return new AutorisationSaisieNotesChecker(
|
||||
$affectationChecker,
|
||||
$this->replacementRepository,
|
||||
);
|
||||
}
|
||||
|
||||
private function seedEvaluation(): void
|
||||
{
|
||||
$evaluation = Evaluation::reconstitute(
|
||||
|
||||
@@ -9,6 +9,8 @@ use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Application\Command\SaveAppreciation\SaveAppreciationCommand;
|
||||
use App\Scolarite\Application\Command\SaveAppreciation\SaveAppreciationHandler;
|
||||
use App\Scolarite\Application\Port\EnseignantAffectationChecker;
|
||||
use App\Scolarite\Application\Service\AutorisationSaisieNotesChecker;
|
||||
use App\Scolarite\Domain\Exception\AppreciationTropLongueException;
|
||||
use App\Scolarite\Domain\Exception\GradeNotFoundException;
|
||||
use App\Scolarite\Domain\Exception\NonProprietaireDeLEvaluationException;
|
||||
@@ -22,6 +24,7 @@ use App\Scolarite\Domain\Model\Grade\GradeStatus;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeValue;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryGradeRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryTeacherReplacementRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
@@ -39,13 +42,18 @@ final class SaveAppreciationHandlerTest extends TestCase
|
||||
|
||||
private InMemoryEvaluationRepository $evaluationRepository;
|
||||
private InMemoryGradeRepository $gradeRepository;
|
||||
private InMemoryTeacherReplacementRepository $replacementRepository;
|
||||
private Clock $clock;
|
||||
private string $gradeId;
|
||||
|
||||
/** @var array<string, bool> */
|
||||
private array $affectationResults = [];
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->evaluationRepository = new InMemoryEvaluationRepository();
|
||||
$this->gradeRepository = new InMemoryGradeRepository();
|
||||
$this->replacementRepository = new InMemoryTeacherReplacementRepository();
|
||||
$this->clock = new class implements Clock {
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
@@ -53,9 +61,16 @@ final class SaveAppreciationHandlerTest extends TestCase
|
||||
}
|
||||
};
|
||||
|
||||
$this->affectationResults = [];
|
||||
$this->affectationResults[self::TEACHER_ID] = true;
|
||||
$this->seedEvaluationAndGrade();
|
||||
}
|
||||
|
||||
public function isTeacherAffecte(string $teacherId): bool
|
||||
{
|
||||
return $this->affectationResults[$teacherId] ?? false;
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itSavesAppreciation(): void
|
||||
{
|
||||
@@ -144,10 +159,35 @@ final class SaveAppreciationHandlerTest extends TestCase
|
||||
return new SaveAppreciationHandler(
|
||||
$this->evaluationRepository,
|
||||
$this->gradeRepository,
|
||||
$this->createAutorisationChecker(),
|
||||
$this->clock,
|
||||
);
|
||||
}
|
||||
|
||||
private function createAutorisationChecker(): AutorisationSaisieNotesChecker
|
||||
{
|
||||
$test = $this;
|
||||
$affectationChecker = new class($test) implements EnseignantAffectationChecker {
|
||||
public function __construct(private readonly SaveAppreciationHandlerTest $test)
|
||||
{
|
||||
}
|
||||
|
||||
public function estAffecte(
|
||||
UserId $teacherId,
|
||||
ClassId $classId,
|
||||
SubjectId $subjectId,
|
||||
TenantId $tenantId,
|
||||
): bool {
|
||||
return $this->test->isTeacherAffecte((string) $teacherId);
|
||||
}
|
||||
};
|
||||
|
||||
return new AutorisationSaisieNotesChecker(
|
||||
$affectationChecker,
|
||||
$this->replacementRepository,
|
||||
);
|
||||
}
|
||||
|
||||
private function seedEvaluationAndGrade(): void
|
||||
{
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
|
||||
@@ -9,6 +9,8 @@ use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Application\Command\SaveGrades\SaveGradesCommand;
|
||||
use App\Scolarite\Application\Command\SaveGrades\SaveGradesHandler;
|
||||
use App\Scolarite\Application\Port\EnseignantAffectationChecker;
|
||||
use App\Scolarite\Application\Service\AutorisationSaisieNotesChecker;
|
||||
use App\Scolarite\Domain\Exception\NonProprietaireDeLEvaluationException;
|
||||
use App\Scolarite\Domain\Exception\NoteRequiseException;
|
||||
use App\Scolarite\Domain\Exception\ValeurNoteInvalideException;
|
||||
@@ -18,8 +20,11 @@ use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationStatus;
|
||||
use App\Scolarite\Domain\Model\Evaluation\GradeScale;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeStatus;
|
||||
use App\Scolarite\Domain\Model\TeacherReplacement\ClassSubjectPair;
|
||||
use App\Scolarite\Domain\Model\TeacherReplacement\TeacherReplacement;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryGradeRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryTeacherReplacementRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
@@ -36,12 +41,17 @@ final class SaveGradesHandlerTest extends TestCase
|
||||
|
||||
private InMemoryEvaluationRepository $evaluationRepository;
|
||||
private InMemoryGradeRepository $gradeRepository;
|
||||
private InMemoryTeacherReplacementRepository $replacementRepository;
|
||||
private Clock $clock;
|
||||
|
||||
/** @var array<string, bool> */
|
||||
private array $affectationResults = [];
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->evaluationRepository = new InMemoryEvaluationRepository();
|
||||
$this->gradeRepository = new InMemoryGradeRepository();
|
||||
$this->replacementRepository = new InMemoryTeacherReplacementRepository();
|
||||
$this->clock = new class implements Clock {
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
@@ -49,9 +59,16 @@ final class SaveGradesHandlerTest extends TestCase
|
||||
}
|
||||
};
|
||||
|
||||
$this->affectationResults = [];
|
||||
$this->setTeacherAffecte(self::TEACHER_ID);
|
||||
$this->seedEvaluation();
|
||||
}
|
||||
|
||||
private function setTeacherAffecte(string $teacherId): void
|
||||
{
|
||||
$this->affectationResults[$teacherId] = true;
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itSavesNewGrades(): void
|
||||
{
|
||||
@@ -169,24 +186,7 @@ final class SaveGradesHandlerTest extends TestCase
|
||||
self::assertNull($savedGrades[0]->value);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenTeacherNotOwner(): void
|
||||
{
|
||||
$handler = $this->createHandler();
|
||||
$otherTeacher = '550e8400-e29b-41d4-a716-446655440099';
|
||||
|
||||
$this->expectException(NonProprietaireDeLEvaluationException::class);
|
||||
|
||||
$handler(new SaveGradesCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
evaluationId: self::EVALUATION_ID,
|
||||
teacherId: $otherTeacher,
|
||||
grades: [
|
||||
['studentId' => self::STUDENT_1_ID, 'value' => 15.5, 'status' => 'graded'],
|
||||
],
|
||||
));
|
||||
}
|
||||
|
||||
/** @see itThrowsWhenTeacherNotAssigned - renamed, now checks assignment instead of ownership */
|
||||
#[Test]
|
||||
public function itThrowsWhenValueExceedsGradeScale(): void
|
||||
{
|
||||
@@ -221,15 +221,119 @@ final class SaveGradesHandlerTest extends TestCase
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenTeacherNotAssigned(): void
|
||||
{
|
||||
$handler = $this->createHandler();
|
||||
$unassignedTeacher = '550e8400-e29b-41d4-a716-446655440099';
|
||||
|
||||
$this->expectException(NonProprietaireDeLEvaluationException::class);
|
||||
|
||||
$handler(new SaveGradesCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
evaluationId: self::EVALUATION_ID,
|
||||
teacherId: $unassignedTeacher,
|
||||
grades: [
|
||||
['studentId' => self::STUDENT_1_ID, 'value' => 15.5, 'status' => 'graded'],
|
||||
],
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itAllowsReplacementTeacherToSave(): void
|
||||
{
|
||||
$replacementTeacherId = '550e8400-e29b-41d4-a716-446655440088';
|
||||
$now = $this->clock->now();
|
||||
|
||||
$replacement = TeacherReplacement::designer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
replacedTeacherId: UserId::fromString(self::TEACHER_ID),
|
||||
replacementTeacherId: UserId::fromString($replacementTeacherId),
|
||||
startDate: $now->modify('-1 day'),
|
||||
endDate: $now->modify('+7 days'),
|
||||
classes: [new ClassSubjectPair(
|
||||
ClassId::fromString('550e8400-e29b-41d4-a716-446655440020'),
|
||||
SubjectId::fromString('550e8400-e29b-41d4-a716-446655440030'),
|
||||
)],
|
||||
reason: 'Maladie',
|
||||
createdBy: UserId::generate(),
|
||||
now: $now->modify('-1 day'),
|
||||
);
|
||||
$this->replacementRepository->save($replacement);
|
||||
|
||||
$handler = $this->createHandler();
|
||||
$savedGrades = $handler(new SaveGradesCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
evaluationId: self::EVALUATION_ID,
|
||||
teacherId: $replacementTeacherId,
|
||||
grades: [
|
||||
['studentId' => self::STUDENT_1_ID, 'value' => 14.0, 'status' => 'graded'],
|
||||
],
|
||||
));
|
||||
|
||||
self::assertCount(1, $savedGrades);
|
||||
self::assertSame((string) UserId::fromString($replacementTeacherId), (string) $savedGrades[0]->createdBy);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itBlocksEvaluationOwnerWithRemovedAssignment(): void
|
||||
{
|
||||
// Teacher IS the evaluation owner but has no active assignment
|
||||
$this->affectationResults = []; // Remove all assignments
|
||||
|
||||
$handler = $this->createHandler();
|
||||
|
||||
$this->expectException(NonProprietaireDeLEvaluationException::class);
|
||||
|
||||
$handler(new SaveGradesCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
evaluationId: self::EVALUATION_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
grades: [
|
||||
['studentId' => self::STUDENT_1_ID, 'value' => 15.5, 'status' => 'graded'],
|
||||
],
|
||||
));
|
||||
}
|
||||
|
||||
private function createHandler(): SaveGradesHandler
|
||||
{
|
||||
return new SaveGradesHandler(
|
||||
$this->evaluationRepository,
|
||||
$this->gradeRepository,
|
||||
$this->createAutorisationChecker(),
|
||||
$this->clock,
|
||||
);
|
||||
}
|
||||
|
||||
private function createAutorisationChecker(): AutorisationSaisieNotesChecker
|
||||
{
|
||||
$test = $this;
|
||||
$affectationChecker = new class($test) implements EnseignantAffectationChecker {
|
||||
public function __construct(private readonly SaveGradesHandlerTest $test)
|
||||
{
|
||||
}
|
||||
|
||||
public function estAffecte(
|
||||
UserId $teacherId,
|
||||
ClassId $classId,
|
||||
SubjectId $subjectId,
|
||||
TenantId $tenantId,
|
||||
): bool {
|
||||
return $this->test->isTeacherAffecte((string) $teacherId);
|
||||
}
|
||||
};
|
||||
|
||||
return new AutorisationSaisieNotesChecker(
|
||||
$affectationChecker,
|
||||
$this->replacementRepository,
|
||||
);
|
||||
}
|
||||
|
||||
public function isTeacherAffecte(string $teacherId): bool
|
||||
{
|
||||
return $this->affectationResults[$teacherId] ?? false;
|
||||
}
|
||||
|
||||
private function seedEvaluation(): void
|
||||
{
|
||||
$evaluation = Evaluation::reconstitute(
|
||||
|
||||
@@ -112,6 +112,14 @@ final class UploadSubmissionAttachmentHandlerTest extends TestCase
|
||||
public function delete(string $path): void
|
||||
{
|
||||
}
|
||||
|
||||
public function readStream(string $path): mixed
|
||||
{
|
||||
/** @var resource $stream */
|
||||
$stream = fopen('php://memory', 'r+');
|
||||
|
||||
return $stream;
|
||||
}
|
||||
};
|
||||
|
||||
$clock = new class implements Clock {
|
||||
|
||||
Reference in New Issue
Block a user