Skip to content

Commit 78950c4

Browse files
committed
feature #480 Add a complete test for AddUserCommand (dmaicher, javiereguiluz)
This PR was merged into the master branch. Discussion ---------- Add a complete test for AddUserCommand This adds a simple test for `AddUserCommand` that creates a user non-interactively and interactively. Fixes #473. Commits ------- 774adca Improved the PHPdoc of some methods c1d23f9 Fixed a variable name 3e15d59 Refactored the code and added some help notes a08c746 add simple test for AddUserCommand
2 parents 24ceaab + 774adca commit 78950c4

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 Tests\Command;
13+
14+
use AppBundle\Command\AddUserCommand;
15+
use AppBundle\Entity\User;
16+
use Symfony\Bundle\FrameworkBundle\Console\Application;
17+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
18+
use Symfony\Component\Console\Tester\CommandTester;
19+
20+
class AddUserCommandTest extends KernelTestCase
21+
{
22+
private $userData = [
23+
'username' => 'chuck_norris',
24+
'password' => 'foobar',
25+
'email' => '[email protected]',
26+
'full-name' => 'Chuck Norris',
27+
];
28+
29+
/**
30+
* @dataProvider isAdminDataProvider
31+
*
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.
34+
*/
35+
public function testCreateUserNonInteractive($isAdmin)
36+
{
37+
$input = $this->userData;
38+
if ($isAdmin) {
39+
$input['--admin'] = 1;
40+
}
41+
$this->executeCommand($input);
42+
43+
$this->assertUserCreated($isAdmin);
44+
}
45+
46+
/**
47+
* @dataProvider isAdminDataProvider
48+
*
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
53+
*/
54+
public function testCreateUserInteractive($isAdmin)
55+
{
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+
64+
$this->assertUserCreated($isAdmin);
65+
}
66+
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+
*/
71+
public function isAdminDataProvider()
72+
{
73+
yield [false];
74+
yield [true];
75+
}
76+
77+
/**
78+
* This helper method checks that the user was correctly created and saved
79+
* 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.
98+
*
99+
* @param array $arguments All the arguments passed when executing the command
100+
* @param array $inputs The (optional) answers given to the command when it asks for the value of the missing arguments
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);
112+
}
113+
}

0 commit comments

Comments
 (0)