-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console] Simplify simulation of user inputs in CommandTester #18710
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 |
---|---|---|
|
@@ -21,12 +21,14 @@ | |
* Eases the testing of console commands. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
* @author Robin Chalas <robin.chalas@gmail.com> | ||
*/ | ||
class CommandTester | ||
{ | ||
private $command; | ||
private $input; | ||
private $output; | ||
private $inputs = array(); | ||
private $statusCode; | ||
|
||
/** | ||
|
@@ -65,6 +67,10 @@ public function execute(array $input, array $options = array()) | |
} | ||
|
||
$this->input = new ArrayInput($input); | ||
if ($this->inputs) { | ||
$this->input->setStream(self::createStream($this->inputs)); | ||
} | ||
|
||
if (isset($options['interactive'])) { | ||
$this->input->setInteractive($options['interactive']); | ||
} | ||
|
@@ -129,4 +135,29 @@ public function getStatusCode() | |
{ | ||
return $this->statusCode; | ||
} | ||
|
||
/** | ||
* Sets the user inputs. | ||
* | ||
* @param array An array of strings representing each input | ||
* passed to the command input stream. | ||
* | ||
* @return CommandTester | ||
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. Imo this docblock as it is now is quite useless and could be removed as well (input types and the return type can be inferred by IDEs from the method itself) and the description doesn't add any value to the user apart from what the method name already states. Though explaining what 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 added a description. |
||
*/ | ||
public function setInputs(array $inputs) | ||
{ | ||
$this->inputs = $inputs; | ||
|
||
return $this; | ||
} | ||
|
||
private static function createStream(array $inputs) | ||
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. What's the benefit of marking it public? I would have kept it private. 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. @fabpot : Did you mean 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. No, I meant public. I think it should be private, I don't see why an end user would have to use this. 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. @fabpot : But the 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. lol, sorry about the confusion. |
||
{ | ||
$stream = fopen('php://memory', 'r+', false); | ||
|
||
fputs($stream, implode(PHP_EOL, $inputs)); | ||
rewind($stream); | ||
|
||
return $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.
This looks wrong to me. Here we are only dealing with one specific case, which is the question helper. We should rather find a way to make this "generic" if possible.
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.
@fabpot Only the QuestionHelper and SymfonyQuestionHelper have an
inputStream
, so I don't see a real benefit to make this looking for another object that thequestion
helper.I made this covering the case of SymfonyStyle is used (SymfonyQuestionHelper) in #18902, as this one is not part of the command helperset, we don't have any way to make it generic. It's planned for 4.0 to move the setInputStream method in the InputInterface, but ATM the QuesitonHelper is the only available place (and documented as is).
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.
Adding an interface with a
setInputStream
method then implement it in the Input object could be a good solution. We could then automatically useInput::$inputStream
in every helper that has aninputStream
, working for both QuestionHelper and SymfonyQuestionHelper. At first we could look for theInput::$inputStream
while letting theQuestionHelper::$inputStream
as deprecated, for finally remove it in 4.0.What do you think? It would close #18902 and ease interactive command testing properly.
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.
@fabpot I just opened #18999 that would BTW solve this problem.
It gives: