security = $this->createMock(Security::class); } public function testSuperAdminMayEditOtherUsers(): void { $this->assertSame( VoterInterface::ACCESS_GRANTED, $this->vote($this->createUser(true), $this->createUser(false)), ); } public function testPlainAdminMayNotEdit(): void { $this->assertSame( VoterInterface::ACCESS_DENIED, $this->vote($this->createUser(false), $this->createUser(false)), ); } public function testSuperAdminMayNotEditThemselves(): void { $currentUser = $this->createUser(true); $this->assertSame( VoterInterface::ACCESS_DENIED, $this->vote($currentUser, $currentUser), ); } public function testImpersonatorMayNotEdit(): void { $this->assertSame( VoterInterface::ACCESS_DENIED, $this->vote($this->createUser(true), $this->createUser(false), true), ); } private function vote(User $currentUser, User $targetUser, bool $isImpersonator = false): int { $this->security ->method('isGranted') ->with('IS_IMPERSONATOR') ->willReturn($isImpersonator); $token = $this->createMock(TokenInterface::class); $token ->method('getUser') ->willReturn($currentUser); $voter = new UserVoter($this->security); return $voter->vote($token, $targetUser, [UserVoter::EDIT]); } private function createUser(bool $superAdmin): User { return (new User()) ->setRoles(['ROLE_ADMIN']) ->setSuperAdmin($superAdmin) ; } }