-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Adding description of how to test a command which expects user input #2398
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,4 +97,43 @@ You can set the max number of times to ask in the ``$attempts`` argument. | |
If you reach this max number it will use the default value, which is given | ||
in the last argument. Using ``false`` means the amount of attempts is infinite. | ||
The user will be asked as long as he provides an invalid answer and will only | ||
be able to proceed if her input is valid. | ||
be able to proceed if her input is valid. | ||
|
||
Testing a command which expects input | ||
------------------------------------- | ||
|
||
If you want to write a unit test for a command which expects some kind of input | ||
from the command line, you need to overwrite the HelperSet used by the command:: | ||
|
||
use Symfony\Component\Console\Helper\DialogHelper; | ||
use Symfony\Component\Console\Helper\HelperSet; | ||
|
||
// ... | ||
public function testExecute() | ||
{ | ||
// ... | ||
$commandTester = new CommandTester($command); | ||
|
||
$dialog = $command->getHelper('dialog'); | ||
$dialog->setInputStream($this->getInputStream('Test\n')); | ||
// Equals to a user inputing "Test" and hitting ENTER | ||
// If you need to enter a confirmation, "yes\n" will work | ||
|
||
$commandTester->execute(array('command' => $command->getName())); | ||
|
||
// $this->assertRegExp('/.../', $commandTester->getDisplay()); | ||
} | ||
|
||
protected function getInputStream($input) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Asserting is not something that's usually done in a Command test. Mock objects are a better practise There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I copied this from the description on how to test commands. I think as a comment it is ok because it ilustrates that the command is now being executed and one can either test the outcome or end the test. |
||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unneeded empty line There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure which line you refer to! I guess between methods there should be an empty line, bracets should also be on their own line. I don't see any other empty lines? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :) there was an empty line somewhere here, but I commented on the oldest commit, but it is showing the comments on the new commit. Just a github bug.. |
||
$stream = fopen('php://memory', 'r+', false); | ||
fputs($stream, $input); | ||
rewind($stream); | ||
|
||
return $stream; | ||
} | ||
|
||
By setting the inputStream of the `DialogHelper`, you do the same the | ||
console would do internally with all user input through the cli. This way | ||
you can test any user interaction (even complex ones) by passing an appropriate | ||
input stream. |
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.
you don't need to overwrite the whole helperSet as all you need is calling a setter on the dialog helper: