1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\PhpUnitWatcher\Screens; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Process\Process; |
6
|
|
|
use Spatie\PhpUnitWatcher\Notification; |
7
|
|
|
|
8
|
|
|
class Phpunit extends Screen |
9
|
|
|
{ |
10
|
|
|
const DEFAULT_BINARY_PATH = 'vendor/bin/phpunit'; |
11
|
|
|
|
12
|
|
|
/** @var array */ |
13
|
|
|
public $options; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
protected $phpunitArguments; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
private $phpunitBinaryPath; |
20
|
|
|
|
21
|
|
|
public function __construct(array $options) |
22
|
|
|
{ |
23
|
|
|
$this->options = $options; |
24
|
|
|
|
25
|
|
|
$this->phpunitArguments = $options['phpunit']['arguments'] ?? ''; |
26
|
|
|
|
27
|
|
|
$this->phpunitBinaryPath = $options['phpunit']['binaryPath'] ?? self::DEFAULT_BINARY_PATH; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function draw(array $changedFilePaths = []) |
31
|
|
|
{ |
32
|
|
|
$this->determineAutoFilter($changedFilePaths); |
33
|
|
|
|
34
|
|
|
if (! $this->options['autoFilter'] || ($this->options['autoFilter'] && ! empty($changedFilePaths))) { |
35
|
|
|
$this |
36
|
|
|
->writeHeader() |
37
|
|
|
->runTests(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (! $this->options['autoFilter']) { |
41
|
|
|
$this->displayManual(); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function registerListeners() |
46
|
|
|
{ |
47
|
|
|
if ($this->options['autoFilter']) { |
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->terminal->onKeyPress(function ($line) { |
52
|
|
|
$line = strtolower($line); |
53
|
|
|
|
54
|
|
|
switch ($line) { |
55
|
|
|
case '': |
56
|
|
|
$this->terminal->refreshScreen(); |
57
|
|
|
break; |
58
|
|
|
case 'a': |
59
|
|
|
$this->options['phpunit']['arguments'] = ''; |
60
|
|
|
|
61
|
|
|
$this->terminal->displayScreen(new Phpunit($this->options)); |
62
|
|
|
break; |
63
|
|
|
case 'g': |
64
|
|
|
$this->terminal->displayScreen(new FilterGroupName()); |
65
|
|
|
break; |
66
|
|
|
case 's': |
67
|
|
|
$this->terminal->displayScreen(new FilterTestSuiteName()); |
68
|
|
|
break; |
69
|
|
|
case 't': |
70
|
|
|
$this->terminal->displayScreen(new FilterTestName()); |
71
|
|
|
break; |
72
|
|
|
case 'p': |
73
|
|
|
$this->terminal->displayScreen(new FilterFileName()); |
74
|
|
|
break; |
75
|
|
|
case 'q': |
76
|
|
|
die(); |
77
|
|
|
break; |
|
|
|
|
78
|
|
|
default: |
79
|
|
|
$this->registerListeners(); |
80
|
|
|
break; |
81
|
|
|
} |
82
|
|
|
}); |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function writeHeader() |
88
|
|
|
{ |
89
|
|
|
$title = 'Starting PHPUnit'; |
90
|
|
|
|
91
|
|
|
if (! empty($this->phpunitArguments)) { |
92
|
|
|
$title .= " with arguments: `{$this->phpunitArguments}`"; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->terminal |
96
|
|
|
->comment($title) |
97
|
|
|
->emptyLine(); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function runTests() |
103
|
|
|
{ |
104
|
|
|
$result = (new Process("{$this->phpunitBinaryPath} {$this->phpunitArguments}")) |
105
|
|
|
->setTty(true) |
106
|
|
|
->run(function ($type, $line) { |
107
|
|
|
echo $line; |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
$this->sendDesktopNotification($result); |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
protected function displayManual() |
116
|
|
|
{ |
117
|
|
|
if ($this->options['hideManual']) { |
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$this->terminal |
122
|
|
|
->emptyLine() |
123
|
|
|
->write('<dim>Press </dim>a<dim> to run all tests.</dim>') |
124
|
|
|
->write('<dim>Press </dim>t<dim> to filter by test name.</dim>') |
125
|
|
|
->write('<dim>Press </dim>p<dim> to filter by file name.</dim>') |
126
|
|
|
->write('<dim>Press </dim>g<dim> to filter by group name.</dim>') |
127
|
|
|
->write('<dim>Press </dim>s<dim> to filter by test suite name.</dim>') |
128
|
|
|
->write('<dim>Press </dim>q<dim> to quit the watcher.</dim>') |
129
|
|
|
->write('<dim>Press </dim>Enter<dim> to trigger a test run.</dim>'); |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function sendDesktopNotification(int $result) |
135
|
|
|
{ |
136
|
|
|
$notificationName = $result === 0 |
137
|
|
|
? 'passingTests' |
138
|
|
|
: 'failingTests'; |
139
|
|
|
|
140
|
|
|
if ($this->options['notifications'][$notificationName]) { |
141
|
|
|
Notification::create()->$notificationName(); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function determineAutoFilter(array $changedFilePaths = []) |
146
|
|
|
{ |
147
|
|
|
$autoFilterOption = null; |
|
|
|
|
148
|
|
|
$this->phpunitArguments = isset($this->options['phpunit']['arguments']) ? $this->options['phpunit']['arguments'] : ''; |
149
|
|
|
|
150
|
|
|
// Apply a filter based on the changed files |
151
|
|
|
if (! empty($changedFilePaths)) { |
152
|
|
|
$testNames = array_map(function ($filePath) { |
153
|
|
|
$filePathParts = explode('/', $filePath); |
154
|
|
|
$fileName = end($filePathParts); |
155
|
|
|
$fileNameParts = explode('.', $fileName); |
156
|
|
|
|
157
|
|
|
$testName = current($fileNameParts); |
158
|
|
|
|
159
|
|
|
// Suffix with "Test" if it's not already a test |
160
|
|
|
$strlen = strlen($testName); |
161
|
|
|
if ($strlen < 4 || ! (substr_compare(strtolower($testName), 'test', $strlen - 4, 4) === 0)) { |
162
|
|
|
$testName .= 'Test'; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $testName; |
166
|
|
|
}, $changedFilePaths); |
167
|
|
|
|
168
|
|
|
$testFilterPattern = '/('.implode('|', $testNames).')/'; |
169
|
|
|
$autoFilterOption = " --filter=\"$testFilterPattern\""; |
170
|
|
|
|
171
|
|
|
$this->phpunitArguments .= $autoFilterOption; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function getPhpunitArguments() |
178
|
|
|
{ |
179
|
|
|
return isset($this->phpunitArguments) ? $this->phpunitArguments : null; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.