1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\PhpUnitWatcher; |
4
|
|
|
|
5
|
|
|
use Spatie\PhpUnitWatcher\Exceptions\InvalidConfigfile; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
11
|
|
|
use Symfony\Component\Yaml\Yaml; |
12
|
|
|
|
13
|
|
|
class WatcherCommand extends Command |
14
|
|
|
{ |
15
|
|
|
protected function configure() |
16
|
|
|
{ |
17
|
|
|
$this->setName('watch') |
18
|
|
|
->setDescription('Rerun PHPUnit tests when source code changes.') |
19
|
|
|
->addArgument('phpunit-options', InputArgument::OPTIONAL, 'Options passed to phpunit'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
23
|
|
|
{ |
24
|
|
|
$options = $this->determineOptions($input); |
25
|
|
|
|
26
|
|
|
[$watcher, $options] = WatcherFactory::create($options); |
|
|
|
|
27
|
|
|
|
28
|
|
|
$this->displayOptions($options, $input, $output); |
29
|
|
|
|
30
|
|
|
$watcher->startWatching(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function determineOptions(InputInterface $input): array |
34
|
|
|
{ |
35
|
|
|
$options = $this->getOptionsFromConfigFile(); |
36
|
|
|
|
37
|
|
|
$commandLineArguments = trim($input->getArgument('phpunit-options'), "'"); |
38
|
|
|
|
39
|
|
|
if (! empty($commandLineArguments)) { |
40
|
|
|
$options['phpunit']['arguments'] = $commandLineArguments; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (OS::isOnWindows()) { |
44
|
|
|
$options['hideManual'] = true; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $options; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function getOptionsFromConfigFile(): array |
51
|
|
|
{ |
52
|
|
|
$configFilePath = $this->getConfigFileLocation(); |
53
|
|
|
|
54
|
|
|
if (! file_exists($configFilePath)) { |
55
|
|
|
return []; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$options = Yaml::parse(file_get_contents($configFilePath)); |
59
|
|
|
|
60
|
|
|
if (is_null($options)) { |
61
|
|
|
throw InvalidConfigfile::invalidContents($configFilePath); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$options['configFilePath'] = $configFilePath; |
65
|
|
|
|
66
|
|
|
return $options; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function getConfigFileLocation() |
70
|
|
|
{ |
71
|
|
|
$configNames = [ |
72
|
|
|
'.phpunit-watcher.yml', |
73
|
|
|
'phpunit-watcher.yml', |
74
|
|
|
'phpunit-watcher.yml.dist', |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
$configDirectory = getcwd(); |
78
|
|
|
|
79
|
|
|
while (is_dir($configDirectory)) { |
80
|
|
|
foreach ($configNames as $configName) { |
81
|
|
|
$configFullPath = $configDirectory.DIRECTORY_SEPARATOR.$configName; |
82
|
|
|
|
83
|
|
|
if (file_exists($configFullPath)) { |
84
|
|
|
return $configFullPath; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$parentDirectory = dirname($configDirectory); |
89
|
|
|
|
90
|
|
|
// We do a direct comparison here since there's a difference between |
91
|
|
|
// the root directories on windows / *nix systems which does not |
92
|
|
|
// let us compare it against the DIRECTORY_SEPARATOR directly |
93
|
|
|
if ($parentDirectory === $configDirectory) { |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$configDirectory = $parentDirectory; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function displayOptions(array $options, InputInterface $input, OutputInterface $output) |
102
|
|
|
{ |
103
|
|
|
$output = new SymfonyStyle($input, $output); |
104
|
|
|
|
105
|
|
|
$output->title('PHPUnit Watcher'); |
106
|
|
|
|
107
|
|
|
$output->text("PHPUnit Watcher {$this->getApplication()->getVersion()} by Spatie and contributors."); |
108
|
|
|
$output->newLine(); |
109
|
|
|
|
110
|
|
|
if (isset($options['configFilePath'])) { |
111
|
|
|
$output->text("Using options from configfile at `{$options['configFilePath']}`"); |
112
|
|
|
} else { |
113
|
|
|
$output->text('No config file detected. Using default options.'); |
114
|
|
|
} |
115
|
|
|
$output->newLine(); |
116
|
|
|
|
117
|
|
|
$output->text("Tests will be rerun when {$options['watch']['fileMask']} files are modified in"); |
118
|
|
|
|
119
|
|
|
$output->listing($options['watch']['directories']); |
120
|
|
|
|
121
|
|
|
$output->newLine(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.