Merge pull request #3301 from nclsHart/list-user-command

Add list users command
This commit is contained in:
Jérémy Benoist 2017-08-02 07:25:02 +02:00 committed by GitHub
commit 8b5bef48d5
3 changed files with 149 additions and 0 deletions

View file

@ -0,0 +1,61 @@
<?php
namespace Wallabag\CoreBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class ListUserCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('wallabag:user:list')
->setDescription('List all users')
->setHelp('This command list all existing users')
->addArgument('search', InputArgument::OPTIONAL, 'Filter list by given search term')
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Max number of displayed users', 100)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$users = $this->getContainer()->get('wallabag_user.user_repository')
->getQueryBuilderForSearch($input->getArgument('search'))
->setMaxResults($input->getOption('limit'))
->getQuery()
->getResult();
$nbUsers = $this->getContainer()->get('wallabag_user.user_repository')
->getSumUsers();
$rows = [];
foreach ($users as $user) {
$rows[] = [
$user->getUsername(),
$user->getEmail(),
$user->isEnabled() ? 'yes' : 'no',
$user->hasRole('ROLE_SUPER_ADMIN') || $user->hasRole('ROLE_ADMIN') ? 'yes' : 'no',
];
}
$io->table(['username', 'email', 'is enabled?', 'is admin?'], $rows);
$io->success(
sprintf(
'%s/%s%s user(s) displayed.',
count($users),
$nbUsers,
$input->getArgument('search') === null ? '' : ' (filtered)'
)
);
return 0;
}
}

View file

@ -55,6 +55,19 @@ class UserRepository extends EntityRepository
->getSingleScalarResult();
}
/**
* Count how many users are existing.
*
* @return int
*/
public function getSumUsers()
{
return $this->createQueryBuilder('u')
->select('count(u)')
->getQuery()
->getSingleScalarResult();
}
/**
* Retrieves users filtered with a search term.
*

View file

@ -0,0 +1,75 @@
<?php
namespace Tests\Wallabag\CoreBundle\Command;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
use Wallabag\CoreBundle\Command\ListUserCommand;
class ListUserCommandTest extends WallabagCoreTestCase
{
public function testRunListUserCommand()
{
$application = new Application($this->getClient()->getKernel());
$application->add(new ListUserCommand());
$command = $application->find('wallabag:user:list');
$tester = new CommandTester($command);
$tester->execute([
'command' => $command->getName(),
]);
$this->assertContains('3/3 user(s) displayed.', $tester->getDisplay());
}
public function testRunListUserCommandWithLimit()
{
$application = new Application($this->getClient()->getKernel());
$application->add(new ListUserCommand());
$command = $application->find('wallabag:user:list');
$tester = new CommandTester($command);
$tester->execute([
'command' => $command->getName(),
'--limit' => 2,
]);
$this->assertContains('2/3 user(s) displayed.', $tester->getDisplay());
}
public function testRunListUserCommandWithSearch()
{
$application = new Application($this->getClient()->getKernel());
$application->add(new ListUserCommand());
$command = $application->find('wallabag:user:list');
$tester = new CommandTester($command);
$tester->execute([
'command' => $command->getName(),
'search' => 'boss',
]);
$this->assertContains('1/3 (filtered) user(s) displayed.', $tester->getDisplay());
}
public function testRunListUserCommandWithSearchAndLimit()
{
$application = new Application($this->getClient()->getKernel());
$application->add(new ListUserCommand());
$command = $application->find('wallabag:user:list');
$tester = new CommandTester($command);
$tester->execute([
'command' => $command->getName(),
'search' => 'bo',
'--limit' => 1,
]);
$this->assertContains('1/3 (filtered) user(s) displayed.', $tester->getDisplay());
}
}