1
0
Fork 0

Migrate ConsoleIO::select to use QuestionHelper and ChoiceQuestion

pull/6788/head
Gawain Lynch 2017-11-06 16:29:55 +01:00
parent 8b42aed060
commit 157075b996
No known key found for this signature in database
GPG Key ID: 508C10F858FD246E
2 changed files with 32 additions and 23 deletions

View File

@ -12,11 +12,12 @@
namespace Composer\IO; namespace Composer\IO;
use Composer\Question\StrictConfirmationQuestion;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface; use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\HelperSet; use Symfony\Component\Console\Question\ChoiceQuestion;
use Composer\Question\StrictConfirmationQuestion;
use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Question\Question;
/** /**
@ -281,11 +282,14 @@ class ConsoleIO extends BaseIO
*/ */
public function select($question, $choices, $default, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false) public function select($question, $choices, $default, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false)
{ {
if ($this->isInteractive()) { /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
return $this->helperSet->get('dialog')->select($this->getErrorOutput(), $question, $choices, $default, $attempts, $errorMessage, $multiselect); $helper = $this->helperSet->get('question');
} $question = new ChoiceQuestion($question, $choices, $default);
$question->setMaxAttempts($attempts ?: null); // IOInterface requires false, and Question requires null or int
$question->setErrorMessage($errorMessage);
$question->setMultiselect($multiselect);
return $default; return $helper->ask($this->input, $this->getErrorOutput(), $question);
} }
/** /**

View File

@ -229,27 +229,32 @@ class ConsoleIOTest extends TestCase
{ {
$inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface'); $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
$outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
$dialogMock = $this->getMock('Symfony\Component\Console\Helper\DialogHelper'); $helperMock = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper');
$helperMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet'); $setMock = $this->getMock('Symfony\Component\Console\Helper\HelperSet');
$helperMock
->expects($this->once())
->method('ask')
->with(
$this->isInstanceOf('Symfony\Component\Console\Input\InputInterface'),
$this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
$this->isInstanceOf('Symfony\Component\Console\Question\Question')
)
;
$setMock
->expects($this->once())
->method('get')
->with($this->equalTo('question'))
->will($this->returnValue($helperMock))
;
$inputMock->expects($this->once()) $inputMock->expects($this->once())
->method('isInteractive') ->method('isInteractive')
->will($this->returnValue(true)); ->will($this->returnValue(true))
$dialogMock->expects($this->once()) ;
->method('select')
->with($this->isInstanceOf('Symfony\Component\Console\Output\OutputInterface'),
$this->equalTo('Select item'),
$this->equalTo(array("item1", "item2")),
$this->equalTo(null),
$this->equalTo(false),
$this->equalTo("Error message"),
$this->equalTo(true));
$helperMock->expects($this->once())
->method('get')
->with($this->equalTo('dialog'))
->will($this->returnValue($dialogMock));
$consoleIO = new ConsoleIO($inputMock, $outputMock, $helperMock); $consoleIO = new ConsoleIO($inputMock, $outputMock, $setMock);
$consoleIO->select('Select item', array("item1", "item2"), null, false, "Error message", true); $consoleIO->select('Select item', array("item1", "item2"), null, false, "Error message", true);
} }