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 getOptionsConfigFile() |
45
|
|
|
{ |
46
|
|
|
$configPath = getcwd(); |
47
|
|
|
$configFile = $configPath . self::PHPUNIT_WATCH_CONFIG_FILENAME; |
48
|
|
|
|
49
|
|
|
while (! file_exists($configFile)): |
50
|
|
|
$configPath = dirname($configPath); |
51
|
|
|
$configFile = $configPath . self::PHPUNIT_WATCH_CONFIG_FILENAME; |
52
|
|
|
if ($configPath === '/') { |
53
|
|
|
$configFile = self::PHPUNIT_WATCH_CONFIG_FILENAME; |
54
|
|
|
break; |
55
|
|
|
} |
56
|
|
|
endwhile; |
57
|
|
|
|
58
|
|
|
return $configFile; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function getOptionsFromConfigFile(): array |
62
|
|
|
{ |
63
|
|
|
$configFile = $this->getOptionsConfigFile(); |
64
|
|
|
|
65
|
|
|
if (! file_exists($configFile)) { |
66
|
|
|
return []; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return Yaml::parse(file_get_contents($configFile)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function displayOptions(array $options, InputInterface $input, OutputInterface $output) |
73
|
|
|
{ |
74
|
|
|
$output = new SymfonyStyle($input, $output); |
75
|
|
|
|
76
|
|
|
$output->title('PHPUnit Watcher'); |
77
|
|
|
|
78
|
|
|
$output->text("Tests will be rerun when {$options['watch']['fileMask']} files are modified in the following directories:\n"); |
79
|
|
|
|
80
|
|
|
$output->listing($options['watch']['directories']); |
81
|
|
|
|
82
|
|
|
$output->newLine(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|