-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add a test for command ListUsers (test emails) #1133
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,50 @@ | ||
<?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 Symfony\Bundle\FrameworkBundle\Console\Application; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
abstract class AbstractCommandTest extends KernelTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
if ('Windows' === \PHP_OS_FAMILY) { | ||
$this->markTestSkipped('`stty` is required to test this command.'); | ||
} | ||
} | ||
|
||
/** | ||
* This helper method abstracts the boilerplate code needed to test the | ||
* execution of a command. | ||
* | ||
* @param array $arguments All the arguments passed when executing the command | ||
* @param array $inputs The (optional) answers given to the command when it asks for the value of the missing arguments | ||
*/ | ||
protected function executeCommand(array $arguments, array $inputs = []): CommandTester | ||
{ | ||
self::bootKernel(); | ||
|
||
// this uses a special testing container that allows you to fetch private services | ||
$command = self::$container->get($this->getCommandFqcn()); | ||
$command->setApplication(new Application(self::$kernel)); | ||
|
||
$commandTester = new CommandTester($command); | ||
$commandTester->setInputs($inputs); | ||
$commandTester->execute($arguments); | ||
|
||
return $commandTester; | ||
} | ||
|
||
abstract protected function getCommandFqcn(): string; | ||
} |
This file contains hidden or 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
This file contains hidden or 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,57 @@ | ||
<?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\ListUsersCommand; | ||
|
||
class ListUsersCommandTest extends AbstractCommandTest | ||
{ | ||
/** | ||
* @dataProvider maxResultsProvider | ||
* | ||
* This test verifies the amount of data is right according to the given parameter max results. | ||
*/ | ||
public function testListUsers(int $maxResults): void | ||
{ | ||
$tester = $this->executeCommand( | ||
['--max-results' => $maxResults] | ||
); | ||
|
||
$emptyDisplayLines = 5; | ||
$this->assertSame($emptyDisplayLines + $maxResults, mb_substr_count($tester->getDisplay(), "\n")); | ||
} | ||
|
||
public function maxResultsProvider(): ?\Generator | ||
{ | ||
yield [1]; | ||
yield [2]; | ||
} | ||
|
||
public function testItSendsNoEmailByDefault(): void | ||
{ | ||
$this->executeCommand([]); | ||
|
||
$this->assertEmailCount(0); | ||
} | ||
|
||
public function testItSendsAnEmailIfOptionProvided(): void | ||
{ | ||
$this->executeCommand(['--send-to' => '[email protected]']); | ||
|
||
$this->assertEmailCount(1); | ||
} | ||
|
||
protected function getCommandFqcn(): string | ||
{ | ||
return ListUsersCommand::class; | ||
} | ||
} |
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.
Moving this to the abstract class is wrong. Not all command tests require
stty
. And AFAICT, testing thelist:users
command will not require stty as it is not an interactive command.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.
Mmmmm, you are right! Anybody willing to make a Pull Request to fix this? Thanks!
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.
Fixed in #1230