Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] Adding console command assertion expectsChoice() #32139

Merged
merged 3 commits into from
Mar 30, 2020
Merged

[7.x] Adding console command assertion expectsChoice() #32139

merged 3 commits into from
Mar 30, 2020

Conversation

nuernbergerA
Copy link
Contributor

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')
  ->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', ['Taylor', 'Dayle'])
  ->assertExitCode(0);
}

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

Per default we compare both arrays ignoring the order using assertEqualsCanonicalizing, if needed can switch to assertEquals via a fourth parameter.

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

@christophrumpel
Copy link
Contributor

+1 👌

@GrahamCampbell GrahamCampbell changed the title [7.x] adding console command assertion expectsChoice() [7.x] Adding console command assertion expectsChoice() Mar 27, 2020
@taylorotwell taylorotwell merged commit 0bb7ebc into laravel:7.x Mar 30, 2020
@nuernbergerA
Copy link
Contributor Author

@taylorotwell any idea how we can cover the whole console assertion stuff with tests?

@nuernbergerA nuernbergerA deleted the feature-console-test-expects-choice branch March 30, 2020 14:15
imacrayon pushed a commit to imacrayon/framework that referenced this pull request Mar 31, 2020
* adding console command assertion expectsChoice()

* Update InteractsWithConsole.php

* Update InteractsWithConsole.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants