1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\PhpUnitWatcher\Screens; |
4
|
|
|
|
5
|
|
|
use Spatie\PhpUnitWatcher\Notification; |
6
|
|
|
use Symfony\Component\Process\Process; |
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
|
|
|
/** @var int */ |
22
|
|
|
private $phpunitTimeout; |
23
|
|
|
|
24
|
|
|
public function __construct(array $options) |
25
|
|
|
{ |
26
|
|
|
$this->options = $options; |
27
|
|
|
|
28
|
|
|
$this->phpunitArguments = $options['phpunit']['arguments'] ?? ''; |
29
|
|
|
$this->phpunitBinaryPath = $options['phpunit']['binaryPath'] ?? self::DEFAULT_BINARY_PATH; |
30
|
|
|
$this->phpunitTimeout = $options['phpunit']['timeout'] ?? 60; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function draw() |
34
|
|
|
{ |
35
|
|
|
$this |
36
|
|
|
->writeHeader() |
37
|
|
|
->runTests() |
38
|
|
|
->displayManual(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function registerListeners() |
42
|
|
|
{ |
43
|
|
|
$this->terminal->onKeyPress(function ($line) { |
44
|
|
|
$line = strtolower($line); |
45
|
|
|
|
46
|
|
|
switch ($line) { |
47
|
|
|
case '': |
48
|
|
|
$this->terminal->refreshScreen(); |
49
|
|
|
break; |
50
|
|
|
case 'a': |
51
|
|
|
$this->options['phpunit']['arguments'] = ''; |
52
|
|
|
|
53
|
|
|
$this->terminal->displayScreen(new self($this->options)); |
54
|
|
|
break; |
55
|
|
|
case 'g': |
56
|
|
|
$this->terminal->displayScreen(new FilterGroupName()); |
57
|
|
|
break; |
58
|
|
|
case 's': |
59
|
|
|
$this->terminal->displayScreen(new FilterTestSuiteName()); |
60
|
|
|
break; |
61
|
|
|
case 't': |
62
|
|
|
$this->terminal->displayScreen(new FilterTestName()); |
63
|
|
|
break; |
64
|
|
|
case 'p': |
65
|
|
|
$this->terminal->displayScreen(new FilterFileName()); |
66
|
|
|
break; |
67
|
|
|
case 'r': |
68
|
|
|
$this->terminal->displayScreen(new RandomSeed()); |
69
|
|
|
break; |
70
|
|
|
case 'q': |
71
|
|
|
die(); |
72
|
|
|
break; |
|
|
|
|
73
|
|
|
default: |
74
|
|
|
$this->registerListeners(); |
75
|
|
|
break; |
76
|
|
|
} |
77
|
|
|
}); |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function writeHeader() |
83
|
|
|
{ |
84
|
|
|
$title = 'Starting PHPUnit'; |
85
|
|
|
|
86
|
|
|
if (! empty($this->phpunitArguments)) { |
87
|
|
|
$title .= " with arguments: `{$this->phpunitArguments}`"; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->terminal |
91
|
|
|
->comment($title) |
92
|
|
|
->emptyLine(); |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function runTests() |
98
|
|
|
{ |
99
|
|
|
$result = (new Process(array_merge([$this->phpunitBinaryPath], explode(' ', $this->phpunitArguments)))) |
100
|
|
|
->setTimeout($this->phpunitTimeout) |
101
|
|
|
->setTty(Process::isTtySupported()) |
102
|
|
|
->run(function ($type, $line) { |
103
|
|
|
echo $line; |
104
|
|
|
}); |
105
|
|
|
|
106
|
|
|
$this->sendDesktopNotification($result); |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function displayManual() |
112
|
|
|
{ |
113
|
|
|
if ($this->options['hideManual']) { |
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$this->terminal |
118
|
|
|
->emptyLine() |
119
|
|
|
->write('<dim>Press </dim>a<dim> to run all tests.</dim>') |
120
|
|
|
->write('<dim>Press </dim>t<dim> to filter by test name.</dim>') |
121
|
|
|
->write('<dim>Press </dim>p<dim> to filter by file name.</dim>') |
122
|
|
|
->write('<dim>Press </dim>g<dim> to filter by group name.</dim>') |
123
|
|
|
->write('<dim>Press </dim>s<dim> to filter by test suite name.</dim>') |
124
|
|
|
->write('<dim>Press </dim>r<dim> to run tests with a random seed.</dim>') |
125
|
|
|
->write('<dim>Press </dim>q<dim> to quit the watcher.</dim>') |
126
|
|
|
->write('<dim>Press </dim>Enter<dim> to trigger a test run.</dim>'); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function sendDesktopNotification(int $result) |
132
|
|
|
{ |
133
|
|
|
$notificationName = $result === 0 |
134
|
|
|
? 'passingTests' |
135
|
|
|
: 'failingTests'; |
136
|
|
|
|
137
|
|
|
if ($this->options['notifications'][$notificationName]) { |
138
|
|
|
Notification::create()->$notificationName(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.