From af31cfed769538fcb7d283cb1fad80ac8d07b663 Mon Sep 17 00:00:00 2001 From: Nicolas Hart Date: Sun, 30 Jul 2017 22:04:29 +0200 Subject: [PATCH 1/2] Add list user command --- .../CoreBundle/Command/ListUserCommand.php | 43 +++++++++++++++++++ .../Command/ListUserCommandTest.php | 26 +++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/Wallabag/CoreBundle/Command/ListUserCommand.php create mode 100644 tests/Wallabag/CoreBundle/Command/ListUserCommandTest.php diff --git a/src/Wallabag/CoreBundle/Command/ListUserCommand.php b/src/Wallabag/CoreBundle/Command/ListUserCommand.php new file mode 100644 index 000000000..852e93b77 --- /dev/null +++ b/src/Wallabag/CoreBundle/Command/ListUserCommand.php @@ -0,0 +1,43 @@ +setName('wallabag:user:list') + ->setDescription('List all users') + ->setHelp('This command list all existing users') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $io = new SymfonyStyle($input, $output); + + $users = $this->getContainer()->get('wallabag_user.user_repository')->findAll(); + + $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 user(s) displayed.', count($users))); + + return 0; + } +} diff --git a/tests/Wallabag/CoreBundle/Command/ListUserCommandTest.php b/tests/Wallabag/CoreBundle/Command/ListUserCommandTest.php new file mode 100644 index 000000000..5e6442471 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Command/ListUserCommandTest.php @@ -0,0 +1,26 @@ +getClient()->getKernel()); + $application->add(new ListUserCommand()); + + $command = $application->find('wallabag:user:list'); + + $tester = new CommandTester($command); + $tester->execute([ + 'command' => $command->getName(), + ]); + + $this->assertContains('3 user(s) displayed.', $tester->getDisplay()); + } +} From f7a4b441361404b378c30b7788b3699c208537ad Mon Sep 17 00:00:00 2001 From: Nicolas Hart Date: Mon, 31 Jul 2017 23:20:41 +0200 Subject: [PATCH 2/2] add search argument and limit option to list users command --- .../CoreBundle/Command/ListUserCommand.php | 22 +++++++- .../UserBundle/Repository/UserRepository.php | 13 +++++ .../Command/ListUserCommandTest.php | 51 ++++++++++++++++++- 3 files changed, 83 insertions(+), 3 deletions(-) diff --git a/src/Wallabag/CoreBundle/Command/ListUserCommand.php b/src/Wallabag/CoreBundle/Command/ListUserCommand.php index 852e93b77..20660d187 100644 --- a/src/Wallabag/CoreBundle/Command/ListUserCommand.php +++ b/src/Wallabag/CoreBundle/Command/ListUserCommand.php @@ -3,7 +3,9 @@ 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; @@ -15,6 +17,8 @@ class ListUserCommand extends ContainerAwareCommand ->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) ; } @@ -22,7 +26,14 @@ class ListUserCommand extends ContainerAwareCommand { $io = new SymfonyStyle($input, $output); - $users = $this->getContainer()->get('wallabag_user.user_repository')->findAll(); + $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) { @@ -36,7 +47,14 @@ class ListUserCommand extends ContainerAwareCommand $io->table(['username', 'email', 'is enabled?', 'is admin?'], $rows); - $io->success(sprintf('%s user(s) displayed.', count($users))); + $io->success( + sprintf( + '%s/%s%s user(s) displayed.', + count($users), + $nbUsers, + $input->getArgument('search') === null ? '' : ' (filtered)' + ) + ); return 0; } diff --git a/src/Wallabag/UserBundle/Repository/UserRepository.php b/src/Wallabag/UserBundle/Repository/UserRepository.php index 75cbeef20..be693d3b1 100644 --- a/src/Wallabag/UserBundle/Repository/UserRepository.php +++ b/src/Wallabag/UserBundle/Repository/UserRepository.php @@ -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. * diff --git a/tests/Wallabag/CoreBundle/Command/ListUserCommandTest.php b/tests/Wallabag/CoreBundle/Command/ListUserCommandTest.php index 5e6442471..9068cf593 100644 --- a/tests/Wallabag/CoreBundle/Command/ListUserCommandTest.php +++ b/tests/Wallabag/CoreBundle/Command/ListUserCommandTest.php @@ -21,6 +21,55 @@ class ListUserCommandTest extends WallabagCoreTestCase 'command' => $command->getName(), ]); - $this->assertContains('3 user(s) displayed.', $tester->getDisplay()); + $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()); } }