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