Skip to content

Commit 3e15d59

Browse files
Refactored the code and added some help notes
1 parent a08c746 commit 3e15d59

File tree

1 file changed

+64
-46
lines changed

1 file changed

+64
-46
lines changed

tests/AppBundle/Command/AddUserCommandTest.php

+64-46
Original file line numberDiff line numberDiff line change
@@ -19,77 +19,95 @@
1919

2020
class AddUserCommandTest extends KernelTestCase
2121
{
22-
private function executeCommand(array $inputArgs, array $interactiveInputs = [])
23-
{
24-
self::bootKernel();
25-
26-
$command = new AddUserCommand();
27-
$command->setApplication(new Application(self::$kernel));
28-
29-
$commandTester = new CommandTester($command);
30-
$commandTester->setInputs($interactiveInputs);
31-
$commandTester->execute($inputArgs);
32-
}
33-
34-
/**
35-
* @param bool $isAdmin
36-
*/
37-
private function assertUserCreated($isAdmin)
38-
{
39-
$container = self::$kernel->getContainer();
40-
41-
/** @var User $user */
42-
$user = $container->get('doctrine')->getRepository(User::class)->findOneByEmail('[email protected]');
43-
$this->assertNotNull($user);
44-
45-
$this->assertSame('Chuck Norris', $user->getFullName());
46-
$this->assertSame('chuck_norris', $user->getUsername());
47-
$this->assertTrue($container->get('security.password_encoder')->isPasswordValid($user, 'foobar'));
48-
$this->assertSame($isAdmin ? ['ROLE_ADMIN'] : ['ROLE_USER'], $user->getRoles());
49-
}
22+
private $userData = [
23+
'username' => 'chuck_norris',
24+
'password' => 'foobar',
25+
'email' => '[email protected]',
26+
'full-name' => 'Chuck Norris',
27+
];
5028

5129
/**
5230
* @dataProvider isAdminDataProvider
5331
*
54-
* @param bool $isAdmin
32+
* This test provides all the arguments required by the command, so the
33+
* command runs non-interactively and it won't ask for any argument.
5534
*/
5635
public function testCreateUserNonInteractive($isAdmin)
5736
{
58-
$input = [
59-
'username' => 'chuck_norris',
60-
'password' => 'foobar',
61-
'email' => '[email protected]',
62-
'full-name' => 'Chuck Norris',
63-
];
64-
37+
$input = $this->userData;
6538
if ($isAdmin) {
6639
$input['--admin'] = 1;
6740
}
68-
6941
$this->executeCommand($input);
42+
7043
$this->assertUserCreated($isAdmin);
7144
}
7245

7346
/**
7447
* @dataProvider isAdminDataProvider
7548
*
76-
* @param bool $isAdmin
49+
* This test doesn't provide all the arguments required by the command, so
50+
* the command runs interactively and it will ask for the value of the missing
51+
* arguments.
52+
* See https://symfony.com/doc/current/components/console/helpers/questionhelper.html#testing-a-command-that-expects-input
7753
*/
7854
public function testCreateUserInteractive($isAdmin)
7955
{
80-
// see https://symfony.com/doc/current/components/console/helpers/questionhelper.html#testing-a-command-that-expects-input
81-
$this->executeCommand($isAdmin ? ['--admin' => 1] : [], [
82-
'chuck_norris',
83-
'foobar',
84-
85-
'Chuck Norris',
86-
]);
56+
$this->executeCommand(
57+
// these are the arguments (only 1 is passed, the rest are missing)
58+
$isAdmin ? ['--admin' => 1] : [],
59+
// these are the responses given to the questions asked by the command
60+
// to get the value of the missing required arguments
61+
array_values($this->userdata)
62+
);
63+
8764
$this->assertUserCreated($isAdmin);
8865
}
8966

67+
/**
68+
* This is used to execute the same test twice: first for normal users
69+
* (isAdmin = false) and then for admin users (isAdmin = true).
70+
*/
9071
public function isAdminDataProvider()
9172
{
92-
yield [true];
9373
yield [false];
74+
yield [true];
75+
}
76+
77+
/**
78+
* This helper method checks that the user was correctly created and saved
79+
* it in the database.
80+
*/
81+
private function assertUserCreated($isAdmin)
82+
{
83+
$container = self::$kernel->getContainer();
84+
85+
/** @var User $user */
86+
$user = $container->get('doctrine')->getRepository(User::class)->findOneByEmail($this->userData['email']);
87+
$this->assertNotNull($user);
88+
89+
$this->assertSame($this->userData['full-name'], $user->getFullName());
90+
$this->assertSame($this->userData['username'], $user->getUsername());
91+
$this->assertTrue($container->get('security.password_encoder')->isPasswordValid($user, $this->userData['password']));
92+
$this->assertSame($isAdmin ? ['ROLE_ADMIN'] : ['ROLE_USER'], $user->getRoles());
93+
}
94+
95+
/**
96+
* This helper method abstracts the boilerplate code needed to test the
97+
* execution of a command. When the command is executed non-interactively,
98+
* all its arguments are passed in $arguments. If some needed argument is
99+
* missing, the command will ask for it interactively. Use the $inputs
100+
* argument to define the answers to provide to the command.
101+
*/
102+
private function executeCommand(array $arguments, array $inputs = [])
103+
{
104+
self::bootKernel();
105+
106+
$command = new AddUserCommand();
107+
$command->setApplication(new Application(self::$kernel));
108+
109+
$commandTester = new CommandTester($command);
110+
$commandTester->setInputs($inputs);
111+
$commandTester->execute($arguments);
94112
}
95113
}

0 commit comments

Comments
 (0)