|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\PhpUnitWatcher\Screens; |
|
4
|
|
|
|
|
5
|
|
|
use Spatie\PhpUnitWatcher\Notification; |
|
6
|
|
|
use Spatie\PhpUnitWatcher\Notifier; |
|
7
|
|
|
use Symfony\Component\Process\Process; |
|
8
|
|
|
|
|
9
|
|
|
class Phpunit extends Screen |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var array */ |
|
12
|
|
|
protected $options; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct(array $options) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->options = $options; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function draw() |
|
20
|
|
|
{ |
|
21
|
|
|
$this |
|
22
|
|
|
->writeHeader() |
|
23
|
|
|
->runTests() |
|
24
|
|
|
->displayManual(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function registerListeners() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->terminal->onKeyPress(function ($line) { |
|
30
|
|
|
$line = strtolower($line); |
|
31
|
|
|
|
|
32
|
|
|
switch ($line) { |
|
33
|
|
|
case '': |
|
34
|
|
|
$this->terminal->refreshScreen(); |
|
35
|
|
|
break; |
|
36
|
|
|
case 'a': |
|
37
|
|
|
$this->terminal->displayScreen(new Phpunit()); |
|
|
|
|
|
|
38
|
|
|
break; |
|
39
|
|
|
case 't': |
|
40
|
|
|
$this->terminal->displayScreen(new FilterTestName()); |
|
41
|
|
|
break; |
|
42
|
|
|
case 'p': |
|
43
|
|
|
$this->terminal->displayScreen(new FilterFileName()); |
|
44
|
|
|
break; |
|
45
|
|
|
case 'q': |
|
46
|
|
|
die(); |
|
|
|
|
|
|
47
|
|
|
break; |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
}); |
|
50
|
|
|
|
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function writeHeader() |
|
55
|
|
|
{ |
|
56
|
|
|
$title = 'Starting PHPUnit'; |
|
57
|
|
|
|
|
58
|
|
|
if (! empty($this->options['phpunitArguments'])) { |
|
59
|
|
|
$title .= " with arguments: `{$this->options['phpunitArguments']}`"; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->terminal |
|
63
|
|
|
->comment($title) |
|
64
|
|
|
->emptyLine(); |
|
65
|
|
|
|
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
protected function runTests() |
|
70
|
|
|
{ |
|
71
|
|
|
$result = (new Process("vendor/bin/phpunit {$this->options['phpunitArguments']}")) |
|
72
|
|
|
->setTty(true) |
|
73
|
|
|
->run(function ($type, $line) { |
|
74
|
|
|
echo $line; |
|
75
|
|
|
}); |
|
76
|
|
|
|
|
77
|
|
|
$this->sendDesktopNotification($result); |
|
78
|
|
|
|
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function displayManual() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->terminal |
|
85
|
|
|
->emptyLine() |
|
86
|
|
|
->write('Press a to run all tests.') |
|
87
|
|
|
->write('Press t to filter by test name.') |
|
88
|
|
|
->write('Press p to filter by file name.') |
|
89
|
|
|
->write('Press q to quit the watcher.') |
|
90
|
|
|
->write('Press Enter to trigger a test run.'); |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
protected function sendDesktopNotification(int $result) |
|
96
|
|
|
{ |
|
97
|
|
|
$notificationName = $result === 0 |
|
98
|
|
|
? 'passingTests' |
|
99
|
|
|
: 'failingTests'; |
|
100
|
|
|
|
|
101
|
|
|
if ($this->options['notifications'][$notificationName]) { |
|
102
|
|
|
Notification::create()->$notificationName(); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
This check looks for function calls that miss required arguments.