Skip to content

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

Merged
taylorotwell merged 3 commits into
laravel:7.xfrom
nuernbergerA:feature-console-test-expects-choice
Mar 30, 2020
Merged

[7.x] Adding console command assertion expectsChoice()#32139
taylorotwell merged 3 commits into
laravel:7.xfrom
nuernbergerA:feature-console-test-expects-choice

Conversation

@nuernbergerA

Copy link
Copy Markdown
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
Copy Markdown
Contributor

+1 👌

@GrahamCampbell GrahamCampbell changed the title [7.x] adding console command assertion expectsChoice() [7.x] Adding console command assertion expectsChoice() Mar 27, 2020
Comment thread src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php
@taylorotwell taylorotwell merged commit 0bb7ebc into laravel:7.x Mar 30, 2020
@nuernbergerA

Copy link
Copy Markdown
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