Skip to content

Commit 409725d

Browse files
committed
feat(test): add test for command list user
The command list user was not tested until now, and that's quite sad because it's one of the only part of the code that actually sends an email. Symfony has some tools for testing emails, let's use them! I also refactored a little bit command testing since there was identical code between test cases. (intantiating the command)
1 parent 1d809c2 commit 409725d

File tree

3 files changed

+120
-31
lines changed

3 files changed

+120
-31
lines changed

tests/Command/AbstractCommandTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
exec('stty 2>&1', $output, $exitcode);
23+
$isSttySupported = 0 === $exitcode;
24+
25+
if ('Windows' === PHP_OS_FAMILY || !$isSttySupported) {
26+
$this->markTestSkipped('`stty` is required to test this command.');
27+
}
28+
}
29+
30+
/**
31+
* This helper method abstracts the boilerplate code needed to test the
32+
* execution of a command.
33+
*
34+
* @param array $arguments All the arguments passed when executing the command
35+
* @param array $inputs The (optional) answers given to the command when it asks for the value of the missing arguments
36+
*/
37+
protected function executeCommand(array $arguments, array $inputs = []): CommandTester
38+
{
39+
self::bootKernel();
40+
41+
// this uses a special testing container that allows you to fetch private services
42+
$command = self::$container->get($this->getCommandFqcn());
43+
$command->setApplication(new Application(self::$kernel));
44+
45+
$commandTester = new CommandTester($command);
46+
$commandTester->setInputs($inputs);
47+
$commandTester->execute($arguments);
48+
49+
return $commandTester;
50+
}
51+
52+
abstract protected function getCommandFqcn(): string;
53+
}

tests/Command/AddUserCommandTest.php

Lines changed: 3 additions & 31 deletions
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,16 +23,6 @@ class AddUserCommandTest extends KernelTestCase
2623
'full-name' => 'Chuck Norris',
2724
];
2825

29-
protected function setUp(): void
30-
{
31-
exec('stty 2>&1', $output, $exitcode);
32-
$isSttySupported = 0 === $exitcode;
33-
34-
if ('Windows' === PHP_OS_FAMILY || !$isSttySupported) {
35-
$this->markTestSkipped('`stty` is required to test this command.');
36-
}
37-
}
38-
3926
/**
4027
* @dataProvider isAdminDataProvider
4128
*
@@ -102,23 +89,8 @@ private function assertUserCreated(bool $isAdmin): void
10289
$this->assertSame($isAdmin ? ['ROLE_ADMIN'] : ['ROLE_USER'], $user->getRoles());
10390
}
10491

105-
/**
106-
* This helper method abstracts the boilerplate code needed to test the
107-
* execution of a command.
108-
*
109-
* @param array $arguments All the arguments passed when executing the command
110-
* @param array $inputs The (optional) answers given to the command when it asks for the value of the missing arguments
111-
*/
112-
private function executeCommand(array $arguments, array $inputs = []): void
92+
protected function getCommandFqcn(): string
11393
{
114-
self::bootKernel();
115-
116-
// this uses a special testing container that allows you to fetch private services
117-
$command = self::$container->get(AddUserCommand::class);
118-
$command->setApplication(new Application(self::$kernel));
119-
120-
$commandTester = new CommandTester($command);
121-
$commandTester->setInputs($inputs);
122-
$commandTester->execute($arguments);
94+
return AddUserCommand::class;
12395
}
12496
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
/**
40+
* @dataProvider sendEmailProvider
41+
*
42+
* This test ensure that the command actually sends an email when the parameter is specified
43+
*/
44+
public function testItSendsAnEmail(bool $sendAndEmail): void
45+
{
46+
$this->executeCommand(
47+
// these are the arguments (only 1 is passed, the rest are missing)
48+
$sendAndEmail ? ['--send-to' => 'john.doe@symfony.com'] : []
49+
);
50+
51+
$this->assertEmailCount($sendAndEmail ? 1 : 0);
52+
}
53+
54+
public function sendEmailProvider(): ?\Generator
55+
{
56+
yield [true];
57+
yield [false];
58+
}
59+
60+
protected function getCommandFqcn(): string
61+
{
62+
return ListUsersCommand::class;
63+
}
64+
}

0 commit comments

Comments
 (0)