Author: Kevin Bond (kevinbond@gmail.com)
Inheritance: extends Symfony\Component\Console\Helper\QuestionHelper
 public function testAskEscapeLabel()
 {
     $helper = new SymfonyQuestionHelper();
     $helper->setInputStream($this->getInputStream('sure'));
     $helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Do you want a \\?'));
     $this->assertOutputContains('Do you want a \\?', $output);
 }
Example #2
0
 /**
  * Main function for asking.
  *
  * @param array[] $tableList All existing tables from data schema.
  */
 private function startAsking($tableList)
 {
     $question = new Question('Please enter <note>TABLE NAME</note>: ');
     $tableName = $this->helper->ask($this->input, $this->output, $question);
     $key = StaticDataLayer::searchInRowSet('table_name', $tableName, $tableList);
     if (!isset($key)) {
         $this->io->logNote('Table \'%s\' not exist.', $tableName);
     } else {
         $this->askForCreateSP('INSERT', $tableName);
         $this->askForCreateSP('UPDATE', $tableName);
         $this->askForCreateSP('DELETE', $tableName);
         $this->askForCreateSP('SELECT', $tableName);
     }
 }
 /**
  * @param Question $question
  *
  * @return string
  */
 public function askQuestion(Question $question)
 {
     if ($this->input->isInteractive()) {
         $this->autoPrependBlock();
     }
     if (!$this->question) {
         $this->question = new SymfonyQuestionHelper();
     }
     $answer = $this->question->ask($this->input, $this, $question);
     if ($this->input->isInteractive()) {
         $this->newLine();
         $this->outputBuffered->write("\n");
     }
     return $answer;
 }
 public function testAskChoice()
 {
     $questionHelper = new SymfonyQuestionHelper();
     $helperSet = new HelperSet(array(new FormatterHelper()));
     $questionHelper->setHelperSet($helperSet);
     $heroes = array('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
     $this->assertEquals('Spiderman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
     $this->assertOutputContains('What is your favorite superhero? [Spiderman]', $output);
     $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
     $question->setMaxAttempts(1);
     $this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
     $this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
     $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
     $question->setErrorMessage('Input "%s" is not a superhero!');
     $question->setMaxAttempts(2);
     $this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
     $this->assertOutputContains('Input "Fabien" is not a superhero!', $output);
     try {
         $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '1');
         $question->setMaxAttempts(1);
         $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question);
         $this->fail();
     } catch (\InvalidArgumentException $e) {
         $this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
     }
     $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
     $question->setMaxAttempts(1);
     $question->setMultiselect(true);
     $this->assertEquals(array('Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
     $this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
     $this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
     $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0,1');
     $question->setMaxAttempts(1);
     $question->setMultiselect(true);
     $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
     $this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
     $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, ' 0 , 1 ');
     $question->setMaxAttempts(1);
     $question->setMultiselect(true);
     $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
     $this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
 }
 /**
  * @expectedException        \Symfony\Component\Console\Exception\RuntimeException
  * @expectedExceptionMessage Aborted
  */
 public function testAskThrowsExceptionOnMissingInput()
 {
     $dialog = new SymfonyQuestionHelper();
     $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new Question('What\'s your name?'));
 }
Example #6
0
 /**
  * Generate main part with name and params.
  *
  * @param array[]     $columns Columns from table.
  * @param string|null $spType  Stored procedure type {insert|update|delete|select}.
  *
  * @return array[]|null
  */
 protected function checkUniqueKeys($columns, $spType = null)
 {
     $primaryKeys = DataLayer::getTablePrimaryKeys($this->dataSchema, $this->tableName);
     $uniqueKeys = DataLayer::getTableUniqueKeys($this->dataSchema, $this->tableName);
     $resultColumns = [];
     if (!isset($spType)) {
         if (count($uniqueKeys) <= 0 && count($primaryKeys) <= 0) {
             return null;
         } else {
             return $columns;
         }
     }
     if (count($primaryKeys) > 0) {
         foreach ($columns as $column) {
             $check = StaticDataLayer::searchInRowSet('Column_name', $column['column_name'], $primaryKeys);
             if (isset($check)) {
                 $resultColumns[] = $column;
             }
         }
         return $resultColumns;
     } else {
         if (count($uniqueKeys) > 0) {
             reset($uniqueKeys);
             $first = key($uniqueKeys);
             if (count($uniqueKeys) > 1) {
                 $this->io->writeln(sprintf('Table <dbo>%s</dbo> has more than one unique key.', $this->tableName));
                 $array = [];
                 foreach ($uniqueKeys as $column) {
                     if (isset($array[$column['Key_name']])) {
                         $array[$column['Key_name']] .= ',';
                         $array[$column['Key_name']] .= $column['Column_name'];
                     } else {
                         $array[$column['Key_name']] = $column['Column_name'];
                     }
                 }
                 $tableArray = [];
                 foreach ($array as $key => $column) {
                     $tableArray[] = [$key, $column];
                 }
                 $table = new Table($this->output);
                 $table->setHeaders(['Name', 'Keys']);
                 $table->setRows($tableArray);
                 $table->render();
                 $question = new Question(sprintf('What unique keys use in statement?(%s): ', $uniqueKeys[$first]['Key_name']), $uniqueKeys[$first]['Key_name']);
                 $uniqueKeys = $this->helper->ask($this->input, $this->output, $question);
                 $uniqueKeys = explode(',', $array[$uniqueKeys]);
                 foreach ($uniqueKeys as $column) {
                     $resultColumns[] = ['column_name' => $column];
                 }
                 return $resultColumns;
             } else {
                 foreach ($uniqueKeys as $column) {
                     $resultColumns[] = ['column_name' => $column['Column_name']];
                 }
                 return $resultColumns;
             }
         } else {
             return null;
         }
     }
 }