|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DF\PHPCoverFish; |
|
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\Input\InputOption; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
use DF\PHPCoverFish\Exception\CoverFishFailExit; |
|
11
|
|
|
use DF\PHPCoverFish\Common\CoverFishHelper; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class CoverFishScanCommand |
|
15
|
|
|
* |
|
16
|
|
|
* @package DF\PHPCoverFish |
|
17
|
|
|
* @author Patrick Paechnatz <[email protected]> |
|
18
|
|
|
* @copyright 2015 Patrick Paechnatz <[email protected]> |
|
19
|
|
|
* @license http://www.opensource.org/licenses/MIT |
|
20
|
|
|
* @link http://github.com/dunkelfrosch/phpcoverfish/tree |
|
21
|
|
|
* @since class available since Release 0.9.0 |
|
22
|
|
|
* @version 1.0.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
class CoverFishScanCommand extends Command |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var CoverFishHelper |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $coverFishHelper; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* additional options and arguments for our cli application |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function configure() |
|
35
|
|
|
{ |
|
36
|
|
|
$this |
|
37
|
|
|
->setName('scan') |
|
38
|
|
|
->setDescription('scan phpunit test files for static code analysis') |
|
39
|
|
|
->setHelp($this->getHelpOutput()) |
|
40
|
|
|
->addArgument( |
|
41
|
|
|
'phpunit-config', |
|
42
|
|
|
InputArgument::OPTIONAL, |
|
43
|
|
|
'the source path of your corresponding phpunit xml config file (e.g. ./tests/phpunit.xml)' |
|
44
|
|
|
) |
|
45
|
|
|
->addOption( |
|
46
|
|
|
'phpunit-config-suite', |
|
47
|
|
|
null, |
|
48
|
|
|
InputOption::VALUE_OPTIONAL, |
|
49
|
|
|
'name of the target test suite inside your php config xml file, this test suite will be scanned' |
|
50
|
|
|
) |
|
51
|
|
|
->addOption( |
|
52
|
|
|
'raw-scan-path', |
|
53
|
|
|
null, |
|
54
|
|
|
InputOption::VALUE_OPTIONAL, |
|
55
|
|
|
'raw mode option: the source path of your corresponding phpunit test files or a specific testFile (e.g. tests/), this option will always override phpunit.xml settings!' |
|
56
|
|
|
) |
|
57
|
|
|
->addOption( |
|
58
|
|
|
'raw-autoload-file', |
|
59
|
|
|
null, |
|
60
|
|
|
InputOption::VALUE_OPTIONAL, |
|
61
|
|
|
'raw-mode option: your application autoload file and path (e.g. ../app/autoload.php for running in symfony context), this option will always override phpunit.xml settings!' |
|
62
|
|
|
) |
|
63
|
|
|
->addOption( |
|
64
|
|
|
'raw-exclude-path', |
|
65
|
|
|
null, |
|
66
|
|
|
InputOption::VALUE_OPTIONAL, |
|
67
|
|
|
'raw-mode option: exclude a specific path from planned scan', |
|
68
|
|
|
null |
|
69
|
|
|
) |
|
70
|
|
|
->addOption( |
|
71
|
|
|
'output-format', |
|
72
|
|
|
'f', |
|
73
|
|
|
InputOption::VALUE_OPTIONAL, |
|
74
|
|
|
'output format of scan result (json|text)', |
|
75
|
|
|
'text' |
|
76
|
|
|
) |
|
77
|
|
|
->addOption( |
|
78
|
|
|
'output-level', |
|
79
|
|
|
'l', |
|
80
|
|
|
InputOption::VALUE_OPTIONAL, |
|
81
|
|
|
'level of output information (0:minimal, 1: normal (default), 2: detailed)', |
|
82
|
|
|
1 |
|
83
|
|
|
) |
|
84
|
|
|
->addOption( |
|
85
|
|
|
'stop-on-error', |
|
86
|
|
|
null, |
|
87
|
|
|
InputOption::VALUE_OPTIONAL, |
|
88
|
|
|
'stop on first application error raises', |
|
89
|
|
|
false |
|
90
|
|
|
) |
|
91
|
|
|
->addOption( |
|
92
|
|
|
'stop-on-failure', |
|
93
|
|
|
null, |
|
94
|
|
|
InputOption::VALUE_OPTIONAL, |
|
95
|
|
|
'stop on first detected coverFish failure raises', |
|
96
|
|
|
false |
|
97
|
|
|
) |
|
98
|
|
|
; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @codeCoverageIgnore |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getHelpOutput() |
|
107
|
|
|
{ |
|
108
|
|
|
// print out some "phpUnit-Mode" runtime samples |
|
109
|
|
|
$help = sprintf('%sscan by using your "phpunit.xml" config-file inside "Tests/" directory and using test suite "My Test Suite" directly with normal output-level and no ansi-colors:%s', PHP_EOL, PHP_EOL); |
|
110
|
|
|
$help .= sprintf('<comment>php</comment> <info>./bin/coverfish</info> <info>scan</info> <comment>./Tests/phpunit.xml</comment> --phpunit-config-suite "<comment>My Test Suite</comment>" --output-level <comment>1</comment> --no-ansi%s', PHP_EOL); |
|
111
|
|
|
$help .= sprintf('%sscan by using your "phpunit.xml" config-file inside "Tests/" directory without any given testSuite (so first suite will taken) with normal output-level and no ansi-colors:%s', PHP_EOL, PHP_EOL); |
|
112
|
|
|
$help .= sprintf('<comment>php</comment> <info>./bin/coverfish</info> <info>scan</info> <comment>./Tests/phpunit.xml</comment> --output-level <comment>1</comment> --no-ansi%s', PHP_EOL); |
|
113
|
|
|
$help .= sprintf('%ssame scan with maximum output-level and disabled ansi output:%s', PHP_EOL, PHP_EOL); |
|
114
|
|
|
$help .= sprintf('<comment>php</comment> <info>./bin/coverfish</info> <info>scan</info> <comment>./Tests/phpunit.xml</comment> --output-level <comment>2</comment>%s', PHP_EOL); |
|
115
|
|
|
|
|
116
|
|
|
// print out some "raw-Mode" runtime samples |
|
117
|
|
|
$help .= sprintf('%sscan by using raw-mode, using "Tests/" directory as base scan path, autoload file "vendor/autoload.php" and exclude "Tests/data/" with normal output-level and no ansi-colors:%s', PHP_EOL, PHP_EOL); |
|
118
|
|
|
$help .= sprintf('<comment>php</comment> <info>./bin/coverfish</info> <info>scan</info> --raw-scan-path <comment>./Tests/phpunit.xml</comment> --raw-autoload-file <comment>vendor/autoload.php</comment> --raw-exclude-path <comment>Tests/data</comment> --output-level <comment>1</comment> --no-ansi%s', PHP_EOL); |
|
119
|
|
|
|
|
120
|
|
|
return $help; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @codeCoverageIgnore |
|
125
|
|
|
* |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getVersion() |
|
129
|
|
|
{ |
|
130
|
|
|
return sprintf('%s.%s.%s', |
|
131
|
|
|
CoverFishScanner::APP_VERSION_MAJOR, |
|
132
|
|
|
CoverFishScanner::APP_VERSION_MINOR, |
|
133
|
|
|
CoverFishScanner::APP_VERSION_BUILD |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @codeCoverageIgnore |
|
139
|
|
|
* |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getLongVersion() |
|
143
|
|
|
{ |
|
144
|
|
|
return sprintf('v%s [%s]', $this->getVersion(), CoverFishScanner::APP_RELEASE_STATE); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param InputInterface $input |
|
149
|
|
|
* @param OutputInterface $output |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function showExecTitle(InputInterface $input, OutputInterface $output) |
|
152
|
|
|
{ |
|
153
|
|
|
/** @var string $outputLevelMsg */ |
|
154
|
|
|
$outputLevelMsg = 'minimal'; |
|
155
|
|
|
switch ((int) $input->getOption('output-level')) { |
|
156
|
|
|
case 1: |
|
157
|
|
|
$outputLevelMsg = 'moderate'; |
|
158
|
|
|
break; |
|
159
|
|
|
case 2: |
|
160
|
|
|
$outputLevelMsg = 'maximum'; |
|
161
|
|
|
break; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$output->writeln( |
|
165
|
|
|
sprintf('<info>%s</info> <comment>%s</comment>%sstart scan process using %s output level', |
|
166
|
|
|
CoverFishScanner::APP_RELEASE_NAME, |
|
167
|
|
|
$this->getLongVersion(), |
|
168
|
|
|
PHP_EOL, |
|
169
|
|
|
$outputLevelMsg |
|
170
|
|
|
) |
|
171
|
|
|
); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* exec command "scan" |
|
176
|
|
|
* |
|
177
|
|
|
* @param InputInterface $input |
|
178
|
|
|
* @param OutputInterface $output |
|
179
|
|
|
* |
|
180
|
|
|
* @return string |
|
181
|
|
|
* @throws \Exception |
|
182
|
|
|
*/ |
|
183
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
184
|
|
|
{ |
|
185
|
|
|
$this->showExecTitle($input, $output); |
|
186
|
|
|
$this->prepareExecute($input); |
|
187
|
|
|
|
|
188
|
|
|
$cliOptions = array( |
|
189
|
|
|
'sys_phpunit_config' => $input->getArgument('phpunit-config'), |
|
190
|
|
|
'sys_phpunit_config_test_suite' => $input->getOption('phpunit-config-suite'), |
|
191
|
|
|
'sys_stop_on_error' => $input->getOption('stop-on-error'), |
|
192
|
|
|
'sys_stop_on_failure' => $input->getOption('stop-on-failure'), |
|
193
|
|
|
'raw_scan_source' => $input->getOption('raw-scan-path'), |
|
194
|
|
|
'raw_scan_autoload_file' => $input->getOption('raw-autoload-file'), |
|
195
|
|
|
'raw_scan_exclude_path' => $input->getOption('raw-exclude-path'), |
|
196
|
|
|
); |
|
197
|
|
|
|
|
198
|
|
|
$outOptions = array( |
|
199
|
|
|
'out_verbose' => $input->getOption('verbose'), |
|
200
|
|
|
'out_format' => $input->getOption('output-format'), |
|
201
|
|
|
'out_level' => (int) $input->getOption('output-level'), |
|
202
|
|
|
'out_no_ansi' => $input->getOption('no-ansi'), |
|
203
|
|
|
'out_no_echo' => $input->getOption('quiet'), |
|
204
|
|
|
); |
|
205
|
|
|
|
|
206
|
|
|
try { |
|
207
|
|
|
$scanner = new CoverFishScanner($cliOptions, $outOptions, $output); |
|
208
|
|
|
$scanner->analysePHPUnitFiles(); |
|
209
|
|
|
} catch (CoverFishFailExit $e) { |
|
210
|
|
|
return CoverFishFailExit::RETURN_CODE_SCAN_FAIL; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return 0; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* prepare exec of command "scan" |
|
218
|
|
|
* |
|
219
|
|
|
* @param InputInterface $input |
|
220
|
|
|
* |
|
221
|
|
|
* @throws \Exception |
|
222
|
|
|
*/ |
|
223
|
|
|
public function prepareExecute(InputInterface $input) |
|
224
|
|
|
{ |
|
225
|
|
|
$this->coverFishHelper = new CoverFishHelper(); |
|
226
|
|
|
|
|
227
|
|
|
$phpUnitConfigFile = $input->getArgument('phpunit-config'); |
|
228
|
|
View Code Duplication |
if (false === empty($phpUnitConfigFile) && |
|
229
|
|
|
false === $this->coverFishHelper->checkFileOrPath($phpUnitConfigFile)) { |
|
230
|
|
|
throw new \Exception(sprintf('phpunit config file "%s" not found! please define your phpunit.xml config file to use (e.g. tests/phpunit.xml)', $phpUnitConfigFile)); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
$testPathOrFile = $input->getOption('raw-scan-path'); |
|
234
|
|
View Code Duplication |
if (false === empty($testPathOrFile) && |
|
235
|
|
|
false === $this->coverFishHelper->checkFileOrPath($testPathOrFile)) { |
|
236
|
|
|
throw new \Exception(sprintf('test path/file "%s" not found! please define test file path (e.g. tests/)', $testPathOrFile)); |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
} |