feat: Affectation des enseignants aux classes et matières
Permet aux administrateurs d'associer un enseignant à une classe pour une matière donnée au sein d'une année scolaire. Cette brique est nécessaire pour construire les emplois du temps et les carnets de notes par la suite. Le modèle impose l'unicité du triplet enseignant × classe × matière par année scolaire, avec réactivation automatique d'une affectation retirée plutôt que duplication. L'isolation multi-tenant est garantie au niveau du repository (findById/get filtrent par tenant_id).
This commit is contained in:
@@ -11,6 +11,7 @@ use App\Administration\Infrastructure\Api\Processor\CreateClassProcessor;
|
||||
use App\Administration\Infrastructure\Api\Resource\ClassResource;
|
||||
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryClassRepository;
|
||||
use App\Administration\Infrastructure\Security\ClassVoter;
|
||||
use App\Administration\Infrastructure\Service\CurrentAcademicYearResolver;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Infrastructure\Tenant\TenantConfig;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
@@ -154,11 +155,19 @@ final class CreateClassProcessorTest extends TestCase
|
||||
->with(ClassVoter::CREATE)
|
||||
->willReturn($authorized);
|
||||
|
||||
$resolvedTenantContext = $tenantContext ?? $this->tenantContext;
|
||||
|
||||
$academicYearResolver = new CurrentAcademicYearResolver(
|
||||
$resolvedTenantContext,
|
||||
$this->clock,
|
||||
);
|
||||
|
||||
return new CreateClassProcessor(
|
||||
$handler,
|
||||
$tenantContext ?? $this->tenantContext,
|
||||
$resolvedTenantContext,
|
||||
$eventBus,
|
||||
$authorizationChecker,
|
||||
$academicYearResolver,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,294 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Infrastructure\Api\Processor;
|
||||
|
||||
use ApiPlatform\Metadata\Post;
|
||||
use App\Administration\Application\Command\AssignTeacher\AssignTeacherHandler;
|
||||
use App\Administration\Domain\Model\SchoolClass\AcademicYearId;
|
||||
use App\Administration\Domain\Model\SchoolClass\ClassId;
|
||||
use App\Administration\Domain\Model\SchoolClass\ClassName;
|
||||
use App\Administration\Domain\Model\SchoolClass\ClassStatus;
|
||||
use App\Administration\Domain\Model\SchoolClass\SchoolClass;
|
||||
use App\Administration\Domain\Model\SchoolClass\SchoolId;
|
||||
use App\Administration\Domain\Model\SchoolClass\SchoolLevel;
|
||||
use App\Administration\Domain\Model\Subject\Subject;
|
||||
use App\Administration\Domain\Model\Subject\SubjectCode;
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\Subject\SubjectName;
|
||||
use App\Administration\Domain\Model\Subject\SubjectStatus;
|
||||
use App\Administration\Domain\Model\User\Email;
|
||||
use App\Administration\Domain\Model\User\Role;
|
||||
use App\Administration\Domain\Model\User\StatutCompte;
|
||||
use App\Administration\Domain\Model\User\User;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Administration\Infrastructure\Api\Processor\CreateTeacherAssignmentProcessor;
|
||||
use App\Administration\Infrastructure\Api\Resource\TeacherAssignmentResource;
|
||||
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryClassRepository;
|
||||
use App\Administration\Infrastructure\Persistence\InMemory\InMemorySubjectRepository;
|
||||
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryTeacherAssignmentRepository;
|
||||
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryUserRepository;
|
||||
use App\Administration\Infrastructure\Security\TeacherAssignmentVoter;
|
||||
use App\Administration\Infrastructure\Service\CurrentAcademicYearResolver;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use App\Shared\Infrastructure\Tenant\TenantConfig;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
use Symfony\Component\Messenger\Envelope;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
|
||||
|
||||
final class CreateTeacherAssignmentProcessorTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440002';
|
||||
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
|
||||
private const string CLASS_ID = '550e8400-e29b-41d4-a716-446655440020';
|
||||
private const string SUBJECT_ID = '550e8400-e29b-41d4-a716-446655440030';
|
||||
private const string ACADEMIC_YEAR_ID = '550e8400-e29b-41d4-a716-446655440040';
|
||||
private const string SCHOOL_ID = '550e8400-e29b-41d4-a716-446655440050';
|
||||
|
||||
private InMemoryTeacherAssignmentRepository $repository;
|
||||
private InMemoryUserRepository $userRepository;
|
||||
private InMemoryClassRepository $classRepository;
|
||||
private InMemorySubjectRepository $subjectRepository;
|
||||
private TenantContext $tenantContext;
|
||||
private Clock $clock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = new InMemoryTeacherAssignmentRepository();
|
||||
$this->userRepository = new InMemoryUserRepository();
|
||||
$this->classRepository = new InMemoryClassRepository();
|
||||
$this->subjectRepository = new InMemorySubjectRepository();
|
||||
$this->clock = new class implements Clock {
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable('2026-02-10 10:00:00');
|
||||
}
|
||||
};
|
||||
|
||||
$this->tenantContext = new TenantContext();
|
||||
$this->tenantContext->setCurrentTenant(new TenantConfig(
|
||||
tenantId: InfraTenantId::fromString(self::TENANT_ID),
|
||||
subdomain: 'ecole-alpha',
|
||||
databaseUrl: 'postgresql://test',
|
||||
));
|
||||
|
||||
$this->seedTestData();
|
||||
}
|
||||
|
||||
private function seedTestData(): void
|
||||
{
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
|
||||
$this->userRepository->save(User::reconstitute(
|
||||
id: UserId::fromString(self::TEACHER_ID),
|
||||
email: new Email('teacher@example.com'),
|
||||
roles: [Role::PROF],
|
||||
tenantId: $tenantId,
|
||||
schoolName: 'École Test',
|
||||
statut: StatutCompte::EN_ATTENTE,
|
||||
dateNaissance: null,
|
||||
createdAt: new DateTimeImmutable('2026-01-15'),
|
||||
hashedPassword: null,
|
||||
activatedAt: null,
|
||||
consentementParental: null,
|
||||
));
|
||||
|
||||
$this->classRepository->save(SchoolClass::reconstitute(
|
||||
id: ClassId::fromString(self::CLASS_ID),
|
||||
tenantId: $tenantId,
|
||||
schoolId: SchoolId::fromString(self::SCHOOL_ID),
|
||||
academicYearId: AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
|
||||
name: new ClassName('6ème A'),
|
||||
level: SchoolLevel::SIXIEME,
|
||||
capacity: 30,
|
||||
status: ClassStatus::ACTIVE,
|
||||
description: null,
|
||||
createdAt: new DateTimeImmutable('2026-01-15'),
|
||||
updatedAt: new DateTimeImmutable('2026-01-15'),
|
||||
deletedAt: null,
|
||||
));
|
||||
|
||||
$this->subjectRepository->save(Subject::reconstitute(
|
||||
id: SubjectId::fromString(self::SUBJECT_ID),
|
||||
tenantId: $tenantId,
|
||||
schoolId: SchoolId::fromString(self::SCHOOL_ID),
|
||||
name: new SubjectName('Mathématiques'),
|
||||
code: new SubjectCode('MATH'),
|
||||
color: null,
|
||||
status: SubjectStatus::ACTIVE,
|
||||
description: null,
|
||||
createdAt: new DateTimeImmutable('2026-01-15'),
|
||||
updatedAt: new DateTimeImmutable('2026-01-15'),
|
||||
deletedAt: null,
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function createsAssignmentSuccessfully(): void
|
||||
{
|
||||
$processor = $this->createProcessor();
|
||||
|
||||
$data = new TeacherAssignmentResource();
|
||||
$data->teacherId = self::TEACHER_ID;
|
||||
$data->classId = self::CLASS_ID;
|
||||
$data->subjectId = self::SUBJECT_ID;
|
||||
$data->academicYearId = self::ACADEMIC_YEAR_ID;
|
||||
|
||||
$result = $processor->process($data, new Post());
|
||||
|
||||
self::assertNotNull($result->id);
|
||||
self::assertSame(self::TEACHER_ID, $result->teacherId);
|
||||
self::assertSame(self::CLASS_ID, $result->classId);
|
||||
self::assertSame(self::SUBJECT_ID, $result->subjectId);
|
||||
self::assertSame('active', $result->status);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function resolvesCurrentAcademicYear(): void
|
||||
{
|
||||
$processor = $this->createProcessor();
|
||||
|
||||
$data = new TeacherAssignmentResource();
|
||||
$data->teacherId = self::TEACHER_ID;
|
||||
$data->classId = self::CLASS_ID;
|
||||
$data->subjectId = self::SUBJECT_ID;
|
||||
$data->academicYearId = 'current';
|
||||
|
||||
$result = $processor->process($data, new Post());
|
||||
|
||||
self::assertNotNull($result->id);
|
||||
self::assertNotNull($result->academicYearId);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function throwsWhenNotAuthorized(): void
|
||||
{
|
||||
$processor = $this->createProcessor(authorized: false);
|
||||
|
||||
$data = new TeacherAssignmentResource();
|
||||
$data->teacherId = self::TEACHER_ID;
|
||||
$data->classId = self::CLASS_ID;
|
||||
$data->subjectId = self::SUBJECT_ID;
|
||||
$data->academicYearId = self::ACADEMIC_YEAR_ID;
|
||||
|
||||
$this->expectException(AccessDeniedHttpException::class);
|
||||
|
||||
$processor->process($data, new Post());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function throwsWhenTenantNotSet(): void
|
||||
{
|
||||
$emptyTenantContext = new TenantContext();
|
||||
$processor = $this->createProcessor(tenantContext: $emptyTenantContext);
|
||||
|
||||
$data = new TeacherAssignmentResource();
|
||||
$data->teacherId = self::TEACHER_ID;
|
||||
$data->classId = self::CLASS_ID;
|
||||
$data->subjectId = self::SUBJECT_ID;
|
||||
$data->academicYearId = self::ACADEMIC_YEAR_ID;
|
||||
|
||||
$this->expectException(UnauthorizedHttpException::class);
|
||||
|
||||
$processor->process($data, new Post());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function throwsConflictWhenDuplicateAssignment(): void
|
||||
{
|
||||
$processor = $this->createProcessor();
|
||||
|
||||
$data = new TeacherAssignmentResource();
|
||||
$data->teacherId = self::TEACHER_ID;
|
||||
$data->classId = self::CLASS_ID;
|
||||
$data->subjectId = self::SUBJECT_ID;
|
||||
$data->academicYearId = self::ACADEMIC_YEAR_ID;
|
||||
|
||||
$processor->process($data, new Post());
|
||||
|
||||
$this->expectException(ConflictHttpException::class);
|
||||
|
||||
$processor->process($data, new Post());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function throwsBadRequestForInvalidUuid(): void
|
||||
{
|
||||
$processor = $this->createProcessor();
|
||||
|
||||
$data = new TeacherAssignmentResource();
|
||||
$data->teacherId = 'not-a-uuid';
|
||||
$data->classId = self::CLASS_ID;
|
||||
$data->subjectId = self::SUBJECT_ID;
|
||||
$data->academicYearId = self::ACADEMIC_YEAR_ID;
|
||||
|
||||
$this->expectException(BadRequestHttpException::class);
|
||||
|
||||
$processor->process($data, new Post());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function throwsBadRequestForInvalidAcademicYearId(): void
|
||||
{
|
||||
$processor = $this->createProcessor();
|
||||
|
||||
$data = new TeacherAssignmentResource();
|
||||
$data->teacherId = self::TEACHER_ID;
|
||||
$data->classId = self::CLASS_ID;
|
||||
$data->subjectId = self::SUBJECT_ID;
|
||||
$data->academicYearId = 'invalid-keyword';
|
||||
|
||||
$this->expectException(BadRequestHttpException::class);
|
||||
|
||||
$processor->process($data, new Post());
|
||||
}
|
||||
|
||||
private function createProcessor(
|
||||
bool $authorized = true,
|
||||
?TenantContext $tenantContext = null,
|
||||
): CreateTeacherAssignmentProcessor {
|
||||
$handler = new AssignTeacherHandler(
|
||||
$this->repository,
|
||||
$this->userRepository,
|
||||
$this->classRepository,
|
||||
$this->subjectRepository,
|
||||
$this->clock,
|
||||
);
|
||||
|
||||
$eventBus = $this->createMock(MessageBusInterface::class);
|
||||
$eventBus->method('dispatch')->willReturnCallback(
|
||||
static fn (object $message) => new Envelope($message),
|
||||
);
|
||||
|
||||
$authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
|
||||
$authorizationChecker->method('isGranted')
|
||||
->with(TeacherAssignmentVoter::CREATE)
|
||||
->willReturn($authorized);
|
||||
|
||||
$resolvedTenantContext = $tenantContext ?? $this->tenantContext;
|
||||
|
||||
$academicYearResolver = new CurrentAcademicYearResolver(
|
||||
$resolvedTenantContext,
|
||||
$this->clock,
|
||||
);
|
||||
|
||||
return new CreateTeacherAssignmentProcessor(
|
||||
$handler,
|
||||
$resolvedTenantContext,
|
||||
$eventBus,
|
||||
$authorizationChecker,
|
||||
$academicYearResolver,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Infrastructure\Api\Processor;
|
||||
|
||||
use ApiPlatform\Metadata\Delete;
|
||||
use App\Administration\Application\Command\RemoveAssignment\RemoveAssignmentHandler;
|
||||
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\TeacherAssignment\TeacherAssignment;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Administration\Infrastructure\Api\Processor\RemoveTeacherAssignmentProcessor;
|
||||
use App\Administration\Infrastructure\Api\Resource\TeacherAssignmentResource;
|
||||
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryTeacherAssignmentRepository;
|
||||
use App\Administration\Infrastructure\Security\TeacherAssignmentVoter;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use App\Shared\Infrastructure\Tenant\TenantConfig;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
use Symfony\Component\Messenger\Envelope;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
|
||||
|
||||
final class RemoveTeacherAssignmentProcessorTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440002';
|
||||
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
|
||||
private const string CLASS_ID = '550e8400-e29b-41d4-a716-446655440020';
|
||||
private const string SUBJECT_ID = '550e8400-e29b-41d4-a716-446655440030';
|
||||
private const string ACADEMIC_YEAR_ID = '550e8400-e29b-41d4-a716-446655440040';
|
||||
|
||||
private InMemoryTeacherAssignmentRepository $repository;
|
||||
private TenantContext $tenantContext;
|
||||
private Clock $clock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = new InMemoryTeacherAssignmentRepository();
|
||||
$this->clock = new class implements Clock {
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable('2026-02-10 10:00:00');
|
||||
}
|
||||
};
|
||||
|
||||
$this->tenantContext = new TenantContext();
|
||||
$this->tenantContext->setCurrentTenant(new TenantConfig(
|
||||
tenantId: InfraTenantId::fromString(self::TENANT_ID),
|
||||
subdomain: 'ecole-alpha',
|
||||
databaseUrl: 'postgresql://test',
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function removesAssignmentSuccessfully(): void
|
||||
{
|
||||
$assignment = $this->createAndSaveAssignment();
|
||||
$processor = $this->createProcessor();
|
||||
|
||||
$result = $processor->process(
|
||||
new TeacherAssignmentResource(),
|
||||
new Delete(),
|
||||
uriVariables: ['id' => (string) $assignment->id],
|
||||
);
|
||||
|
||||
self::assertNull($result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function throwsWhenNotAuthorized(): void
|
||||
{
|
||||
$assignment = $this->createAndSaveAssignment();
|
||||
$processor = $this->createProcessor(authorized: false);
|
||||
|
||||
$this->expectException(AccessDeniedHttpException::class);
|
||||
|
||||
$processor->process(
|
||||
new TeacherAssignmentResource(),
|
||||
new Delete(),
|
||||
uriVariables: ['id' => (string) $assignment->id],
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function throwsWhenTenantNotSet(): void
|
||||
{
|
||||
$assignment = $this->createAndSaveAssignment();
|
||||
$emptyTenantContext = new TenantContext();
|
||||
$processor = $this->createProcessor(tenantContext: $emptyTenantContext);
|
||||
|
||||
$this->expectException(UnauthorizedHttpException::class);
|
||||
|
||||
$processor->process(
|
||||
new TeacherAssignmentResource(),
|
||||
new Delete(),
|
||||
uriVariables: ['id' => (string) $assignment->id],
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function throwsNotFoundWhenAssignmentDoesNotExist(): void
|
||||
{
|
||||
$processor = $this->createProcessor();
|
||||
|
||||
$this->expectException(NotFoundHttpException::class);
|
||||
|
||||
$processor->process(
|
||||
new TeacherAssignmentResource(),
|
||||
new Delete(),
|
||||
uriVariables: ['id' => '550e8400-e29b-41d4-a716-446655440099'],
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function throwsNotFoundWhenNoIdProvided(): void
|
||||
{
|
||||
$processor = $this->createProcessor();
|
||||
|
||||
$this->expectException(NotFoundHttpException::class);
|
||||
|
||||
$processor->process(
|
||||
new TeacherAssignmentResource(),
|
||||
new Delete(),
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function throwsNotFoundForInvalidUuid(): void
|
||||
{
|
||||
$processor = $this->createProcessor();
|
||||
|
||||
$this->expectException(NotFoundHttpException::class);
|
||||
|
||||
$processor->process(
|
||||
new TeacherAssignmentResource(),
|
||||
new Delete(),
|
||||
uriVariables: ['id' => 'not-a-uuid'],
|
||||
);
|
||||
}
|
||||
|
||||
private function createAndSaveAssignment(): TeacherAssignment
|
||||
{
|
||||
$assignment = TeacherAssignment::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
classId: ClassId::fromString(self::CLASS_ID),
|
||||
subjectId: SubjectId::fromString(self::SUBJECT_ID),
|
||||
academicYearId: AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
|
||||
createdAt: new DateTimeImmutable('2026-02-10 10:00:00'),
|
||||
);
|
||||
|
||||
// Consommer les événements du domaine pour éviter les interférences
|
||||
$assignment->pullDomainEvents();
|
||||
|
||||
$this->repository->save($assignment);
|
||||
|
||||
return $assignment;
|
||||
}
|
||||
|
||||
private function createProcessor(
|
||||
bool $authorized = true,
|
||||
?TenantContext $tenantContext = null,
|
||||
): RemoveTeacherAssignmentProcessor {
|
||||
$handler = new RemoveAssignmentHandler($this->repository, $this->clock);
|
||||
|
||||
$eventBus = $this->createMock(MessageBusInterface::class);
|
||||
$eventBus->method('dispatch')->willReturnCallback(
|
||||
static fn (object $message) => new Envelope($message),
|
||||
);
|
||||
|
||||
$authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
|
||||
$authorizationChecker->method('isGranted')
|
||||
->with(TeacherAssignmentVoter::DELETE)
|
||||
->willReturn($authorized);
|
||||
|
||||
return new RemoveTeacherAssignmentProcessor(
|
||||
$handler,
|
||||
$tenantContext ?? $this->tenantContext,
|
||||
$eventBus,
|
||||
$authorizationChecker,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Infrastructure\Api\Provider;
|
||||
|
||||
use ApiPlatform\Metadata\Get;
|
||||
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\TeacherAssignment\TeacherAssignment;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Administration\Infrastructure\Api\Provider\TeacherAssignmentItemProvider;
|
||||
use App\Administration\Infrastructure\Api\Resource\TeacherAssignmentResource;
|
||||
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryTeacherAssignmentRepository;
|
||||
use App\Administration\Infrastructure\Security\TeacherAssignmentVoter;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use App\Shared\Infrastructure\Tenant\TenantConfig;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
|
||||
|
||||
final class TeacherAssignmentItemProviderTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440002';
|
||||
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
|
||||
private const string CLASS_ID = '550e8400-e29b-41d4-a716-446655440020';
|
||||
private const string SUBJECT_ID = '550e8400-e29b-41d4-a716-446655440030';
|
||||
private const string ACADEMIC_YEAR_ID = '550e8400-e29b-41d4-a716-446655440040';
|
||||
|
||||
private InMemoryTeacherAssignmentRepository $repository;
|
||||
private TenantContext $tenantContext;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = new InMemoryTeacherAssignmentRepository();
|
||||
|
||||
$this->tenantContext = new TenantContext();
|
||||
$this->tenantContext->setCurrentTenant(new TenantConfig(
|
||||
tenantId: InfraTenantId::fromString(self::TENANT_ID),
|
||||
subdomain: 'ecole-alpha',
|
||||
databaseUrl: 'postgresql://test',
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function returnsAssignmentById(): void
|
||||
{
|
||||
$assignment = $this->createAndSaveAssignment();
|
||||
$provider = $this->createProvider();
|
||||
|
||||
$result = $provider->provide(
|
||||
new Get(),
|
||||
['id' => (string) $assignment->id],
|
||||
);
|
||||
|
||||
self::assertInstanceOf(TeacherAssignmentResource::class, $result);
|
||||
self::assertSame((string) $assignment->id, $result->id);
|
||||
self::assertSame(self::TEACHER_ID, $result->teacherId);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function returnsNullForUnknownId(): void
|
||||
{
|
||||
$provider = $this->createProvider();
|
||||
|
||||
$result = $provider->provide(
|
||||
new Get(),
|
||||
['id' => '550e8400-e29b-41d4-a716-446655440099'],
|
||||
);
|
||||
|
||||
self::assertNull($result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function returnsNullForInvalidUuid(): void
|
||||
{
|
||||
$provider = $this->createProvider();
|
||||
|
||||
$result = $provider->provide(
|
||||
new Get(),
|
||||
['id' => 'not-a-uuid'],
|
||||
);
|
||||
|
||||
self::assertNull($result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function throwsUnauthorizedWhenNoTenant(): void
|
||||
{
|
||||
$tenantContext = new TenantContext();
|
||||
$provider = $this->createProvider(tenantContext: $tenantContext);
|
||||
|
||||
$this->expectException(UnauthorizedHttpException::class);
|
||||
|
||||
$provider->provide(
|
||||
new Get(),
|
||||
['id' => '550e8400-e29b-41d4-a716-446655440099'],
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function throwsAccessDeniedWhenNotAuthorized(): void
|
||||
{
|
||||
$provider = $this->createProvider(authorized: false);
|
||||
|
||||
$this->expectException(AccessDeniedHttpException::class);
|
||||
|
||||
$provider->provide(
|
||||
new Get(),
|
||||
['id' => '550e8400-e29b-41d4-a716-446655440099'],
|
||||
);
|
||||
}
|
||||
|
||||
private function createAndSaveAssignment(): TeacherAssignment
|
||||
{
|
||||
$assignment = TeacherAssignment::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
classId: ClassId::fromString(self::CLASS_ID),
|
||||
subjectId: SubjectId::fromString(self::SUBJECT_ID),
|
||||
academicYearId: AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
|
||||
createdAt: new DateTimeImmutable('2026-02-10 10:00:00'),
|
||||
);
|
||||
|
||||
$this->repository->save($assignment);
|
||||
|
||||
return $assignment;
|
||||
}
|
||||
|
||||
private function createProvider(
|
||||
bool $authorized = true,
|
||||
?TenantContext $tenantContext = null,
|
||||
): TeacherAssignmentItemProvider {
|
||||
$authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
|
||||
$authorizationChecker->method('isGranted')
|
||||
->with(TeacherAssignmentVoter::DELETE)
|
||||
->willReturn($authorized);
|
||||
|
||||
return new TeacherAssignmentItemProvider(
|
||||
$this->repository,
|
||||
$tenantContext ?? $this->tenantContext,
|
||||
$authorizationChecker,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Infrastructure\Api\Provider;
|
||||
|
||||
use ApiPlatform\Metadata\GetCollection;
|
||||
use App\Administration\Application\Query\GetTeachersForClass\GetTeachersForClassHandler;
|
||||
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\TeacherAssignment\TeacherAssignment;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Administration\Infrastructure\Api\Provider\TeacherAssignmentsByClassProvider;
|
||||
use App\Administration\Infrastructure\Api\Resource\TeacherAssignmentResource;
|
||||
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryTeacherAssignmentRepository;
|
||||
use App\Administration\Infrastructure\Security\TeacherAssignmentVoter;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use App\Shared\Infrastructure\Tenant\TenantConfig;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
|
||||
|
||||
final class TeacherAssignmentsByClassProviderTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440002';
|
||||
private const string CLASS_ID = '550e8400-e29b-41d4-a716-446655440020';
|
||||
private const string ACADEMIC_YEAR_ID = '550e8400-e29b-41d4-a716-446655440040';
|
||||
|
||||
private InMemoryTeacherAssignmentRepository $repository;
|
||||
private TenantContext $tenantContext;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = new InMemoryTeacherAssignmentRepository();
|
||||
|
||||
$this->tenantContext = new TenantContext();
|
||||
$this->tenantContext->setCurrentTenant(new TenantConfig(
|
||||
tenantId: InfraTenantId::fromString(self::TENANT_ID),
|
||||
subdomain: 'ecole-alpha',
|
||||
databaseUrl: 'postgresql://test',
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function returnsTeachersForClass(): void
|
||||
{
|
||||
$this->createAndSaveAssignment('550e8400-e29b-41d4-a716-446655440010');
|
||||
$this->createAndSaveAssignment('550e8400-e29b-41d4-a716-446655440011');
|
||||
|
||||
$provider = $this->createProvider();
|
||||
|
||||
$results = $provider->provide(
|
||||
new GetCollection(),
|
||||
['classId' => self::CLASS_ID],
|
||||
);
|
||||
|
||||
self::assertCount(2, $results);
|
||||
self::assertContainsOnlyInstancesOf(TeacherAssignmentResource::class, $results);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function returnsEmptyArrayWhenNoTeachersAssigned(): void
|
||||
{
|
||||
$provider = $this->createProvider();
|
||||
|
||||
$results = $provider->provide(
|
||||
new GetCollection(),
|
||||
['classId' => self::CLASS_ID],
|
||||
);
|
||||
|
||||
self::assertSame([], $results);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function throwsBadRequestForInvalidUuid(): void
|
||||
{
|
||||
$provider = $this->createProvider();
|
||||
|
||||
$this->expectException(\Symfony\Component\HttpKernel\Exception\BadRequestHttpException::class);
|
||||
|
||||
$provider->provide(
|
||||
new GetCollection(),
|
||||
['classId' => 'not-a-uuid'],
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function throwsAccessDeniedWhenNotAuthorized(): void
|
||||
{
|
||||
$provider = $this->createProvider(authorized: false);
|
||||
|
||||
$this->expectException(AccessDeniedHttpException::class);
|
||||
|
||||
$provider->provide(
|
||||
new GetCollection(),
|
||||
['classId' => self::CLASS_ID],
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function throwsUnauthorizedWhenNoTenant(): void
|
||||
{
|
||||
$tenantContext = new TenantContext();
|
||||
$provider = $this->createProvider(tenantContext: $tenantContext);
|
||||
|
||||
$this->expectException(UnauthorizedHttpException::class);
|
||||
|
||||
$provider->provide(
|
||||
new GetCollection(),
|
||||
['classId' => self::CLASS_ID],
|
||||
);
|
||||
}
|
||||
|
||||
private function createAndSaveAssignment(string $teacherId): void
|
||||
{
|
||||
$assignment = TeacherAssignment::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
teacherId: UserId::fromString($teacherId),
|
||||
classId: ClassId::fromString(self::CLASS_ID),
|
||||
subjectId: SubjectId::fromString('550e8400-e29b-41d4-a716-446655440030'),
|
||||
academicYearId: AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
|
||||
createdAt: new DateTimeImmutable('2026-02-10 10:00:00'),
|
||||
);
|
||||
|
||||
$this->repository->save($assignment);
|
||||
}
|
||||
|
||||
private function createProvider(
|
||||
bool $authorized = true,
|
||||
?TenantContext $tenantContext = null,
|
||||
): TeacherAssignmentsByClassProvider {
|
||||
$handler = new GetTeachersForClassHandler($this->repository);
|
||||
|
||||
$authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
|
||||
$authorizationChecker->method('isGranted')
|
||||
->with(TeacherAssignmentVoter::VIEW)
|
||||
->willReturn($authorized);
|
||||
|
||||
return new TeacherAssignmentsByClassProvider(
|
||||
$handler,
|
||||
$tenantContext ?? $this->tenantContext,
|
||||
$authorizationChecker,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Infrastructure\Api\Provider;
|
||||
|
||||
use ApiPlatform\Metadata\GetCollection;
|
||||
use App\Administration\Application\Query\GetAssignmentsForTeacher\GetAssignmentsForTeacherHandler;
|
||||
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\TeacherAssignment\TeacherAssignment;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Administration\Infrastructure\Api\Provider\TeacherAssignmentsByTeacherProvider;
|
||||
use App\Administration\Infrastructure\Api\Resource\TeacherAssignmentResource;
|
||||
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryTeacherAssignmentRepository;
|
||||
use App\Administration\Infrastructure\Security\TeacherAssignmentVoter;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use App\Shared\Infrastructure\Tenant\TenantConfig;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
|
||||
|
||||
final class TeacherAssignmentsByTeacherProviderTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440002';
|
||||
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
|
||||
private const string ACADEMIC_YEAR_ID = '550e8400-e29b-41d4-a716-446655440040';
|
||||
|
||||
private InMemoryTeacherAssignmentRepository $repository;
|
||||
private TenantContext $tenantContext;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = new InMemoryTeacherAssignmentRepository();
|
||||
|
||||
$this->tenantContext = new TenantContext();
|
||||
$this->tenantContext->setCurrentTenant(new TenantConfig(
|
||||
tenantId: InfraTenantId::fromString(self::TENANT_ID),
|
||||
subdomain: 'ecole-alpha',
|
||||
databaseUrl: 'postgresql://test',
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function returnsAssignmentsForTeacher(): void
|
||||
{
|
||||
$this->createAndSaveAssignment('550e8400-e29b-41d4-a716-446655440020');
|
||||
$this->createAndSaveAssignment('550e8400-e29b-41d4-a716-446655440021');
|
||||
|
||||
$provider = $this->createProvider();
|
||||
|
||||
$results = $provider->provide(
|
||||
new GetCollection(),
|
||||
['teacherId' => self::TEACHER_ID],
|
||||
);
|
||||
|
||||
self::assertCount(2, $results);
|
||||
self::assertContainsOnlyInstancesOf(TeacherAssignmentResource::class, $results);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function returnsEmptyArrayWhenNoAssignments(): void
|
||||
{
|
||||
$provider = $this->createProvider();
|
||||
|
||||
$results = $provider->provide(
|
||||
new GetCollection(),
|
||||
['teacherId' => self::TEACHER_ID],
|
||||
);
|
||||
|
||||
self::assertSame([], $results);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function throwsBadRequestForInvalidUuid(): void
|
||||
{
|
||||
$provider = $this->createProvider();
|
||||
|
||||
$this->expectException(\Symfony\Component\HttpKernel\Exception\BadRequestHttpException::class);
|
||||
|
||||
$provider->provide(
|
||||
new GetCollection(),
|
||||
['teacherId' => 'not-a-uuid'],
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function throwsAccessDeniedWhenNotAuthorized(): void
|
||||
{
|
||||
$provider = $this->createProvider(authorized: false);
|
||||
|
||||
$this->expectException(AccessDeniedHttpException::class);
|
||||
|
||||
$provider->provide(
|
||||
new GetCollection(),
|
||||
['teacherId' => self::TEACHER_ID],
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function throwsUnauthorizedWhenNoTenant(): void
|
||||
{
|
||||
$tenantContext = new TenantContext();
|
||||
$provider = $this->createProvider(tenantContext: $tenantContext);
|
||||
|
||||
$this->expectException(UnauthorizedHttpException::class);
|
||||
|
||||
$provider->provide(
|
||||
new GetCollection(),
|
||||
['teacherId' => self::TEACHER_ID],
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function passesTeacherIdAsSubjectToVoter(): void
|
||||
{
|
||||
$authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
|
||||
$authorizationChecker->expects(self::once())
|
||||
->method('isGranted')
|
||||
->with(
|
||||
TeacherAssignmentVoter::VIEW,
|
||||
self::callback(static function (TeacherAssignmentResource $subject): bool {
|
||||
return $subject->teacherId === self::TEACHER_ID;
|
||||
}),
|
||||
)
|
||||
->willReturn(true);
|
||||
|
||||
$handler = new GetAssignmentsForTeacherHandler($this->repository);
|
||||
|
||||
$provider = new TeacherAssignmentsByTeacherProvider(
|
||||
$handler,
|
||||
$this->tenantContext,
|
||||
$authorizationChecker,
|
||||
);
|
||||
|
||||
$provider->provide(
|
||||
new GetCollection(),
|
||||
['teacherId' => self::TEACHER_ID],
|
||||
);
|
||||
}
|
||||
|
||||
private function createAndSaveAssignment(string $classId): void
|
||||
{
|
||||
$assignment = TeacherAssignment::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
classId: ClassId::fromString($classId),
|
||||
subjectId: SubjectId::fromString('550e8400-e29b-41d4-a716-446655440030'),
|
||||
academicYearId: AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
|
||||
createdAt: new DateTimeImmutable('2026-02-10 10:00:00'),
|
||||
);
|
||||
|
||||
$this->repository->save($assignment);
|
||||
}
|
||||
|
||||
private function createProvider(
|
||||
bool $authorized = true,
|
||||
?TenantContext $tenantContext = null,
|
||||
): TeacherAssignmentsByTeacherProvider {
|
||||
$handler = new GetAssignmentsForTeacherHandler($this->repository);
|
||||
|
||||
$authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
|
||||
$authorizationChecker->method('isGranted')
|
||||
->willReturn($authorized);
|
||||
|
||||
return new TeacherAssignmentsByTeacherProvider(
|
||||
$handler,
|
||||
$tenantContext ?? $this->tenantContext,
|
||||
$authorizationChecker,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Infrastructure\Api\Resource;
|
||||
|
||||
use App\Administration\Application\Query\GetAssignmentsForTeacher\TeacherAssignmentDto;
|
||||
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\TeacherAssignment\TeacherAssignment;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Administration\Infrastructure\Api\Resource\TeacherAssignmentResource;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class TeacherAssignmentResourceTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
|
||||
private const string CLASS_ID = '550e8400-e29b-41d4-a716-446655440020';
|
||||
private const string SUBJECT_ID = '550e8400-e29b-41d4-a716-446655440030';
|
||||
private const string ACADEMIC_YEAR_ID = '550e8400-e29b-41d4-a716-446655440040';
|
||||
|
||||
#[Test]
|
||||
public function fromDomainMapsCorrectly(): void
|
||||
{
|
||||
$createdAt = new DateTimeImmutable('2026-02-10 10:00:00');
|
||||
|
||||
$assignment = TeacherAssignment::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
classId: ClassId::fromString(self::CLASS_ID),
|
||||
subjectId: SubjectId::fromString(self::SUBJECT_ID),
|
||||
academicYearId: AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
|
||||
createdAt: $createdAt,
|
||||
);
|
||||
|
||||
$resource = TeacherAssignmentResource::fromDomain($assignment);
|
||||
|
||||
self::assertSame((string) $assignment->id, $resource->id);
|
||||
self::assertSame(self::TEACHER_ID, $resource->teacherId);
|
||||
self::assertSame(self::CLASS_ID, $resource->classId);
|
||||
self::assertSame(self::SUBJECT_ID, $resource->subjectId);
|
||||
self::assertSame(self::ACADEMIC_YEAR_ID, $resource->academicYearId);
|
||||
self::assertSame('active', $resource->status);
|
||||
self::assertEquals($createdAt, $resource->startDate);
|
||||
self::assertNull($resource->endDate);
|
||||
self::assertEquals($createdAt, $resource->createdAt);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function fromDomainMapsRemovedAssignment(): void
|
||||
{
|
||||
$createdAt = new DateTimeImmutable('2026-02-10 10:00:00');
|
||||
$removedAt = new DateTimeImmutable('2026-02-11 14:00:00');
|
||||
|
||||
$assignment = TeacherAssignment::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
classId: ClassId::fromString(self::CLASS_ID),
|
||||
subjectId: SubjectId::fromString(self::SUBJECT_ID),
|
||||
academicYearId: AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
|
||||
createdAt: $createdAt,
|
||||
);
|
||||
$assignment->retirer($removedAt);
|
||||
|
||||
$resource = TeacherAssignmentResource::fromDomain($assignment);
|
||||
|
||||
self::assertSame('removed', $resource->status);
|
||||
self::assertEquals($removedAt, $resource->endDate);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function fromDtoMapsCorrectly(): void
|
||||
{
|
||||
$createdAt = new DateTimeImmutable('2026-02-10 10:00:00');
|
||||
|
||||
$dto = new TeacherAssignmentDto(
|
||||
id: '550e8400-e29b-41d4-a716-446655440099',
|
||||
teacherId: self::TEACHER_ID,
|
||||
classId: self::CLASS_ID,
|
||||
subjectId: self::SUBJECT_ID,
|
||||
academicYearId: self::ACADEMIC_YEAR_ID,
|
||||
status: 'active',
|
||||
startDate: $createdAt,
|
||||
endDate: null,
|
||||
createdAt: $createdAt,
|
||||
);
|
||||
|
||||
$resource = TeacherAssignmentResource::fromDto($dto);
|
||||
|
||||
self::assertSame('550e8400-e29b-41d4-a716-446655440099', $resource->id);
|
||||
self::assertSame(self::TEACHER_ID, $resource->teacherId);
|
||||
self::assertSame(self::CLASS_ID, $resource->classId);
|
||||
self::assertSame(self::SUBJECT_ID, $resource->subjectId);
|
||||
self::assertSame(self::ACADEMIC_YEAR_ID, $resource->academicYearId);
|
||||
self::assertSame('active', $resource->status);
|
||||
self::assertEquals($createdAt, $resource->startDate);
|
||||
self::assertNull($resource->endDate);
|
||||
self::assertEquals($createdAt, $resource->createdAt);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,290 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Infrastructure\Persistence\InMemory;
|
||||
|
||||
use App\Administration\Domain\Exception\AffectationNotFoundException;
|
||||
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\TeacherAssignment\TeacherAssignment;
|
||||
use App\Administration\Domain\Model\TeacherAssignment\TeacherAssignmentId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryTeacherAssignmentRepository;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class InMemoryTeacherAssignmentRepositoryTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
|
||||
private const string CLASS_ID = '550e8400-e29b-41d4-a716-446655440020';
|
||||
private const string SUBJECT_ID = '550e8400-e29b-41d4-a716-446655440030';
|
||||
private const string ACADEMIC_YEAR_ID = '550e8400-e29b-41d4-a716-446655440040';
|
||||
|
||||
private InMemoryTeacherAssignmentRepository $repository;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = new InMemoryTeacherAssignmentRepository();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function saveAndGet(): void
|
||||
{
|
||||
$assignment = $this->createAssignment();
|
||||
|
||||
$this->repository->save($assignment);
|
||||
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$retrieved = $this->repository->get($assignment->id, $tenantId);
|
||||
self::assertTrue($assignment->id->equals($retrieved->id));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getThrowsForUnknownId(): void
|
||||
{
|
||||
$this->expectException(AffectationNotFoundException::class);
|
||||
|
||||
$this->repository->get(
|
||||
TeacherAssignmentId::generate(),
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getThrowsForDifferentTenant(): void
|
||||
{
|
||||
$assignment = $this->createAssignment();
|
||||
$this->repository->save($assignment);
|
||||
|
||||
$this->expectException(AffectationNotFoundException::class);
|
||||
|
||||
$this->repository->get(
|
||||
$assignment->id,
|
||||
TenantId::fromString('550e8400-e29b-41d4-a716-446655440099'),
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function findByIdReturnsNullForUnknownId(): void
|
||||
{
|
||||
$result = $this->repository->findById(
|
||||
TeacherAssignmentId::generate(),
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertNull($result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function findByIdReturnsNullForDifferentTenant(): void
|
||||
{
|
||||
$assignment = $this->createAssignment();
|
||||
$this->repository->save($assignment);
|
||||
|
||||
$found = $this->repository->findById(
|
||||
$assignment->id,
|
||||
TenantId::fromString('550e8400-e29b-41d4-a716-446655440099'),
|
||||
);
|
||||
|
||||
self::assertNull($found);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function findByIdReturnsAssignment(): void
|
||||
{
|
||||
$assignment = $this->createAssignment();
|
||||
$this->repository->save($assignment);
|
||||
|
||||
$found = $this->repository->findById(
|
||||
$assignment->id,
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertNotNull($found);
|
||||
self::assertTrue($assignment->id->equals($found->id));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function findByTeacherClassSubjectReturnsActiveAssignment(): void
|
||||
{
|
||||
$assignment = $this->createAssignment();
|
||||
$this->repository->save($assignment);
|
||||
|
||||
$found = $this->repository->findByTeacherClassSubject(
|
||||
UserId::fromString(self::TEACHER_ID),
|
||||
ClassId::fromString(self::CLASS_ID),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertNotNull($found);
|
||||
self::assertTrue($assignment->id->equals($found->id));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function findByTeacherClassSubjectReturnsNullForRemovedAssignment(): void
|
||||
{
|
||||
$assignment = $this->createAssignment();
|
||||
$assignment->retirer(new DateTimeImmutable('2026-02-11 10:00:00'));
|
||||
$this->repository->save($assignment);
|
||||
|
||||
$found = $this->repository->findByTeacherClassSubject(
|
||||
UserId::fromString(self::TEACHER_ID),
|
||||
ClassId::fromString(self::CLASS_ID),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertNull($found);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function findByTeacherClassSubjectReturnsNullForDifferentTenant(): void
|
||||
{
|
||||
$assignment = $this->createAssignment();
|
||||
$this->repository->save($assignment);
|
||||
|
||||
$found = $this->repository->findByTeacherClassSubject(
|
||||
UserId::fromString(self::TEACHER_ID),
|
||||
ClassId::fromString(self::CLASS_ID),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
|
||||
TenantId::fromString('550e8400-e29b-41d4-a716-446655440099'),
|
||||
);
|
||||
|
||||
self::assertNull($found);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function findRemovedByTeacherClassSubjectReturnsRemovedAssignment(): void
|
||||
{
|
||||
$assignment = $this->createAssignment();
|
||||
$assignment->retirer(new DateTimeImmutable('2026-02-11 10:00:00'));
|
||||
$this->repository->save($assignment);
|
||||
|
||||
$found = $this->repository->findRemovedByTeacherClassSubject(
|
||||
UserId::fromString(self::TEACHER_ID),
|
||||
ClassId::fromString(self::CLASS_ID),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertNotNull($found);
|
||||
self::assertTrue($assignment->id->equals($found->id));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function findRemovedByTeacherClassSubjectReturnsNullForActiveAssignment(): void
|
||||
{
|
||||
$assignment = $this->createAssignment();
|
||||
$this->repository->save($assignment);
|
||||
|
||||
$found = $this->repository->findRemovedByTeacherClassSubject(
|
||||
UserId::fromString(self::TEACHER_ID),
|
||||
ClassId::fromString(self::CLASS_ID),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertNull($found);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function findActiveByTeacher(): void
|
||||
{
|
||||
$assignment1 = $this->createAssignment();
|
||||
$assignment2 = $this->createAssignment(
|
||||
subjectId: '550e8400-e29b-41d4-a716-446655440031',
|
||||
);
|
||||
$removedAssignment = $this->createAssignment(
|
||||
subjectId: '550e8400-e29b-41d4-a716-446655440032',
|
||||
);
|
||||
$removedAssignment->retirer(new DateTimeImmutable('2026-02-11 10:00:00'));
|
||||
|
||||
$this->repository->save($assignment1);
|
||||
$this->repository->save($assignment2);
|
||||
$this->repository->save($removedAssignment);
|
||||
|
||||
$result = $this->repository->findActiveByTeacher(
|
||||
UserId::fromString(self::TEACHER_ID),
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertCount(2, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function findActiveByTeacherReturnsEmptyForDifferentTenant(): void
|
||||
{
|
||||
$assignment = $this->createAssignment();
|
||||
$this->repository->save($assignment);
|
||||
|
||||
$result = $this->repository->findActiveByTeacher(
|
||||
UserId::fromString(self::TEACHER_ID),
|
||||
TenantId::fromString('550e8400-e29b-41d4-a716-446655440099'),
|
||||
);
|
||||
|
||||
self::assertEmpty($result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function findActiveByClass(): void
|
||||
{
|
||||
$assignment1 = $this->createAssignment();
|
||||
$assignment2 = $this->createAssignment(
|
||||
teacherId: '550e8400-e29b-41d4-a716-446655440011',
|
||||
);
|
||||
$removedAssignment = $this->createAssignment(
|
||||
teacherId: '550e8400-e29b-41d4-a716-446655440012',
|
||||
);
|
||||
$removedAssignment->retirer(new DateTimeImmutable('2026-02-11 10:00:00'));
|
||||
|
||||
$this->repository->save($assignment1);
|
||||
$this->repository->save($assignment2);
|
||||
$this->repository->save($removedAssignment);
|
||||
|
||||
$result = $this->repository->findActiveByClass(
|
||||
ClassId::fromString(self::CLASS_ID),
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertCount(2, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function findActiveByClassReturnsEmptyForDifferentTenant(): void
|
||||
{
|
||||
$assignment = $this->createAssignment();
|
||||
$this->repository->save($assignment);
|
||||
|
||||
$result = $this->repository->findActiveByClass(
|
||||
ClassId::fromString(self::CLASS_ID),
|
||||
TenantId::fromString('550e8400-e29b-41d4-a716-446655440099'),
|
||||
);
|
||||
|
||||
self::assertEmpty($result);
|
||||
}
|
||||
|
||||
private function createAssignment(
|
||||
?string $teacherId = null,
|
||||
?string $subjectId = null,
|
||||
): TeacherAssignment {
|
||||
return TeacherAssignment::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
teacherId: UserId::fromString($teacherId ?? self::TEACHER_ID),
|
||||
classId: ClassId::fromString(self::CLASS_ID),
|
||||
subjectId: SubjectId::fromString($subjectId ?? self::SUBJECT_ID),
|
||||
academicYearId: AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
|
||||
createdAt: new DateTimeImmutable('2026-02-10 10:00:00'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,259 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Infrastructure\Security;
|
||||
|
||||
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\TeacherAssignment\TeacherAssignment;
|
||||
use App\Administration\Domain\Model\User\Role;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Administration\Infrastructure\Api\Resource\TeacherAssignmentResource;
|
||||
use App\Administration\Infrastructure\Security\SecurityUser;
|
||||
use App\Administration\Infrastructure\Security\TeacherAssignmentVoter;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
|
||||
final class TeacherAssignmentVoterTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440002';
|
||||
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
|
||||
|
||||
private TeacherAssignmentVoter $voter;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->voter = new TeacherAssignmentVoter();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itAbstainsForUnrelatedAttributes(): void
|
||||
{
|
||||
$token = $this->tokenWithSecurityUser(Role::ADMIN->value);
|
||||
|
||||
$result = $this->voter->vote($token, null, ['SOME_OTHER_ATTRIBUTE']);
|
||||
|
||||
self::assertSame(Voter::ACCESS_ABSTAIN, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itDeniesAccessToUnauthenticatedUsers(): void
|
||||
{
|
||||
$token = $this->createMock(TokenInterface::class);
|
||||
$token->method('getUser')->willReturn(null);
|
||||
|
||||
$result = $this->voter->vote($token, null, [TeacherAssignmentVoter::VIEW]);
|
||||
|
||||
self::assertSame(Voter::ACCESS_DENIED, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itDeniesAccessToNonSecurityUserInstances(): void
|
||||
{
|
||||
$user = $this->createMock(UserInterface::class);
|
||||
$user->method('getRoles')->willReturn([Role::ADMIN->value]);
|
||||
|
||||
$token = $this->createMock(TokenInterface::class);
|
||||
$token->method('getUser')->willReturn($user);
|
||||
|
||||
$result = $this->voter->vote($token, null, [TeacherAssignmentVoter::VIEW]);
|
||||
|
||||
self::assertSame(Voter::ACCESS_DENIED, $result);
|
||||
}
|
||||
|
||||
// --- VIEW ---
|
||||
|
||||
#[Test]
|
||||
#[DataProvider('viewAllowedRolesProvider')]
|
||||
public function itGrantsViewToStaffRoles(string $role): void
|
||||
{
|
||||
$token = $this->tokenWithSecurityUser($role);
|
||||
|
||||
$result = $this->voter->vote($token, null, [TeacherAssignmentVoter::VIEW]);
|
||||
|
||||
self::assertSame(Voter::ACCESS_GRANTED, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<string, array{string}>
|
||||
*/
|
||||
public static function viewAllowedRolesProvider(): iterable
|
||||
{
|
||||
yield 'SUPER_ADMIN' => [Role::SUPER_ADMIN->value];
|
||||
yield 'ADMIN' => [Role::ADMIN->value];
|
||||
yield 'VIE_SCOLAIRE' => [Role::VIE_SCOLAIRE->value];
|
||||
yield 'SECRETARIAT' => [Role::SECRETARIAT->value];
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itGrantsViewToTeacherForOwnResource(): void
|
||||
{
|
||||
$token = $this->tokenWithSecurityUser(Role::PROF->value, self::TEACHER_ID);
|
||||
|
||||
$resource = new TeacherAssignmentResource();
|
||||
$resource->teacherId = self::TEACHER_ID;
|
||||
|
||||
$result = $this->voter->vote($token, $resource, [TeacherAssignmentVoter::VIEW]);
|
||||
|
||||
self::assertSame(Voter::ACCESS_GRANTED, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itGrantsViewToTeacherForOwnDomainModel(): void
|
||||
{
|
||||
$token = $this->tokenWithSecurityUser(Role::PROF->value, self::TEACHER_ID);
|
||||
|
||||
$assignment = TeacherAssignment::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
classId: ClassId::fromString('550e8400-e29b-41d4-a716-446655440020'),
|
||||
subjectId: SubjectId::fromString('550e8400-e29b-41d4-a716-446655440030'),
|
||||
academicYearId: AcademicYearId::fromString('550e8400-e29b-41d4-a716-446655440040'),
|
||||
createdAt: new DateTimeImmutable('2026-02-10 10:00:00'),
|
||||
);
|
||||
|
||||
$result = $this->voter->vote($token, $assignment, [TeacherAssignmentVoter::VIEW]);
|
||||
|
||||
self::assertSame(Voter::ACCESS_GRANTED, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itDeniesViewToTeacherForOtherTeacherResource(): void
|
||||
{
|
||||
$token = $this->tokenWithSecurityUser(Role::PROF->value, self::TEACHER_ID);
|
||||
|
||||
$resource = new TeacherAssignmentResource();
|
||||
$resource->teacherId = '550e8400-e29b-41d4-a716-446655440099';
|
||||
|
||||
$result = $this->voter->vote($token, $resource, [TeacherAssignmentVoter::VIEW]);
|
||||
|
||||
self::assertSame(Voter::ACCESS_DENIED, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itDeniesViewToTeacherWithNoSubject(): void
|
||||
{
|
||||
$token = $this->tokenWithSecurityUser(Role::PROF->value, self::TEACHER_ID);
|
||||
|
||||
$result = $this->voter->vote($token, null, [TeacherAssignmentVoter::VIEW]);
|
||||
|
||||
self::assertSame(Voter::ACCESS_DENIED, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
#[DataProvider('viewDeniedRolesProvider')]
|
||||
public function itDeniesViewToNonStaffRoles(string $role): void
|
||||
{
|
||||
$token = $this->tokenWithSecurityUser($role);
|
||||
|
||||
$result = $this->voter->vote($token, null, [TeacherAssignmentVoter::VIEW]);
|
||||
|
||||
self::assertSame(Voter::ACCESS_DENIED, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<string, array{string}>
|
||||
*/
|
||||
public static function viewDeniedRolesProvider(): iterable
|
||||
{
|
||||
yield 'PARENT' => [Role::PARENT->value];
|
||||
yield 'ELEVE' => [Role::ELEVE->value];
|
||||
}
|
||||
|
||||
// --- CREATE ---
|
||||
|
||||
#[Test]
|
||||
#[DataProvider('adminRolesProvider')]
|
||||
public function itGrantsCreateToAdminRoles(string $role): void
|
||||
{
|
||||
$token = $this->tokenWithSecurityUser($role);
|
||||
|
||||
$result = $this->voter->vote($token, null, [TeacherAssignmentVoter::CREATE]);
|
||||
|
||||
self::assertSame(Voter::ACCESS_GRANTED, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
#[DataProvider('nonAdminRolesProvider')]
|
||||
public function itDeniesCreateToNonAdminRoles(string $role): void
|
||||
{
|
||||
$token = $this->tokenWithSecurityUser($role);
|
||||
|
||||
$result = $this->voter->vote($token, null, [TeacherAssignmentVoter::CREATE]);
|
||||
|
||||
self::assertSame(Voter::ACCESS_DENIED, $result);
|
||||
}
|
||||
|
||||
// --- DELETE ---
|
||||
|
||||
#[Test]
|
||||
#[DataProvider('adminRolesProvider')]
|
||||
public function itGrantsDeleteToAdminRoles(string $role): void
|
||||
{
|
||||
$token = $this->tokenWithSecurityUser($role);
|
||||
|
||||
$result = $this->voter->vote($token, null, [TeacherAssignmentVoter::DELETE]);
|
||||
|
||||
self::assertSame(Voter::ACCESS_GRANTED, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
#[DataProvider('nonAdminRolesProvider')]
|
||||
public function itDeniesDeleteToNonAdminRoles(string $role): void
|
||||
{
|
||||
$token = $this->tokenWithSecurityUser($role);
|
||||
|
||||
$result = $this->voter->vote($token, null, [TeacherAssignmentVoter::DELETE]);
|
||||
|
||||
self::assertSame(Voter::ACCESS_DENIED, $result);
|
||||
}
|
||||
|
||||
// --- Data Providers ---
|
||||
|
||||
/**
|
||||
* @return iterable<string, array{string}>
|
||||
*/
|
||||
public static function adminRolesProvider(): iterable
|
||||
{
|
||||
yield 'SUPER_ADMIN' => [Role::SUPER_ADMIN->value];
|
||||
yield 'ADMIN' => [Role::ADMIN->value];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<string, array{string}>
|
||||
*/
|
||||
public static function nonAdminRolesProvider(): iterable
|
||||
{
|
||||
yield 'PROF' => [Role::PROF->value];
|
||||
yield 'VIE_SCOLAIRE' => [Role::VIE_SCOLAIRE->value];
|
||||
yield 'SECRETARIAT' => [Role::SECRETARIAT->value];
|
||||
yield 'PARENT' => [Role::PARENT->value];
|
||||
yield 'ELEVE' => [Role::ELEVE->value];
|
||||
}
|
||||
|
||||
private function tokenWithSecurityUser(
|
||||
string $role,
|
||||
string $userId = '550e8400-e29b-41d4-a716-446655440001',
|
||||
): TokenInterface {
|
||||
$securityUser = new SecurityUser(
|
||||
UserId::fromString($userId),
|
||||
'test@example.com',
|
||||
'hashed_password',
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
[$role],
|
||||
);
|
||||
|
||||
$token = $this->createMock(TokenInterface::class);
|
||||
$token->method('getUser')->willReturn($securityUser);
|
||||
|
||||
return $token;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Infrastructure\Service;
|
||||
|
||||
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\TeacherAssignment\TeacherAssignment;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryTeacherAssignmentRepository;
|
||||
use App\Administration\Infrastructure\Service\RepositoryTeacherAssignmentChecker;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class RepositoryTeacherAssignmentCheckerTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
|
||||
private const string CLASS_ID = '550e8400-e29b-41d4-a716-446655440020';
|
||||
private const string SUBJECT_ID = '550e8400-e29b-41d4-a716-446655440030';
|
||||
private const string ACADEMIC_YEAR_ID = '550e8400-e29b-41d4-a716-446655440040';
|
||||
|
||||
private InMemoryTeacherAssignmentRepository $repository;
|
||||
private RepositoryTeacherAssignmentChecker $checker;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = new InMemoryTeacherAssignmentRepository();
|
||||
$this->checker = new RepositoryTeacherAssignmentChecker($this->repository);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function estAffecteReturnsTrueWhenActiveAssignmentExists(): void
|
||||
{
|
||||
$this->createAndSaveAssignment();
|
||||
|
||||
self::assertTrue($this->checker->estAffecte(
|
||||
UserId::fromString(self::TEACHER_ID),
|
||||
ClassId::fromString(self::CLASS_ID),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function estAffecteReturnsFalseWhenNoAssignment(): void
|
||||
{
|
||||
self::assertFalse($this->checker->estAffecte(
|
||||
UserId::fromString(self::TEACHER_ID),
|
||||
ClassId::fromString(self::CLASS_ID),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function estAffecteReturnsFalseWhenAssignmentRemoved(): void
|
||||
{
|
||||
$assignment = $this->createAndSaveAssignment();
|
||||
$assignment->retirer(new DateTimeImmutable('2026-03-01 10:00:00'));
|
||||
$this->repository->save($assignment);
|
||||
|
||||
self::assertFalse($this->checker->estAffecte(
|
||||
UserId::fromString(self::TEACHER_ID),
|
||||
ClassId::fromString(self::CLASS_ID),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function estAffecteReturnsFalseForDifferentSubject(): void
|
||||
{
|
||||
$this->createAndSaveAssignment();
|
||||
|
||||
self::assertFalse($this->checker->estAffecte(
|
||||
UserId::fromString(self::TEACHER_ID),
|
||||
ClassId::fromString(self::CLASS_ID),
|
||||
SubjectId::fromString('550e8400-e29b-41d4-a716-446655440099'),
|
||||
AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
));
|
||||
}
|
||||
|
||||
private function createAndSaveAssignment(): TeacherAssignment
|
||||
{
|
||||
$assignment = TeacherAssignment::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
classId: ClassId::fromString(self::CLASS_ID),
|
||||
subjectId: SubjectId::fromString(self::SUBJECT_ID),
|
||||
academicYearId: AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
|
||||
createdAt: new DateTimeImmutable('2026-02-12 10:00:00'),
|
||||
);
|
||||
|
||||
$this->repository->save($assignment);
|
||||
|
||||
return $assignment;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user