forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InteractsWithConsole.php
91 lines (76 loc) · 2.29 KB
/
InteractsWithConsole.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace Illuminate\Foundation\Testing\Concerns;
use Illuminate\Console\OutputStyle;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Arr;
use Illuminate\Testing\PendingCommand;
trait InteractsWithConsole
{
/**
* Indicates if the console output should be mocked.
*
* @var bool
*/
public $mockConsoleOutput = true;
/**
* All of the expected output lines.
*
* @var array
*/
public $expectedOutput = [];
/**
* All of the expected questions.
*
* @var array
*/
public $expectedQuestions = [];
/**
* All of the expected choice questions.
*
* @var array
*/
public $expectedChoices = [];
/**
* Call artisan command and return code.
*
* @param string $command
* @param array $parameters
* @return \Illuminate\Testing\PendingCommand|int
*/
public function artisan($command, $parameters = [])
{
if (! $this->mockConsoleOutput) {
return $this->app[Kernel::class]->call($command, $parameters);
}
$this->beforeApplicationDestroyed(function () {
if (count($this->expectedQuestions)) {
$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.');
}
});
return new PendingCommand($this, $this->app, $command, $parameters);
}
/**
* Disable mocking the console output.
*
* @return $this
*/
protected function withoutMockingConsoleOutput()
{
$this->mockConsoleOutput = false;
$this->app->offsetUnset(OutputStyle::class);
return $this;
}
}