-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a complete test for AddUserCommand #480
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Tests\Command; | ||
|
||
use AppBundle\Command\AddUserCommand; | ||
use AppBundle\Entity\User; | ||
use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
class AddUserCommandTest extends KernelTestCase | ||
{ | ||
private $userData = [ | ||
'username' => 'chuck_norris', | ||
'password' => 'foobar', | ||
'email' => 'chuck@norris.com', | ||
'full-name' => 'Chuck Norris', | ||
]; | ||
|
||
/** | ||
* @dataProvider isAdminDataProvider | ||
* | ||
* This test provides all the arguments required by the command, so the | ||
* command runs non-interactively and it won't ask for any argument. | ||
*/ | ||
public function testCreateUserNonInteractive($isAdmin) | ||
{ | ||
$input = $this->userData; | ||
if ($isAdmin) { | ||
$input['--admin'] = 1; | ||
} | ||
$this->executeCommand($input); | ||
|
||
$this->assertUserCreated($isAdmin); | ||
} | ||
|
||
/** | ||
* @dataProvider isAdminDataProvider | ||
* | ||
* 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 | ||
*/ | ||
public function testCreateUserInteractive($isAdmin) | ||
{ | ||
$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 function isAdminDataProvider() | ||
{ | ||
yield [false]; | ||
yield [true]; | ||
} | ||
|
||
/** | ||
* This helper method checks that the user was correctly created and saved | ||
* it in the database. | ||
*/ | ||
private function assertUserCreated($isAdmin) | ||
{ | ||
$container = self::$kernel->getContainer(); | ||
|
||
/** @var User $user */ | ||
$user = $container->get('doctrine')->getRepository(User::class)->findOneByEmail($this->userData['email']); | ||
$this->assertNotNull($user); | ||
|
||
$this->assertSame($this->userData['full-name'], $user->getFullName()); | ||
$this->assertSame($this->userData['username'], $user->getUsername()); | ||
$this->assertTrue($container->get('security.password_encoder')->isPasswordValid($user, $this->userData['password'])); | ||
$this->assertSame($isAdmin ? ['ROLE_ADMIN'] : ['ROLE_USER'], $user->getRoles()); | ||
} | ||
|
||
/** | ||
* This helper method abstracts the boilerplate code needed to test the | ||
* execution of a command. When the command is executed non-interactively, | ||
* all its arguments are passed in $arguments. If some needed argument is | ||
* missing, the command will ask for it interactively. Use the $inputs | ||
* argument to define the answers to provide to the command. | ||
*/ | ||
private function executeCommand(array $arguments, array $inputs = []) | ||
{ | ||
self::bootKernel(); | ||
|
||
$command = new AddUserCommand(); | ||
$command->setApplication(new Application(self::$kernel)); | ||
|
||
$commandTester = new CommandTester($command); | ||
$commandTester->setInputs($inputs); | ||
$commandTester->execute($arguments); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@javiereguiluz
$this->userData
😉