ConfirmationQuestion example

if (!file_exists($info_file)) {
      $io->getErrorStyle()->error("The theme info file $info_file could not be read.");
      return 1;
    }

    $info = Yaml::decode(file_get_contents($info_file));
    $info['name'] = $input->getOption('name') ?: $destination_theme;

    $info['core_version_requirement'] = '^' . $this->getVersion();

    if (!array_key_exists('version', $info)) {
      $confirm_versionless_source_theme = new ConfirmationQuestion(sprintf('The source theme %s does not have a version specified. This makes tracking changes in the source theme difficult. Are you sure you want to continue?', $source_theme->getName()));
      if (!$io->askQuestion($confirm_versionless_source_theme)) {
        return 0;
      }
    }

    $source_version = $info['version'] ?? 'unknown-version';
    if ($source_version === 'VERSION') {
      $source_version = \Drupal::VERSION;
    }
    // A version in the generator string like "9.4.0-dev" is not very helpful.     // When this occurs, generate a version string that points to a commit.

        $question = new Question($question);

        $question->setHidden(true);
        $question->setValidator($validator);

        return $this->askQuestion($question);
    }

    public function confirm(string $question, bool $default = true): bool
    {
        return $this->askQuestion(new ConfirmationQuestion($question$default));
    }

    public function choice(string $question, array $choices, mixed $default = null, bool $multiSelect = false): mixed
    {
        if (null !== $default) {
            $values = array_flip($choices);
            $default = $values[$default] ?? $default;
        }

        $questionChoice = new ChoiceQuestion($question$choices$default);
        $questionChoice->setMultiselect($multiSelect);

        
$this->assertSame(8, ftell($response));
    }

    /** * @dataProvider getAskConfirmationData */
    public function testAskConfirmation($question$expected$default = true)
    {
        $dialog = new QuestionHelper();

        $inputStream = $this->getInputStream($question."\n");
        $question = new ConfirmationQuestion('Do you like French fries?', $default);
        $this->assertEquals($expected$dialog->ask($this->createStreamableInputInterfaceMock($inputStream)$this->createOutputInterface()$question), 'confirmation question should '.($expected ? 'pass' : 'cancel'));
    }

    public static function getAskConfirmationData()
    {
        return [
            ['', true],
            ['', false, false],
            ['y', true],
            ['yes', true],
            ['n', false],
            [

        $question = new Question($question);

        $question->setHidden(true);
        $question->setValidator($validator);

        return $this->askQuestion($question);
    }

    public function confirm(string $question, bool $default = true): bool
    {
        return $this->askQuestion(new ConfirmationQuestion($question$default));
    }

    public function choice(string $question, array $choices, mixed $default = null, bool $multiSelect = false): mixed
    {
        if (null !== $default) {
            $values = array_flip($choices);
            $default = $values[$default] ?? $default;
        }

        $questionChoice = new ChoiceQuestion($question$choices$default);
        $questionChoice->setMultiselect($multiSelect);

        
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Question\ConfirmationQuestion;

class ConfirmationQuestionTest extends TestCase
{
    /** * @dataProvider normalizerUsecases */
    public function testDefaultRegexUsecases($default$answers$expected$message)
    {
        $sut = new ConfirmationQuestion('A question', $default);

        foreach ($answers as $answer) {
            $normalizer = $sut->getNormalizer();
            $actual = $normalizer($answer);
            $this->assertEquals($expected$actualsprintf($message$answer));
        }
    }

    public static function normalizerUsecases()
    {
        return [
            [
Home | Imprint | This part of the site doesn't use cookies.