Skip to content
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

PHPUnit test freezes when testing ChoiceCommand with multiSelect enabled #30138

Closed
dugajean opened this issue Feb 11, 2019 · 4 comments
Closed

Comments

@dugajean
Copy link

dugajean commented Feb 11, 2019

Symfony version(s) affected: Console 4.2.3

Description
This is a standalone Symfony Console project. I have one simple test, to test a command that requires input. I use the setInputs method in the test on $commandTester and input 6 answers there for the 6 questions that will be asked. However, the test hangs at the ChoiceQuestion with multiSelect enabled ($question->setMultiselect(true);), causing PHP to use a lot of processing power (CPU usage) and the Terminal ends up using a lot of RAM.

How to reproduce
Make a simple test like this one:

<?php

namespace Dockr\Tests\Command;

use MyApp\Commands\MyCoolCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

class MyCoolCommandTest extends TestCase
{
    public function testExecute()
    {
        $application = new Application();
        $application->add(new MyCoolCommand);

        $command = $application->find('do_cool_stuff');

        $commandTester = new CommandTester($command);
        $commandTester->setInputs(['This', 'This', 'This', 'This', 'This', 'This']);
        $commandTester->execute(['command' => $command->getName()]);
    }
}

And make sure that there is one multiSelect question ($question->setMultiselect(true);). The question should hang at the $questionHelper->ask($input, $output, $question) call.

Please note: This command works perfectly fine when running the command manually, only fails when running it via a test like above.

@chalasr
Copy link
Member

chalasr commented Feb 14, 2019

I confirm it hangs when no input is given to the question (that is a bug).
However, it works as expected if you set the proper inputs.
Can you show the tested command? Note that, as in a regular cli context, a multi-select input must be only one string with coma-separated values, i.e:

// command
$question = new ChoiceQuestion('Choice', ['a', 'b', 'c']);
$questionHelper->ask($input, $output, $question);

// test
$tester->setInputs('a, b');

And if your command asks multiple questions, an input string must be set for all of them (using an empty string for simulating enter).
I'm looking at fixing the issue when one or more inputs are missing.

@dugajean
Copy link
Author

Good! Thank you!

Well, I provided wrong input - input that wasn't part of the $choices. That resulted in a hang as well.

Yes, I'm aware that you need to provide an answer for each question. Above you can see 6 answers (This x 6) for my 6 questions in the command. I will provide the command later today.

@chalasr
Copy link
Member

chalasr commented Feb 16, 2019

Providing a wrong input to a choice question leads to the question being asked again, except if the question has no validator or if $question->setMaxAttempts(1) has been called, hence it behaves as a missing input (since no input is set for the second attempt).
Still, tests should never hang. #30271 fixes it, would be nice to have your confirmation.

@dugajean
Copy link
Author

@chalasr The test passes now. Thanks a lot!

chalasr pushed a commit that referenced this issue Feb 19, 2019
…alasr)

This PR was merged into the 3.4 branch.

Discussion
----------

[Console] Fix command testing with missing user inputs

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #30138
| License       | MIT
| Doc PR        | n/a

Fixes the remaining cases where test execution hangs using `CommandTester`.

Commits
-------

ac4e9b0 [Console] Fix command testing with missing inputs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants