userRepository = new InMemoryUserRepository(); $clock = new class implements Clock { public function now(): DateTimeImmutable { return new DateTimeImmutable('2026-02-08 10:00:00'); } }; $this->handler = new AssignRoleHandler($this->userRepository, $clock); } #[Test] public function itAssignsRoleToUser(): void { $user = $this->createUser(Role::PROF); $this->userRepository->save($user); $result = ($this->handler)(new AssignRoleCommand( userId: (string) $user->id, role: Role::PARENT->value, )); self::assertTrue($result->aLeRole(Role::PROF)); self::assertTrue($result->aLeRole(Role::PARENT)); } #[Test] public function itRecordsRoleAttribueEvent(): void { $user = $this->createUser(Role::PROF); $this->userRepository->save($user); $user->pullDomainEvents(); ($this->handler)(new AssignRoleCommand( userId: (string) $user->id, role: Role::PARENT->value, )); $savedUser = $this->userRepository->get($user->id); $events = $savedUser->pullDomainEvents(); self::assertCount(1, $events); self::assertInstanceOf(RoleAttribue::class, $events[0]); } #[Test] public function itThrowsWhenRoleAlreadyAssigned(): void { $user = $this->createUser(Role::PROF); $this->userRepository->save($user); $this->expectException(RoleDejaAttribueException::class); ($this->handler)(new AssignRoleCommand( userId: (string) $user->id, role: Role::PROF->value, )); } #[Test] public function itThrowsWhenRoleIsInvalid(): void { $user = $this->createUser(Role::PROF); $this->userRepository->save($user); $this->expectException(InvalidArgumentException::class); ($this->handler)(new AssignRoleCommand( userId: (string) $user->id, role: 'ROLE_INVALID', )); } private function createUser(Role $role): User { return User::creer( email: new Email('user@example.com'), role: $role, tenantId: TenantId::fromString(self::TENANT_ID), schoolName: 'École Alpha', dateNaissance: null, createdAt: new DateTimeImmutable('2026-01-15 10:00:00'), ); } }