forked from symfony/demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddUserCommandTest.php
110 lines (95 loc) · 3.48 KB
/
AddUserCommandTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Command;
use App\Command\AddUserCommand;
use App\Repository\UserRepository;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
final class AddUserCommandTest extends AbstractCommandTestCase
{
/**
* @var string[]
*/
private array $userData = [
'username' => 'chuck_norris',
'password' => 'foobar',
'email' => '[email protected]',
'full-name' => 'Chuck Norris',
];
protected function setUp(): void
{
if ('Windows' === \PHP_OS_FAMILY) {
$this->markTestSkipped('`stty` is required to test this command.');
}
}
/**
* This test provides all the arguments required by the command, so the
* command runs non-interactively and it won't ask for any argument.
*/
#[DataProvider('isAdminDataProvider')]
public function testCreateUserNonInteractive(bool $isAdmin): void
{
$input = $this->userData;
if ($isAdmin) {
$input['--admin'] = 1;
}
$this->executeCommand($input);
$this->assertUserCreated($isAdmin);
}
/**
* This test doesn't provide all the arguments required by the command, so
* the command runs interactively and it will ask for the value of the missing
* arguments.
*
* @see https://symfony.com/doc/current/components/console/helpers/questionhelper.html#testing-a-command-that-expects-input
*/
#[DataProvider('isAdminDataProvider')]
public function testCreateUserInteractive(bool $isAdmin): void
{
$this->executeCommand(
// these are the arguments (only 1 is passed, the rest are missing)
$isAdmin ? ['--admin' => 1] : [],
// these are the responses given to the questions asked by the command
// to get the value of the missing required arguments
array_values($this->userData)
);
$this->assertUserCreated($isAdmin);
}
/**
* This is used to execute the same test twice: first for normal users
* (isAdmin = false) and then for admin users (isAdmin = true).
*/
public static function isAdminDataProvider(): \Generator
{
yield [false];
yield [true];
}
/**
* This helper method checks that the user was correctly created and saved
* in the database.
*/
private function assertUserCreated(bool $isAdmin): void
{
/** @var UserRepository $repository */
$repository = $this->getContainer()->get(UserRepository::class);
/** @var UserPasswordHasherInterface $passwordHasher */
$passwordHasher = $this->getContainer()->get(UserPasswordHasherInterface::class);
$user = $repository->findOneByEmail($this->userData['email']);
$this->assertNotNull($user);
$this->assertSame($this->userData['full-name'], $user->getFullName());
$this->assertSame($this->userData['username'], $user->getUsername());
$this->assertTrue($passwordHasher->isPasswordValid($user, $this->userData['password']));
$this->assertSame($isAdmin ? ['ROLE_ADMIN'] : ['ROLE_USER'], $user->getRoles());
}
protected function getCommandFqcn(): string
{
return AddUserCommand::class;
}
}