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