feat: Gestion des matières scolaires
Les établissements ont besoin de définir leur référentiel de matières pour pouvoir ensuite les associer aux enseignants et aux classes. Cette fonctionnalité permet aux administrateurs de créer, modifier et archiver les matières avec leurs propriétés (nom, code court, couleur). L'architecture suit le pattern DDD avec des Value Objects utilisant les property hooks PHP 8.5 pour garantir l'immutabilité et la validation. L'isolation multi-tenant est assurée par vérification dans les handlers.
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Application\Command\ArchiveSubject;
|
||||
|
||||
use App\Administration\Application\Command\ArchiveSubject\ArchiveSubjectCommand;
|
||||
use App\Administration\Application\Command\ArchiveSubject\ArchiveSubjectHandler;
|
||||
use App\Administration\Domain\Exception\SubjectNotFoundException;
|
||||
use App\Administration\Domain\Model\SchoolClass\SchoolId;
|
||||
use App\Administration\Domain\Model\Subject\Subject;
|
||||
use App\Administration\Domain\Model\Subject\SubjectCode;
|
||||
use App\Administration\Domain\Model\Subject\SubjectColor;
|
||||
use App\Administration\Domain\Model\Subject\SubjectName;
|
||||
use App\Administration\Domain\Model\Subject\SubjectStatus;
|
||||
use App\Administration\Infrastructure\Persistence\InMemory\InMemorySubjectRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ArchiveSubjectHandlerTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
private const string OTHER_TENANT_ID = '550e8400-e29b-41d4-a716-446655440099';
|
||||
private const string SCHOOL_ID = '550e8400-e29b-41d4-a716-446655440002';
|
||||
|
||||
private InMemorySubjectRepository $subjectRepository;
|
||||
private Clock $clock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->subjectRepository = new InMemorySubjectRepository();
|
||||
$this->clock = new class implements Clock {
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable('2026-02-01 10:00:00');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itArchivesSubjectSuccessfully(): void
|
||||
{
|
||||
$subject = $this->createAndSaveSubject();
|
||||
$handler = new ArchiveSubjectHandler($this->subjectRepository, $this->clock);
|
||||
|
||||
$command = new ArchiveSubjectCommand(
|
||||
subjectId: (string) $subject->id,
|
||||
tenantId: self::TENANT_ID,
|
||||
);
|
||||
|
||||
$archivedSubject = $handler($command);
|
||||
|
||||
self::assertSame(SubjectStatus::ARCHIVED, $archivedSubject->status);
|
||||
self::assertFalse($archivedSubject->estActive());
|
||||
self::assertNotNull($archivedSubject->deletedAt);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itPersistsArchivedSubjectInRepository(): void
|
||||
{
|
||||
$subject = $this->createAndSaveSubject();
|
||||
$handler = new ArchiveSubjectHandler($this->subjectRepository, $this->clock);
|
||||
|
||||
$command = new ArchiveSubjectCommand(
|
||||
subjectId: (string) $subject->id,
|
||||
tenantId: self::TENANT_ID,
|
||||
);
|
||||
|
||||
$handler($command);
|
||||
|
||||
// Retrieve from repository
|
||||
$retrievedSubject = $this->subjectRepository->get($subject->id);
|
||||
|
||||
self::assertSame(SubjectStatus::ARCHIVED, $retrievedSubject->status);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsExceptionWhenSubjectNotFound(): void
|
||||
{
|
||||
$handler = new ArchiveSubjectHandler($this->subjectRepository, $this->clock);
|
||||
|
||||
$this->expectException(SubjectNotFoundException::class);
|
||||
|
||||
$command = new ArchiveSubjectCommand(
|
||||
subjectId: '550e8400-e29b-41d4-a716-446655440099',
|
||||
tenantId: self::TENANT_ID,
|
||||
);
|
||||
$handler($command);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsExceptionWhenSubjectBelongsToDifferentTenant(): void
|
||||
{
|
||||
$subject = $this->createAndSaveSubject();
|
||||
$handler = new ArchiveSubjectHandler($this->subjectRepository, $this->clock);
|
||||
|
||||
$this->expectException(SubjectNotFoundException::class);
|
||||
|
||||
// Try to archive with a different tenant ID
|
||||
$command = new ArchiveSubjectCommand(
|
||||
subjectId: (string) $subject->id,
|
||||
tenantId: self::OTHER_TENANT_ID,
|
||||
);
|
||||
$handler($command);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itIsIdempotentWhenArchivingAlreadyArchivedSubject(): void
|
||||
{
|
||||
$subject = $this->createAndSaveSubject();
|
||||
$handler = new ArchiveSubjectHandler($this->subjectRepository, $this->clock);
|
||||
|
||||
$command = new ArchiveSubjectCommand(
|
||||
subjectId: (string) $subject->id,
|
||||
tenantId: self::TENANT_ID,
|
||||
);
|
||||
|
||||
// Archive twice
|
||||
$handler($command);
|
||||
$archivedAgain = $handler($command);
|
||||
|
||||
self::assertSame(SubjectStatus::ARCHIVED, $archivedAgain->status);
|
||||
}
|
||||
|
||||
private function createAndSaveSubject(): Subject
|
||||
{
|
||||
$subject = Subject::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
schoolId: SchoolId::fromString(self::SCHOOL_ID),
|
||||
name: new SubjectName('Mathématiques'),
|
||||
code: new SubjectCode('MATH'),
|
||||
color: new SubjectColor('#3B82F6'),
|
||||
createdAt: new DateTimeImmutable('2026-01-15 10:00:00'),
|
||||
);
|
||||
$this->subjectRepository->save($subject);
|
||||
|
||||
return $subject;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Application\Command\CreateSubject;
|
||||
|
||||
use App\Administration\Application\Command\CreateSubject\CreateSubjectCommand;
|
||||
use App\Administration\Application\Command\CreateSubject\CreateSubjectHandler;
|
||||
use App\Administration\Domain\Exception\SubjectDejaExistanteException;
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\Subject\SubjectStatus;
|
||||
use App\Administration\Infrastructure\Persistence\InMemory\InMemorySubjectRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class CreateSubjectHandlerTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
private const string SCHOOL_ID = '550e8400-e29b-41d4-a716-446655440002';
|
||||
|
||||
private InMemorySubjectRepository $subjectRepository;
|
||||
private Clock $clock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->subjectRepository = new InMemorySubjectRepository();
|
||||
$this->clock = new class implements Clock {
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable('2026-01-31 10:00:00');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itCreatesSubjectSuccessfully(): void
|
||||
{
|
||||
$handler = new CreateSubjectHandler($this->subjectRepository, $this->clock);
|
||||
$command = new CreateSubjectCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
schoolId: self::SCHOOL_ID,
|
||||
name: 'Mathématiques',
|
||||
code: 'MATH',
|
||||
color: '#3B82F6',
|
||||
);
|
||||
|
||||
$subject = $handler($command);
|
||||
|
||||
self::assertNotEmpty((string) $subject->id);
|
||||
self::assertSame('Mathématiques', (string) $subject->name);
|
||||
self::assertSame('MATH', (string) $subject->code);
|
||||
self::assertNotNull($subject->color);
|
||||
self::assertSame('#3B82F6', (string) $subject->color);
|
||||
self::assertSame(SubjectStatus::ACTIVE, $subject->status);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itCreatesSubjectWithNullColor(): void
|
||||
{
|
||||
$handler = new CreateSubjectHandler($this->subjectRepository, $this->clock);
|
||||
$command = new CreateSubjectCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
schoolId: self::SCHOOL_ID,
|
||||
name: 'Arts plastiques',
|
||||
code: 'ART',
|
||||
color: null,
|
||||
);
|
||||
|
||||
$subject = $handler($command);
|
||||
|
||||
self::assertNotEmpty((string) $subject->id);
|
||||
self::assertSame('Arts plastiques', (string) $subject->name);
|
||||
self::assertSame('ART', (string) $subject->code);
|
||||
self::assertNull($subject->color);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itPersistsSubjectInRepository(): void
|
||||
{
|
||||
$handler = new CreateSubjectHandler($this->subjectRepository, $this->clock);
|
||||
$command = new CreateSubjectCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
schoolId: self::SCHOOL_ID,
|
||||
name: 'Mathématiques',
|
||||
code: 'MATH',
|
||||
color: '#3B82F6',
|
||||
);
|
||||
|
||||
$createdSubject = $handler($command);
|
||||
|
||||
$subject = $this->subjectRepository->get(
|
||||
SubjectId::fromString((string) $createdSubject->id),
|
||||
);
|
||||
|
||||
self::assertSame('Mathématiques', (string) $subject->name);
|
||||
self::assertSame('MATH', (string) $subject->code);
|
||||
self::assertSame(SubjectStatus::ACTIVE, $subject->status);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsExceptionWhenSubjectCodeAlreadyExists(): void
|
||||
{
|
||||
$handler = new CreateSubjectHandler($this->subjectRepository, $this->clock);
|
||||
$command = new CreateSubjectCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
schoolId: self::SCHOOL_ID,
|
||||
name: 'Mathématiques',
|
||||
code: 'MATH',
|
||||
color: '#3B82F6',
|
||||
);
|
||||
|
||||
// First creation should succeed
|
||||
$handler($command);
|
||||
|
||||
// Second creation with same code should throw
|
||||
$this->expectException(SubjectDejaExistanteException::class);
|
||||
$this->expectExceptionMessage('Une matière avec le code "MATH" existe déjà dans cet établissement.');
|
||||
|
||||
$commandDuplicate = new CreateSubjectCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
schoolId: self::SCHOOL_ID,
|
||||
name: 'Maths avancées', // Different name, same code
|
||||
code: 'MATH',
|
||||
color: '#EF4444',
|
||||
);
|
||||
$handler($commandDuplicate);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itAllowsSameCodeInDifferentTenant(): void
|
||||
{
|
||||
$handler = new CreateSubjectHandler($this->subjectRepository, $this->clock);
|
||||
|
||||
// Create in tenant 1
|
||||
$command1 = new CreateSubjectCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
schoolId: self::SCHOOL_ID,
|
||||
name: 'Mathématiques',
|
||||
code: 'MATH',
|
||||
color: '#3B82F6',
|
||||
);
|
||||
$subject1 = $handler($command1);
|
||||
|
||||
// Create same code in tenant 2 should succeed
|
||||
$command2 = new CreateSubjectCommand(
|
||||
tenantId: '550e8400-e29b-41d4-a716-446655440099',
|
||||
schoolId: self::SCHOOL_ID,
|
||||
name: 'Mathématiques',
|
||||
code: 'MATH',
|
||||
color: '#3B82F6',
|
||||
);
|
||||
$subject2 = $handler($command2);
|
||||
|
||||
self::assertFalse($subject1->id->equals($subject2->id));
|
||||
self::assertSame('MATH', (string) $subject1->code);
|
||||
self::assertSame('MATH', (string) $subject2->code);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itAllowsSameCodeInDifferentSchool(): void
|
||||
{
|
||||
$handler = new CreateSubjectHandler($this->subjectRepository, $this->clock);
|
||||
|
||||
// Create in school 1
|
||||
$command1 = new CreateSubjectCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
schoolId: self::SCHOOL_ID,
|
||||
name: 'Mathématiques',
|
||||
code: 'MATH',
|
||||
color: '#3B82F6',
|
||||
);
|
||||
$subject1 = $handler($command1);
|
||||
|
||||
// Create same code in school 2 should succeed
|
||||
$command2 = new CreateSubjectCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
schoolId: '550e8400-e29b-41d4-a716-446655440099',
|
||||
name: 'Mathématiques',
|
||||
code: 'MATH',
|
||||
color: '#3B82F6',
|
||||
);
|
||||
$subject2 = $handler($command2);
|
||||
|
||||
self::assertFalse($subject1->id->equals($subject2->id));
|
||||
self::assertSame('MATH', (string) $subject1->code);
|
||||
self::assertSame('MATH', (string) $subject2->code);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itNormalizesCodeToUppercase(): void
|
||||
{
|
||||
$handler = new CreateSubjectHandler($this->subjectRepository, $this->clock);
|
||||
$command = new CreateSubjectCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
schoolId: self::SCHOOL_ID,
|
||||
name: 'Mathématiques',
|
||||
code: 'math',
|
||||
color: '#3B82F6',
|
||||
);
|
||||
|
||||
$subject = $handler($command);
|
||||
|
||||
self::assertSame('MATH', (string) $subject->code);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Application\Command\UpdateSubject;
|
||||
|
||||
use App\Administration\Application\Command\UpdateSubject\UpdateSubjectCommand;
|
||||
use App\Administration\Application\Command\UpdateSubject\UpdateSubjectHandler;
|
||||
use App\Administration\Domain\Exception\SubjectDejaExistanteException;
|
||||
use App\Administration\Domain\Exception\SubjectNotFoundException;
|
||||
use App\Administration\Domain\Model\SchoolClass\SchoolId;
|
||||
use App\Administration\Domain\Model\Subject\Subject;
|
||||
use App\Administration\Domain\Model\Subject\SubjectCode;
|
||||
use App\Administration\Domain\Model\Subject\SubjectColor;
|
||||
use App\Administration\Domain\Model\Subject\SubjectName;
|
||||
use App\Administration\Infrastructure\Persistence\InMemory\InMemorySubjectRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class UpdateSubjectHandlerTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
private const string OTHER_TENANT_ID = '550e8400-e29b-41d4-a716-446655440099';
|
||||
private const string SCHOOL_ID = '550e8400-e29b-41d4-a716-446655440002';
|
||||
|
||||
private InMemorySubjectRepository $subjectRepository;
|
||||
private Clock $clock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->subjectRepository = new InMemorySubjectRepository();
|
||||
$this->clock = new class implements Clock {
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable('2026-02-01 10:00:00');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itUpdatesSubjectName(): void
|
||||
{
|
||||
$subject = $this->createAndSaveSubject();
|
||||
$handler = new UpdateSubjectHandler($this->subjectRepository, $this->clock);
|
||||
|
||||
$command = new UpdateSubjectCommand(
|
||||
subjectId: (string) $subject->id,
|
||||
tenantId: self::TENANT_ID,
|
||||
schoolId: self::SCHOOL_ID,
|
||||
name: 'Mathématiques avancées',
|
||||
);
|
||||
|
||||
$updatedSubject = $handler($command);
|
||||
|
||||
self::assertSame('Mathématiques avancées', (string) $updatedSubject->name);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itUpdatesSubjectCode(): void
|
||||
{
|
||||
$subject = $this->createAndSaveSubject();
|
||||
$handler = new UpdateSubjectHandler($this->subjectRepository, $this->clock);
|
||||
|
||||
$command = new UpdateSubjectCommand(
|
||||
subjectId: (string) $subject->id,
|
||||
tenantId: self::TENANT_ID,
|
||||
schoolId: self::SCHOOL_ID,
|
||||
code: 'MATHS',
|
||||
);
|
||||
|
||||
$updatedSubject = $handler($command);
|
||||
|
||||
self::assertSame('MATHS', (string) $updatedSubject->code);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itUpdatesSubjectColor(): void
|
||||
{
|
||||
$subject = $this->createAndSaveSubject();
|
||||
$handler = new UpdateSubjectHandler($this->subjectRepository, $this->clock);
|
||||
|
||||
$command = new UpdateSubjectCommand(
|
||||
subjectId: (string) $subject->id,
|
||||
tenantId: self::TENANT_ID,
|
||||
schoolId: self::SCHOOL_ID,
|
||||
color: '#EF4444',
|
||||
);
|
||||
|
||||
$updatedSubject = $handler($command);
|
||||
|
||||
self::assertNotNull($updatedSubject->color);
|
||||
self::assertSame('#EF4444', (string) $updatedSubject->color);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itClearsSubjectColor(): void
|
||||
{
|
||||
$subject = $this->createAndSaveSubject();
|
||||
$handler = new UpdateSubjectHandler($this->subjectRepository, $this->clock);
|
||||
|
||||
$command = new UpdateSubjectCommand(
|
||||
subjectId: (string) $subject->id,
|
||||
tenantId: self::TENANT_ID,
|
||||
schoolId: self::SCHOOL_ID,
|
||||
clearColor: true,
|
||||
);
|
||||
|
||||
$updatedSubject = $handler($command);
|
||||
|
||||
self::assertNull($updatedSubject->color);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itUpdatesSubjectDescription(): void
|
||||
{
|
||||
$subject = $this->createAndSaveSubject();
|
||||
$handler = new UpdateSubjectHandler($this->subjectRepository, $this->clock);
|
||||
|
||||
$command = new UpdateSubjectCommand(
|
||||
subjectId: (string) $subject->id,
|
||||
tenantId: self::TENANT_ID,
|
||||
schoolId: self::SCHOOL_ID,
|
||||
description: 'Cours de mathématiques pour tous les niveaux',
|
||||
);
|
||||
|
||||
$updatedSubject = $handler($command);
|
||||
|
||||
self::assertSame('Cours de mathématiques pour tous les niveaux', $updatedSubject->description);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itClearsSubjectDescription(): void
|
||||
{
|
||||
$subject = $this->createAndSaveSubject();
|
||||
// First add a description
|
||||
$subject->decrire('Une description', new DateTimeImmutable());
|
||||
$this->subjectRepository->save($subject);
|
||||
|
||||
$handler = new UpdateSubjectHandler($this->subjectRepository, $this->clock);
|
||||
|
||||
$command = new UpdateSubjectCommand(
|
||||
subjectId: (string) $subject->id,
|
||||
tenantId: self::TENANT_ID,
|
||||
schoolId: self::SCHOOL_ID,
|
||||
clearDescription: true,
|
||||
);
|
||||
|
||||
$updatedSubject = $handler($command);
|
||||
|
||||
self::assertNull($updatedSubject->description);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsExceptionWhenChangingToExistingCode(): void
|
||||
{
|
||||
// Create first subject with code MATH
|
||||
$subject1 = $this->createAndSaveSubject();
|
||||
|
||||
// Create second subject with code FR
|
||||
$subject2 = Subject::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
schoolId: SchoolId::fromString(self::SCHOOL_ID),
|
||||
name: new SubjectName('Français'),
|
||||
code: new SubjectCode('FR'),
|
||||
color: new SubjectColor('#EF4444'),
|
||||
createdAt: new DateTimeImmutable('2026-01-15 10:00:00'),
|
||||
);
|
||||
$this->subjectRepository->save($subject2);
|
||||
|
||||
$handler = new UpdateSubjectHandler($this->subjectRepository, $this->clock);
|
||||
|
||||
// Try to change subject2's code to MATH (which already exists)
|
||||
$this->expectException(SubjectDejaExistanteException::class);
|
||||
|
||||
$command = new UpdateSubjectCommand(
|
||||
subjectId: (string) $subject2->id,
|
||||
tenantId: self::TENANT_ID,
|
||||
schoolId: self::SCHOOL_ID,
|
||||
code: 'MATH',
|
||||
);
|
||||
$handler($command);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itAllowsKeepingSameCode(): void
|
||||
{
|
||||
$subject = $this->createAndSaveSubject();
|
||||
$handler = new UpdateSubjectHandler($this->subjectRepository, $this->clock);
|
||||
|
||||
// Update name but keep same code
|
||||
$command = new UpdateSubjectCommand(
|
||||
subjectId: (string) $subject->id,
|
||||
tenantId: self::TENANT_ID,
|
||||
schoolId: self::SCHOOL_ID,
|
||||
name: 'Maths avancées',
|
||||
code: 'MATH', // Same code
|
||||
);
|
||||
|
||||
$updatedSubject = $handler($command);
|
||||
|
||||
self::assertSame('Maths avancées', (string) $updatedSubject->name);
|
||||
self::assertSame('MATH', (string) $updatedSubject->code);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itUpdatesMultipleFieldsAtOnce(): void
|
||||
{
|
||||
$subject = $this->createAndSaveSubject();
|
||||
$handler = new UpdateSubjectHandler($this->subjectRepository, $this->clock);
|
||||
|
||||
$command = new UpdateSubjectCommand(
|
||||
subjectId: (string) $subject->id,
|
||||
tenantId: self::TENANT_ID,
|
||||
schoolId: self::SCHOOL_ID,
|
||||
name: 'Maths avancées',
|
||||
color: '#10B981',
|
||||
description: 'Niveau supérieur',
|
||||
);
|
||||
|
||||
$updatedSubject = $handler($command);
|
||||
|
||||
self::assertSame('Maths avancées', (string) $updatedSubject->name);
|
||||
self::assertNotNull($updatedSubject->color);
|
||||
self::assertSame('#10B981', (string) $updatedSubject->color);
|
||||
self::assertSame('Niveau supérieur', $updatedSubject->description);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsExceptionWhenSubjectBelongsToDifferentTenant(): void
|
||||
{
|
||||
$subject = $this->createAndSaveSubject();
|
||||
$handler = new UpdateSubjectHandler($this->subjectRepository, $this->clock);
|
||||
|
||||
$this->expectException(SubjectNotFoundException::class);
|
||||
|
||||
// Try to update with a different tenant ID (tenant isolation violation)
|
||||
$command = new UpdateSubjectCommand(
|
||||
subjectId: (string) $subject->id,
|
||||
tenantId: self::OTHER_TENANT_ID,
|
||||
schoolId: self::SCHOOL_ID,
|
||||
name: 'Tentative de modification',
|
||||
);
|
||||
$handler($command);
|
||||
}
|
||||
|
||||
private function createAndSaveSubject(): Subject
|
||||
{
|
||||
$subject = Subject::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
schoolId: SchoolId::fromString(self::SCHOOL_ID),
|
||||
name: new SubjectName('Mathématiques'),
|
||||
code: new SubjectCode('MATH'),
|
||||
color: new SubjectColor('#3B82F6'),
|
||||
createdAt: new DateTimeImmutable('2026-01-15 10:00:00'),
|
||||
);
|
||||
$this->subjectRepository->save($subject);
|
||||
|
||||
return $subject;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user