|
19 | 19 |
|
20 | 20 | class AddUserCommandTest extends KernelTestCase
|
21 | 21 | {
|
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 | + |
| 26 | + 'full-name' => 'Chuck Norris', |
| 27 | + ]; |
50 | 28 |
|
51 | 29 | /**
|
52 | 30 | * @dataProvider isAdminDataProvider
|
53 | 31 | *
|
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. |
55 | 34 | */
|
56 | 35 | public function testCreateUserNonInteractive($isAdmin)
|
57 | 36 | {
|
58 |
| - $input = [ |
59 |
| - 'username' => 'chuck_norris', |
60 |
| - 'password' => 'foobar', |
61 |
| - |
62 |
| - 'full-name' => 'Chuck Norris', |
63 |
| - ]; |
64 |
| - |
| 37 | + $input = $this->userData; |
65 | 38 | if ($isAdmin) {
|
66 | 39 | $input['--admin'] = 1;
|
67 | 40 | }
|
68 |
| - |
69 | 41 | $this->executeCommand($input);
|
| 42 | + |
70 | 43 | $this->assertUserCreated($isAdmin);
|
71 | 44 | }
|
72 | 45 |
|
73 | 46 | /**
|
74 | 47 | * @dataProvider isAdminDataProvider
|
75 | 48 | *
|
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 |
77 | 53 | */
|
78 | 54 | public function testCreateUserInteractive($isAdmin)
|
79 | 55 | {
|
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 | + |
87 | 64 | $this->assertUserCreated($isAdmin);
|
88 | 65 | }
|
89 | 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 | + */ |
90 | 71 | public function isAdminDataProvider()
|
91 | 72 | {
|
92 |
| - yield [true]; |
93 | 73 | 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); |
94 | 112 | }
|
95 | 113 | }
|
0 commit comments