Skip to content

Commit 6fd129c

Browse files
Nek-javiereguiluz
authored andcommitted
Add a test for command ListUsers (test emails)
1 parent 7ed6c4a commit 6fd129c

File tree

3 files changed

+110
-28
lines changed

3 files changed

+110
-28
lines changed

tests/Command/AbstractCommandTest.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace App\Tests\Command;
13+
14+
use Symfony\Bundle\FrameworkBundle\Console\Application;
15+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
16+
use Symfony\Component\Console\Tester\CommandTester;
17+
18+
abstract class AbstractCommandTest extends KernelTestCase
19+
{
20+
protected function setUp(): void
21+
{
22+
if ('Windows' === \PHP_OS_FAMILY) {
23+
$this->markTestSkipped('`stty` is required to test this command.');
24+
}
25+
}
26+
27+
/**
28+
* This helper method abstracts the boilerplate code needed to test the
29+
* execution of a command.
30+
*
31+
* @param array $arguments All the arguments passed when executing the command
32+
* @param array $inputs The (optional) answers given to the command when it asks for the value of the missing arguments
33+
*/
34+
protected function executeCommand(array $arguments, array $inputs = []): CommandTester
35+
{
36+
self::bootKernel();
37+
38+
// this uses a special testing container that allows you to fetch private services
39+
$command = self::$container->get($this->getCommandFqcn());
40+
$command->setApplication(new Application(self::$kernel));
41+
42+
$commandTester = new CommandTester($command);
43+
$commandTester->setInputs($inputs);
44+
$commandTester->execute($arguments);
45+
46+
return $commandTester;
47+
}
48+
49+
abstract protected function getCommandFqcn(): string;
50+
}

tests/Command/AddUserCommandTest.php

+3-28
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313

1414
use App\Command\AddUserCommand;
1515
use App\Repository\UserRepository;
16-
use Symfony\Bundle\FrameworkBundle\Console\Application;
17-
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
18-
use Symfony\Component\Console\Tester\CommandTester;
1916

20-
class AddUserCommandTest extends KernelTestCase
17+
class AddUserCommandTest extends AbstractCommandTest
2118
{
2219
private $userData = [
2320
'username' => 'chuck_norris',
@@ -26,13 +23,6 @@ class AddUserCommandTest extends KernelTestCase
2623
'full-name' => 'Chuck Norris',
2724
];
2825

29-
protected function setUp(): void
30-
{
31-
if ('Windows' === \PHP_OS_FAMILY) {
32-
$this->markTestSkipped('Windows OS does not support testing this command.');
33-
}
34-
}
35-
3626
/**
3727
* @dataProvider isAdminDataProvider
3828
*
@@ -97,23 +87,8 @@ private function assertUserCreated(bool $isAdmin): void
9787
$this->assertSame($isAdmin ? ['ROLE_ADMIN'] : ['ROLE_USER'], $user->getRoles());
9888
}
9989

100-
/**
101-
* This helper method abstracts the boilerplate code needed to test the
102-
* execution of a command.
103-
*
104-
* @param array $arguments All the arguments passed when executing the command
105-
* @param array $inputs The (optional) answers given to the command when it asks for the value of the missing arguments
106-
*/
107-
private function executeCommand(array $arguments, array $inputs = []): void
90+
protected function getCommandFqcn(): string
10891
{
109-
self::bootKernel();
110-
111-
// this uses a special testing container that allows you to fetch private services
112-
$command = self::$container->get(AddUserCommand::class);
113-
$command->setApplication(new Application(self::$kernel));
114-
115-
$commandTester = new CommandTester($command);
116-
$commandTester->setInputs($inputs);
117-
$commandTester->execute($arguments);
92+
return AddUserCommand::class;
11893
}
11994
}
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace App\Tests\Command;
13+
14+
use App\Command\ListUsersCommand;
15+
16+
class ListUsersCommandTest extends AbstractCommandTest
17+
{
18+
/**
19+
* @dataProvider maxResultsProvider
20+
*
21+
* This test verifies the amount of data is right according to the given parameter max results.
22+
*/
23+
public function testListUsers(int $maxResults): void
24+
{
25+
$tester = $this->executeCommand(
26+
['--max-results' => $maxResults]
27+
);
28+
29+
$emptyDisplayLines = 5;
30+
$this->assertSame($emptyDisplayLines + $maxResults, mb_substr_count($tester->getDisplay(), "\n"));
31+
}
32+
33+
public function maxResultsProvider(): ?\Generator
34+
{
35+
yield [1];
36+
yield [2];
37+
}
38+
39+
public function testItSendsNoEmailByDefault(): void
40+
{
41+
$this->executeCommand([]);
42+
43+
$this->assertEmailCount(0);
44+
}
45+
46+
public function testItSendsAnEmailIfOptionProvided(): void
47+
{
48+
$this->executeCommand(['--send-to' => '[email protected]']);
49+
50+
$this->assertEmailCount(1);
51+
}
52+
53+
protected function getCommandFqcn(): string
54+
{
55+
return ListUsersCommand::class;
56+
}
57+
}

0 commit comments

Comments
 (0)