QuestionHelper example



    /** * Gets the default helper set with the helpers that should always be available. */
    protected function getDefaultHelperSet(): HelperSet
    {
        return new HelperSet([
            new FormatterHelper(),
            new DebugFormatterHelper(),
            new ProcessHelper(),
            new QuestionHelper(),
        ]);
    }

    /** * Returns abbreviated suggestions in string format. */
    private function getAbbreviationSuggestions(array $abbrevs): string
    {
        return ' '.implode("\n ", $abbrevs);
    }

    


    public function testCommandWithInputs()
    {
        $questions = [
            'What\'s your name?',
            'How are you?',
            'Where do you come from?',
        ];

        $command = new Command('foo');
        $command->setHelperSet(new HelperSet([new QuestionHelper()]));
        $command->setCode(function D$input$output) use ($questions$command) {
            $helper = $command->getHelper('question');
            $helper->ask($input$outputnew Question($questions[0]));
            $helper->ask($input$outputnew Question($questions[1]));
            $helper->ask($input$outputnew Question($questions[2]));
        });

        $tester = new CommandTester($command);
        $tester->setInputs(['Bobby', 'Fine', 'France']);
        $tester->execute([]);

        


    /** * Gets the default helper set with the helpers that should always be available. */
    protected function getDefaultHelperSet(): HelperSet
    {
        return new HelperSet([
            new FormatterHelper(),
            new DebugFormatterHelper(),
            new ProcessHelper(),
            new QuestionHelper(),
        ]);
    }

    /** * Returns abbreviated suggestions in string format. */
    private function getAbbreviationSuggestions(array $abbrevs): string
    {
        return ' '.implode("\n ", $abbrevs);
    }

    
public function testGetDisplay()
    {
        $this->assertEquals('foo'.\PHP_EOL, $this->tester->getDisplay(), '->getDisplay() returns the display of the last execution');
    }

    public function testSetInputs()
    {
        $application = new Application();
        $application->setAutoExit(false);
        $application->register('foo')->setCode(function D$input$output) {
            $helper = new QuestionHelper();
            $helper->ask($input$outputnew Question('Q1'));
            $helper->ask($input$outputnew Question('Q2'));
            $helper->ask($input$outputnew Question('Q3'));
        });
        $tester = new ApplicationTester($application);

        $tester->setInputs(['I1', 'I2', 'I3']);
        $tester->run(['command' => 'foo']);

        $tester->assertCommandIsSuccessful();
        $this->assertEquals('Q1Q2Q3', $tester->getDisplay(true));
    }
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Terminal;
use Symfony\Component\Console\Tester\ApplicationTester;

/** * @group tty */
class QuestionHelperTest extends AbstractQuestionHelperTestCase
{
    public function testAskChoice()
    {
        $questionHelper = new QuestionHelper();

        $helperSet = new HelperSet([new FormatterHelper()]);
        $questionHelper->setHelperSet($helperSet);

        $heroes = ['Superman', 'Batman', 'Spiderman'];

        $inputStream = $this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n");

        $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '2');
        $question->setMaxAttempts(1);
        // first answer is an empty answer, we're supposed to receive the default value
use Symfony\Component\Console\Question\Question;

$vendor = __DIR__;
while (!file_exists($vendor.'/vendor')) {
    $vendor = \dirname($vendor);
}
require $vendor.'/vendor/autoload.php';

(new Application())
    ->register('app')
    ->setCode(function(InputInterface $input, OutputInterface $output) {
        $output->writeln((new QuestionHelper())->ask($input$outputnew Question('Foo?', 'foo')));
        $output->writeln((new QuestionHelper())->ask($input$outputnew Question('Bar?', 'bar')));
    })
    ->getApplication()
    ->setDefaultCommand('app', true)
    ->run()
;
--EXPECT--
Foo?Hello World
Bar?bar
$inputStream = fopen('php://memory', 'r+', false);
        fwrite($inputStream, "Batman & Robin\n");
        rewind($inputStream);

        $input = $this->createMock(StreamableInputInterface::class);
        $input->expects($this->once())->method('isInteractive')->willReturn(true);
        $input->expects($this->once())->method('getStream')->willReturn($inputStream);

        $sections = [];
        $output = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());

        (new QuestionHelper())->ask($input$outputnew Question('What\'s your favorite super hero?'));
        $output->clear();

        rewind($output->getStream());
        $this->assertSame('What\'s your favorite super hero?'.\PHP_EOL."\x1b[2A\x1b[0J", stream_get_contents($output->getStream()));
    }

    public function testWriteWithoutNewLine()
    {
        $sections = [];
        $output = new ConsoleSectionOutput($this->stream, $sections, OutputInterface::VERBOSITY_NORMAL, true, new OutputFormatter());

        
Home | Imprint | This part of the site doesn't use cookies.