application = new Application($kernel); $container = self::$kernel->getContainer(); /** @var UserService $userService */ $userService = $container->get(UserService::class); $this->application->add(new DemoteUserCommand($userService)); } public function testCommandName(): void { $application = $this->application; $command = $application->find('kimai:user:demote'); self::assertInstanceOf(DemoteUserCommand::class, $command); } private function callCommand(?string $username, ?string $role, bool $super = false): CommandTester { $command = $this->application->find('kimai:user:demote'); $input = [ 'command' => $command->getName(), ]; if ($role !== null) { $input['role'] = $role; } if ($username !== null) { $input['username'] = $username; } if ($super) { $input['--super'] = true; } $commandTester = new CommandTester($command); $commandTester->execute($input); return $commandTester; } public function testDemoteRole(): void { $commandTester = $this->callCommand('tony_teamlead', 'ROLE_TEAMLEAD'); $output = $commandTester->getDisplay(); self::assertStringContainsString('[OK] Role "ROLE_TEAMLEAD" has been removed from user "tony_teamlead".', $output); $container = self::$kernel->getContainer(); /** @var Registry $doctrine */ $doctrine = $container->get('doctrine'); /** @var UserRepository $userRepository */ $userRepository = $doctrine->getRepository(User::class); $user = $userRepository->loadUserByIdentifier('tony_teamlead'); self::assertInstanceOf(User::class, $user); self::assertFalse($user->hasTeamleadRole()); } public function testDemoteSuper(): void { $commandTester = $this->callCommand('susan_super', null, true); $output = $commandTester->getDisplay(); self::assertStringContainsString('[OK] Super administrator role has been removed from the user "susan_super".', $output); $container = self::$kernel->getContainer(); /** @var Registry $doctrine */ $doctrine = $container->get('doctrine'); /** @var UserRepository $userRepository */ $userRepository = $doctrine->getRepository(User::class); $user = $userRepository->loadUserByIdentifier('susan_super'); self::assertInstanceOf(User::class, $user); self::assertFalse($user->isSuperAdmin()); } public function testDemoteSuperFailsOnTeamlead(): void { $commandTester = $this->callCommand('tony_teamlead', null, true); $output = $commandTester->getDisplay(); self::assertStringContainsString('[WARNING] User "tony_teamlead" doesn\'t have the super administrator role.', $output); } public function testDemoteAdminFailsOnTeamlead(): void { $commandTester = $this->callCommand('tony_teamlead', 'ROLE_ADMIN', false); $output = $commandTester->getDisplay(); self::assertStringContainsString('[WARNING] User "tony_teamlead" didn\'t have "ROLE_ADMIN" role.', $output); } public function testDemoteRoleAndSuperFails(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('You can pass either the role or the --super option (but not both simultaneously).'); $this->callCommand('john_user', 'ROLE_TEAMLEAD', true); } public function testWithMissingUsername(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Not enough arguments (missing: "username").'); $this->callCommand(null, null, true); } }