Browse Source

Migrate ConsoleIO::select to use QuestionHelper and ChoiceQuestion

Gawain Lynch 7 years ago
parent
commit
157075b996
2 changed files with 32 additions and 23 deletions
  1. 10 6
      src/Composer/IO/ConsoleIO.php
  2. 22 17
      tests/Composer/Test/IO/ConsoleIOTest.php

+ 10 - 6
src/Composer/IO/ConsoleIO.php

@@ -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 Composer\Question\StrictConfirmationQuestion;
+use Symfony\Component\Console\Question\ChoiceQuestion;
 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()) {
-            return $this->helperSet->get('dialog')->select($this->getErrorOutput(), $question, $choices, $default, $attempts, $errorMessage, $multiselect);
-        }
+        /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
+        $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);
     }
     }
 
 
     /**
     /**

+ 22 - 17
tests/Composer/Test/IO/ConsoleIOTest.php

@@ -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\HelperSet');
+        $helperMock = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper');
+        $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));
-        $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));
+            ->will($this->returnValue(true))
+        ;
 
 
-        $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);
     }
     }