Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] fix a bug when you are passing a default value and passing -n would output the index #25521

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

Simperfit
Copy link
Contributor

@Simperfit Simperfit commented Dec 16, 2017

Q A
Branch? 2.7
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #25470
License MIT
Doc PR

We are fixing a bug when you are using a default value with -n, it would output the index instead of the value.

Simple reproducer Simperfit/symfony-reproducer@5079dd1?diff=split#diff-77efcc28bc5309e1af9ac07a1e073009R40

Copy link
Contributor

@sroze sroze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are missing a test sir!

@@ -44,7 +44,13 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
$output = $output->getErrorOutput();
}

if (!$input->isInteractive()) {
$isInteractive = !$input->isInteractive();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$isNotInteractive ?

if (!$input->isInteractive()) {
$isInteractive = !$input->isInteractive();

if ($isInteractive && $question instanceof ChoiceQuestion) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or I think that you should just put if ($question instanceof ChoiceQuestion) { within the existing if (!$input->isInteractive()) {.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn’t the ChoiceQuestion itself take care of returning the correct default value ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if we should nest theses ifs, IMO its clearer like this.

@mathroc I wanted to it like this firstly but it's hard since the getDefault is called without the choices being changed.

Copy link
Contributor Author

@Simperfit Simperfit Dec 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nvm @sroze you are right, it avoids one test for nothing.

@Simperfit Simperfit force-pushed the bugfix/fix-a-wrong-output-when-passing-in-interactive-mode branch 2 times, most recently from 338c78d to 56ccf18 Compare December 16, 2017 11:44
@Simperfit Simperfit changed the base branch from master to 2.7 December 16, 2017 11:46
@Simperfit Simperfit force-pushed the bugfix/fix-a-wrong-output-when-passing-in-interactive-mode branch from 56ccf18 to 066108a Compare December 16, 2017 11:47
@Simperfit
Copy link
Contributor Author

Test added

@Simperfit
Copy link
Contributor Author

Status: Needs Review

@@ -84,6 +84,10 @@ public function testAskChoice()
$question->setMultiselect(true);

$this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));

$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, 0);
// We are supposed to get the default value since we are in interactive mode
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we are not in interactive mode

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! forgot that little word :p

@mathroc
Copy link
Contributor

mathroc commented Dec 16, 2017

thx @Simperfit to have taken a look at this. there’s still something odd.

I’m not sure the default value passed to the question should be its key, the doc comment says @param mixed $default The default answer to return so maybe the problem is instead that SymfonyStyle is converting the default value to its key

ps: but to be fair, that’s what the documentation is doing

@Simperfit
Copy link
Contributor Author

@mathroc SymfonyStyle is transforming this array. So it has value -> key. This fixes both behaviour when passing the key or the value look at the reproducer i've done to be sure that nothing will be broken : Simperfit/symfony-reproducer@b37a2a5

@Simperfit Simperfit force-pushed the bugfix/fix-a-wrong-output-when-passing-in-interactive-mode branch 3 times, most recently from f878424 to 0704f80 Compare December 16, 2017 12:11
@Simperfit Simperfit force-pushed the bugfix/fix-a-wrong-output-when-passing-in-interactive-mode branch from 0704f80 to 41ffc69 Compare December 16, 2017 12:12
@Simperfit
Copy link
Contributor Author

Travis failure is unrelated

@nicolas-grekas nicolas-grekas added this to the 2.7 milestone Dec 18, 2017
@nicolas-grekas
Copy link
Member

Thank you @Simperfit.

@nicolas-grekas nicolas-grekas merged commit 41ffc69 into symfony:2.7 Dec 18, 2017
nicolas-grekas added a commit that referenced this pull request Dec 18, 2017
…nd passing -n would output the index (Simperfit)

This PR was merged into the 2.7 branch.

Discussion
----------

[Console] fix a bug when you are passing a default value and passing -n would output the index

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | #25470
| License       | MIT
| Doc PR        |

We are fixing a bug when you are using a default value with -n, it would output the index instead of the value.

Simple reproducer Simperfit/symfony-reproducer@5079dd1?diff=split#diff-77efcc28bc5309e1af9ac07a1e073009R40

Commits
-------

41ffc69 [Console] fix a bug when you are passing a default value and passing -n would ouput the index
@Simperfit Simperfit deleted the bugfix/fix-a-wrong-output-when-passing-in-interactive-mode branch December 19, 2017 07:59
greg0ire added a commit to greg0ire/Behat that referenced this pull request May 21, 2018
This is how the QuestionHelper API was intended to be used, but it only
has been so since a recent bugfix.
See symfony/symfony#25521
Fixes Behat#1151
@greg0ire greg0ire mentioned this pull request May 21, 2018
greg0ire added a commit to greg0ire/Behat that referenced this pull request May 21, 2018
This is how the QuestionHelper API was intended to be used, but it only
has been so since a recent bugfix.
See symfony/symfony#25521
Fixes Behat#1151
greg0ire added a commit to greg0ire/Behat that referenced this pull request May 21, 2018
This is how the QuestionHelper API was intended to be used, but it only
has been so since a recent bugfix.
See symfony/symfony#25521
Fixes Behat#1151
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants