userRepository = new InMemoryUserRepository(); $clock = new class implements Clock { public function now(): DateTimeImmutable { return new DateTimeImmutable('2026-02-08 10:00:00'); } }; $this->activeRoleStore = $this->createMock(ActiveRoleStore::class); $this->handler = new UpdateUserRolesHandler($this->userRepository, $clock, $this->activeRoleStore); } #[Test] public function itUpdatesRolesBulk(): void { $user = $this->createUser(Role::PROF); $this->userRepository->save($user); $result = ($this->handler)(new UpdateUserRolesCommand( userId: (string) $user->id, roles: [Role::PARENT->value, Role::VIE_SCOLAIRE->value], )); self::assertFalse($result->aLeRole(Role::PROF)); self::assertTrue($result->aLeRole(Role::PARENT)); self::assertTrue($result->aLeRole(Role::VIE_SCOLAIRE)); } #[Test] public function itKeepsExistingRolesIfInTargetList(): void { $user = $this->createUser(Role::PROF); $user->attribuerRole(Role::PARENT, new DateTimeImmutable()); $this->userRepository->save($user); $result = ($this->handler)(new UpdateUserRolesCommand( userId: (string) $user->id, roles: [Role::PROF->value, Role::PARENT->value, Role::ADMIN->value], )); self::assertTrue($result->aLeRole(Role::PROF)); self::assertTrue($result->aLeRole(Role::PARENT)); self::assertTrue($result->aLeRole(Role::ADMIN)); self::assertCount(3, $result->roles); } #[Test] public function itThrowsWhenEmptyRoles(): void { $user = $this->createUser(Role::PROF); $this->userRepository->save($user); $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Au moins un rôle est requis.'); ($this->handler)(new UpdateUserRolesCommand( userId: (string) $user->id, roles: [], )); } #[Test] public function itThrowsWhenInvalidRole(): void { $user = $this->createUser(Role::PROF); $this->userRepository->save($user); $this->expectException(InvalidArgumentException::class); ($this->handler)(new UpdateUserRolesCommand( userId: (string) $user->id, roles: ['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'), ); } }