createUser(Role::PROF); self::assertSame([Role::PROF], $user->roles); } #[Test] public function attribuerRoleAddsRoleToUser(): void { $user = $this->createUser(Role::PROF); $user->pullDomainEvents(); $user->attribuerRole(Role::PARENT, new DateTimeImmutable('2026-02-08 10:00:00')); self::assertContains(Role::PROF, $user->roles); self::assertContains(Role::PARENT, $user->roles); self::assertCount(2, $user->roles); } #[Test] public function attribuerRoleRecordsRoleAttribueEvent(): void { $user = $this->createUser(Role::PROF); $user->pullDomainEvents(); $user->attribuerRole(Role::PARENT, new DateTimeImmutable('2026-02-08 10:00:00')); $events = $user->pullDomainEvents(); self::assertCount(1, $events); self::assertInstanceOf(RoleAttribue::class, $events[0]); } #[Test] public function attribuerRoleThrowsWhenRoleAlreadyAssigned(): void { $user = $this->createUser(Role::PROF); $this->expectException(RoleDejaAttribueException::class); $user->attribuerRole(Role::PROF, new DateTimeImmutable()); } #[Test] public function retirerRoleRemovesRoleFromUser(): void { $user = $this->createUser(Role::PROF); $user->attribuerRole(Role::PARENT, new DateTimeImmutable()); $user->pullDomainEvents(); $user->retirerRole(Role::PARENT, new DateTimeImmutable('2026-02-08 10:00:00')); self::assertSame([Role::PROF], $user->roles); } #[Test] public function retirerRoleRecordsRoleRetireEvent(): void { $user = $this->createUser(Role::PROF); $user->attribuerRole(Role::PARENT, new DateTimeImmutable()); $user->pullDomainEvents(); $user->retirerRole(Role::PARENT, new DateTimeImmutable('2026-02-08 10:00:00')); $events = $user->pullDomainEvents(); self::assertCount(1, $events); self::assertInstanceOf(RoleRetire::class, $events[0]); } #[Test] public function retirerRoleThrowsWhenRoleNotAssigned(): void { $user = $this->createUser(Role::PROF); $this->expectException(RoleNonAttribueException::class); $user->retirerRole(Role::PARENT, new DateTimeImmutable()); } #[Test] public function retirerRoleThrowsWhenLastRole(): void { $user = $this->createUser(Role::PROF); $this->expectException(DernierRoleNonRetirableException::class); $user->retirerRole(Role::PROF, new DateTimeImmutable()); } #[Test] public function userCanHaveMultipleRoles(): void { $user = $this->createUser(Role::PROF); $user->attribuerRole(Role::PARENT, new DateTimeImmutable()); $user->attribuerRole(Role::VIE_SCOLAIRE, new DateTimeImmutable()); self::assertCount(3, $user->roles); self::assertContains(Role::PROF, $user->roles); self::assertContains(Role::PARENT, $user->roles); self::assertContains(Role::VIE_SCOLAIRE, $user->roles); } #[Test] public function aLeRoleReturnsTrueForAssignedRole(): void { $user = $this->createUser(Role::PROF); self::assertTrue($user->aLeRole(Role::PROF)); self::assertFalse($user->aLeRole(Role::PARENT)); } #[Test] public function rolePrincipalReturnsFirstRole(): void { $user = $this->createUser(Role::PROF); self::assertSame(Role::PROF, $user->rolePrincipal()); } private function createUser(Role $role = Role::PARENT): User { return User::creer( email: new Email('user@example.com'), role: $role, tenantId: TenantId::fromString(self::TENANT_ID), schoolName: self::SCHOOL_NAME, dateNaissance: null, createdAt: new DateTimeImmutable('2026-01-15 10:00:00'), ); } }