]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
add search argument and limit option to list users command 3301/head
authorNicolas Hart <contact@nclshart.net>
Mon, 31 Jul 2017 21:20:41 +0000 (23:20 +0200)
committerNicolas Hart <contact@nclshart.net>
Mon, 31 Jul 2017 21:20:41 +0000 (23:20 +0200)
src/Wallabag/CoreBundle/Command/ListUserCommand.php
src/Wallabag/UserBundle/Repository/UserRepository.php
tests/Wallabag/CoreBundle/Command/ListUserCommandTest.php

index 852e93b772ae6255ad6652d9da4a6b9040feb1d6..20660d18742d674b5f0db6693abaa52ff8c3c995 100644 (file)
@@ -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;
     }
index 75cbeef20b28e6c02b445920dd89a5f88c2da10f..be693d3b1312f6e86aae660c3798ee2b1e5d6530 100644 (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.
      *
index 5e64424715bb6237227bbd6d04ef56b33a808d50..9068cf5935ef7b8a4e01ed5e8c55a152af5c544e 100644 (file)
@@ -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());
     }
 }