-
-
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
Conversation
ad967f7
to
63fbfd1
Compare
{ | ||
$stream = fopen('php://memory', 'r+', false); | ||
|
||
fputs($stream, implode("\n", $this->userInputs)); |
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.
I think we use fwrite instead of fputs
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.
I tried to follow this http://symfony.com/doc/current/components/console/helpers/questionhelper.html#testing-a-command-that-expects-input as possible, please let me know if you still think that I should use fwrite.
63fbfd1
to
07c7fb1
Compare
@@ -129,4 +137,48 @@ public function getStatusCode() | |||
{ | |||
return $this->statusCode; | |||
} | |||
|
|||
/** | |||
* Sets the faked user inputs. |
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.
would remove the word faked
this should be documented somewhere thanks for your effort 👍 |
@OskarStark Thanks for the review. |
@chalasr could you please make the test more complex to simulate several inputs in a row? Thanks! |
daab5d1
to
e0be96f
Compare
@javiereguiluz Of course, I added a failure test (3 questions for only 2 inputs) and made the first taking 3 questions for 3 inputs. I also fixed the @OskarStark line notes and started to write documentation. |
e0be96f
to
9b5a645
Compare
Thank you for your effort 👍 |
According to #17136, this will not work if the SymfonyStyle is used. EDIT Should be fixed by #18902 |
Doc submitted. |
* | ||
* @return CommandTester | ||
*/ | ||
public function setUserInputs(array $inputs) |
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.
I would have named the method setInputs()
instead.
I made the changes and updated the doc. |
* | ||
* @param array | ||
* | ||
* @return CommandTester |
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.
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 $inputs
exactly is expected to be would be something that justified its presence.
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.
I added a description.
b4768e0
to
4c39bc8
Compare
Rebased this on #18999 to see the benefit by testing commands using both QuestionHelper and SymfonyQuestionHelper (via SymfonyStyle, no need of an HelperSet). |
438ca34
to
0e64796
Compare
|
||
return $this->statusCode = $this->command->run($this->input, $this->output); | ||
} | ||
/** |
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.
There is an indentation problem here for this method.
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. The changes of this PR are separated in 69dd333
0e64796
to
69dd333
Compare
…(chalasr) This PR was merged into the 3.2-dev branch. Discussion ---------- [Console] Centralize input stream in base Input class | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #10844 | License | MIT | Doc PR | not yet Actually we have two classes that have an `inputStream` property with its getter+setter: the QuestionHelper ('question' helper) and the SymfonyQuestionHelper (used via SymfonyStyle, that inherits the inputStream from the QuestionHelper class). This situation makes command testing really hard. ATM we can't test a command that uses `SymfonyStyle::ask` without ugly hacks, and we can't find a generic way to set the inputs passed to a command from the CommandTester, as it need to be done on a specific helper that may not be the one used by the command (SymfonyStyle case). What I propose here is to add a `stream` property (and its getter+setter) to the abstract `Symfony\Component\Console\Input\Input` class. For now I just made the two helpers setting their `inputStream` from `Input::getStream`, as both QuestionHelper and SymfonyQuestionHelper classes have an `ask` method that takes the command Input as argument. In a next time (4.0), we could remove the `getInputStream` and `setInputStream` methods from the QuestionHelper class (this only deprecates them in favor of Input `setStream` and `getStream`). This would close PR #18902 (trying to make interactive command testing with SymfonyStyle easier). This would also make PR #18710 widely better by setting the input stream in a generic way (working for both helpers without caring about if they are used and which one is used). Please give me your opinions. Commits ------- 54d3d63 Centralize input stream in base Input class
return $this; | ||
} | ||
|
||
private function getInputStream() |
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.
I would name it createInputStream()
and pass $this->inputs
as an argument.
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.
I would then rename it to createStream()
Just noticed that the second commit messages contain all messages from squashed commits, can you remove them as they are useless? Thanks. |
769312f
to
b748435
Compare
Made the changes, rebased and cleaned the commit message. |
Rename getInputStream() to getStream(), taking inputs as argument Make createStream static, typehint as array
b748435
to
c7ba38a
Compare
@fabpot As it takes the inputs as argument, I made the |
return $this; | ||
} | ||
|
||
private static function createStream(array $inputs) |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
@fabpot : Did you mean static
?
I guess it makes sense, as this method looks like a utility one.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
@fabpot : But the createStream
method is actually private
. Not public
. 😕
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.
lol, sorry about the confusion.
Thank you @chalasr. |
…dTester (chalasr) This PR was merged into the 3.2-dev branch. Discussion ---------- [Console] Simplify simulation of user inputs in CommandTester | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | symfony/symfony-docs#6623 After @javiereguiluz pointed it in #17470, I open this PR to simplify the simulation of user inputs for testing a Command. It would be done by calling `CommandTester::setUserInputs()` with an array of inputs as argument, and so make the CommandTester creating an input stream from the inputs set by the developer, then call `QuestionHelper::setInputStream` and assign it to the helperSet of the command, sort as all is done automatically in one call. Depends on #18999 Commits ------- c7ba38a [Console] Set user inputs from CommandTester
…ing user inputs (chalasr) This PR was merged into the master branch. Discussion ---------- [Console] Adapt doc for easier testing of commands needing user inputs Doc-PR for symfony/symfony#18710. This one eases testing of commands needing user interactions by simulating them from the CommandTester directly, by creating a input stream from the ones set by the developer. Commits ------- 26fdbe0 [Console] Adapt doc for easier testing of commands needing user inputs
After @javiereguiluz pointed it in #17470, I open this PR to simplify the simulation of user inputs for testing a Command.
It would be done by calling
CommandTester::setUserInputs()
with an array of inputs as argument, and so make the CommandTester creating an input stream from the inputs set by the developer, then callQuestionHelper::setInputStream
and assign it to the helperSet of the command, sort as all is done automatically in one call.Depends on #18999