Core\Test::answerCli
Core\Test::answerCli(array<string> $answers): voidCore\Test::answerCli( array<string> $answers, ): void
Core\Test::answerCli() gives the answers for the next questions your program asks with Core\Cli. It lets you test a command-line program that asks questions, with no person at the keyboard. You pass an array of strings, one answer per question, in the order the program asks.
A question that Core\Cli asks is called a prompt. Each prompt takes the first answer that is still waiting. It does not read the terminal. Write what a person would type. Core\Cli::confirm reads y or n, and Core\Cli::select reads the number of a choice. When an answer is not valid, the prompt asks again and takes the next answer.
A second call adds its answers after the ones still waiting. When no answer is left, a prompt returns its default. A prompt with no default throws Core\Cli\NotInteractive.
You can call answerCli in a test and in a command-line program. In server code, it throws a LogicError. Server code is a request, a connection, a scheduled task or a queued job. There is one exception: a request that a test sends with Core\Test::request may call it.
The examples below show one answer, several prompts in a row, and a test of a setup program.
Parameters
$answersarray<string>One answer per prompt, in the order the program asks. Write what a person would type, without the line ending.Core\Cli::selectreads the number of a choice, andCore\Cli::confirmreadsyorn.
Return value
Returns void. Nothing. A second call adds its answers to the end of the list. When the list is empty, a prompt returns the default you gave it. A prompt with no default throws Core\Cli\NotInteractive.
Errors
LogicErrorThe call runs in a server, such as in a request, a connection, a scheduled task or a queued job. There is one exception: a request that a test sends withCore\Test::requestmay call it. Tests and command-line programs may always call it.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Test::answerCli sets the input that each prompt reads in a test. Core\Test\Outcome->hasPassed() checks whether a test passed. The old names were scriptAnswers and passed. |
Examples
Answer one question
<?nvs// Shows how to answer a question from `Core\Cli::ask` without a person at the keyboard.
// The next prompt gets the answer "Ana".Core\Test::answerCli(['Ana']);
// `ask` takes the answer and does not wait for the terminal.// The result is a `tainted string`, because the text comes from outside the program.tainted string $name = Core\Cli::ask('What is your name? ');
// This prints `Hello, Ana!`.echo 'Hello, ', $name, "!\n";Hello, Ana!Several prompts in a row
<?nvs// Shows how several prompts take the answers in order, and what happens when none is left.
// "maybe" is not a valid answer to `confirm`, so it asks again and takes "y".// `select` reads "2" and returns the second choice.Core\Test::answerCli(['maybe', 'y', '2']);echo Core\Cli::confirm('Continue?') ? 'yes' : 'no', "\n";echo Core\Cli::select('Size?', ['small', 'medium', 'large']), "\n";
// A second call adds one more answer.Core\Test::answerCli(['Oslo']);echo Core\Cli::ask('City?'), "\n";
// No answer is left. A prompt with a `default` returns it.echo Core\Cli::ask('Country?', { default: 'Norway' }), "\n";
// A prompt with no `default` throws `Core\Cli\NotInteractive`.try { Core\Cli::ask('Street?');} catch (Core\Cli\NotInteractive $error) { echo "no answer left\n";}yesmediumOsloNorwayno answer leftTest a setup program
<?nvs// Shows a test of a setup program that asks three questions and writes a configuration line.
final class Setup{ // Asks for a project name, a database and whether to add example data. // The name comes from outside the program, so the result is a `tainted string`. public static function run(): tainted string { tainted string $name = Core\Cli::ask('Project name?'); array<string> $databases = ['sqlite', 'postgres', 'mysql']; string $database = Core\Cli::select('Database?', $databases); bool $examples = Core\Cli::confirm('Add example data?', { default: false }); return $name . ' ' . $database . ' examples=' . ($examples ? 'yes' : 'no'); }}
// The test gives the three answers a person would type.Core\Test::answerCli(['shop', '2', 'y']);Core\Test::assertSame(Setup::run(), 'shop postgres examples=yes');
// An empty answer to `confirm` means its `default`, which is "no".Core\Test::answerCli(['blog', '1', '']);Core\Test::assertSame(Setup::run(), 'blog sqlite examples=no');
echo "the setup program works\n";the setup program works