Skip to content

Commit

Permalink
[7.x] Adding console command assertion expectsChoice() (laravel#32139)
Browse files Browse the repository at this point in the history
* adding console command assertion expectsChoice()

* Update InteractsWithConsole.php

* Update InteractsWithConsole.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
nuernbergerA and taylorotwell committed Mar 30, 2020
1 parent eb0c397 commit 0bb7ebc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ trait InteractsWithConsole
*/
public $expectedQuestions = [];

/**
* All of the expected choice questions.
*
* @var array
*/
public $expectedChoices = [];

/**
* Call artisan command and return code.
*
Expand All @@ -48,6 +55,18 @@ public function artisan($command, $parameters = [])
$this->fail('Question "'.Arr::first($this->expectedQuestions)[0].'" was not asked.');
}

if (count($this->expectedChoices) > 0) {
foreach ($this->expectedChoices as $question => $answers) {
$assertion = $answers['strict'] ? 'assertEquals' : 'assertEqualsCanonicalizing';

$this->{$assertion}(
$answers['expected'],
$answers['actual'],
'Question "'.$question.'" has different options.'
);
}
}

if (count($this->expectedOutput)) {
$this->fail('Output "'.Arr::first($this->expectedOutput).'" was not printed.');
}
Expand Down
23 changes: 23 additions & 0 deletions src/Illuminate/Testing/PendingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,25 @@ public function expectsConfirmation($question, $answer = 'no')
return $this->expectsQuestion($question, strtolower($answer) === 'yes');
}

/**
* Specify an expected choice question with expected answers that will be asked/shown when the command runs.
*
* @param string $question
* @param string $answer
* @param array $answers
* @param bool $strict
* @return $this
*/
public function expectsChoice($question, $answer, $answers, $strict = false)
{
$this->test->expectedChoices[$question] = [
'expected' => $answers,
'strict' => $strict,
];

return $this->expectsQuestion($question, $answer);
}

/**
* Specify output that should be printed when the command runs.
*
Expand Down Expand Up @@ -181,6 +200,10 @@ protected function mockConsoleOutput()
->once()
->ordered()
->with(Mockery::on(function ($argument) use ($question) {
if (isset($this->test->expectedChoices[$question[0]])) {
$this->test->expectedChoices[$question[0]]['actual'] = $argument->getAutocompleterValues();
}

return $argument->getQuestion() == $question[0];
}))
->andReturnUsing(function () use ($question, $i) {
Expand Down

0 comments on commit 0bb7ebc

Please sign in to comment.