Skip to content

Instantly share code, notes, and snippets.

@christophrumpel
Last active March 27, 2020 18:11
Show Gist options
  • Save christophrumpel/e8c8460d1d45a43dba4d03812cc1fd83 to your computer and use it in GitHub Desktop.
Save christophrumpel/e8c8460d1d45a43dba4d03812cc1fd83 to your computer and use it in GitHub Desktop.

This PR adds a new console test method for asking choices.

Current State

You can ask a multiple-choice question in a Laravel command with the choice method:

$name = $this->choice('What is your name?', ['Taylor', 'Dayle'], $defaultIndex);

Currently, you can only assert the message of this question and define a reply like:

$this->artisan('question')
  ->expectsQuestion('What is your name?', 'Taylor Otwell')
  ->assertExitCode(0);
}

What you cannot test here are the given choices: ['Taylor', 'Dayle'] Depending on how your command looks like, these choices may change through input.

Solution

In order to solve this problem, this PR adds a new method to expect choices. The code above can now be tested like:

$this->artisan('question')
  ->expectsChoice('What is your name?', 'Taylor Otwell', ['Taylor', 'Dayle'])
  ->assertExitCode(0);
}

Next to the message and the reply, you can now define the expected choices as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment