/** * */ public function testQuestionWithNoInputAndCustomDefault() { $input = m::mock('mako\\cli\\input\\Input'); $input->shouldReceive('read')->once()->andReturn(); $output = m::mock('mako\\cli\\output\\Output'); $output->shouldReceive('write')->once()->with('Username: '******'foobar', $question->ask('Username:'******'foobar')); }
/** * Asks user for confirmation and returns value corresponding to the chosen value. * * @access public * @param string $question Question to ask * @param string $default Default answer * @param array $options Answer options * @return boolean */ public function ask($question, $default = 'n', array $options = null) { $options = $options === null ? ['y' => true, 'n' => false] : $this->normalizeKeys($options); $input = parent::ask(trim($question) . ' [' . $this->getOptions($options, $default) . '] '); $input = mb_strtolower(empty($input) ? $default : $input); if (!isset($options[$input])) { return $this->ask($question, $default, $options); } return $options[$input]; }
/** * Writes question to output and returns hidden user input. * * @access public * @param string $question Question to ask * @param null|mixed $default Default if no input is entered * @param boolean $fallback Fall back to non-hidden input? * @return string */ public function ask($question, $default = null, $fallback = false) { if (DIRECTORY_SEPARATOR === '\\' || $this->hasStty()) { $this->output->write(trim($question) . ' '); if (DIRECTORY_SEPARATOR === '\\') { $answer = trim(shell_exec(__DIR__ . '/resources/hiddeninput.exe')); } else { $settings = shell_exec('stty -g'); exec('stty -echo'); $answer = $this->input->read(); exec('stty ' . $settings); } $this->output->write(PHP_EOL); return empty($answer) ? $default : $answer; } elseif ($fallback) { return parent::ask($question, $default); } else { throw new RuntimeException(vsprintf("%s(): Unable to hide the user input.", [__METHOD__])); } }