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:
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user