1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\PhpUnitWatcher; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
10
|
|
|
use Symfony\Component\Yaml\Yaml; |
11
|
|
|
|
12
|
|
|
class WatcherCommand extends Command |
13
|
|
|
{ |
14
|
|
|
protected function configure() |
15
|
|
|
{ |
16
|
|
|
$this->setName('watch') |
17
|
|
|
->setDescription('Rerun PHPUnit tests when source code changes.') |
18
|
|
|
->addArgument('phpunit-options', InputArgument::OPTIONAL, 'Options passed to phpunit'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
22
|
|
|
{ |
23
|
|
|
$options = $this->determineOptions($input); |
24
|
|
|
|
25
|
|
|
list($watcher, $options) = WatcherFactory::create($options); |
26
|
|
|
|
27
|
|
|
$this->displayOptions($options, $input, $output); |
28
|
|
|
|
29
|
|
|
$watcher->startWatching(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function determineOptions(InputInterface $input): array |
33
|
|
|
{ |
34
|
|
|
$options = $this->getOptionsFromConfigFile(); |
35
|
|
|
|
36
|
|
|
$options['phpunitArguments'] = trim($input->getArgument('phpunit-options'), "'"); |
37
|
|
|
|
38
|
|
|
return $options; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function getOptionsFromConfigFile(): array |
42
|
|
|
{ |
43
|
|
|
$configFile = getcwd() . '/.phpunit-watcher.yml'; |
44
|
|
|
|
45
|
|
|
if (! file_exists($configFile)) { |
46
|
|
|
return []; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return Yaml::parse(file_get_contents($configFile)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function displayOptions(array $options, InputInterface $input, OutputInterface $output) |
53
|
|
|
{ |
54
|
|
|
$output = new SymfonyStyle($input, $output); |
55
|
|
|
|
56
|
|
|
$output->title("Starting PHPUnit Watcher"); |
57
|
|
|
|
58
|
|
|
$output->text("Tests will be rerun when {$options['watch']['fileMask']} files are modified in"); |
59
|
|
|
|
60
|
|
|
$output->listing($options['watch']['directories']); |
61
|
|
|
|
62
|
|
|
$output->newLine(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|