1
|
|
|
<?php |
2
|
|
|
namespace ParaTest\Console\Testers; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Command\Command; |
5
|
|
|
use Symfony\Component\Console\Input\InputOption; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use ParaTest\Runners\PHPUnit\Configuration; |
10
|
|
|
use ParaTest\Runners\PHPUnit\Runner; |
11
|
|
|
use ParaTest\Runners\PHPUnit\WrapperRunner; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class PHPUnit |
15
|
|
|
* |
16
|
|
|
* Creates the interface for PHPUnit testing |
17
|
|
|
* |
18
|
|
|
* @package ParaTest\Console\Testers |
19
|
|
|
*/ |
20
|
|
|
class PHPUnit extends Tester |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var \ParaTest\Console\Commands\ParaTestCommand |
24
|
|
|
*/ |
25
|
|
|
protected $command; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Configures the ParaTestCommand with PHPUnit specific |
29
|
|
|
* definitions |
30
|
|
|
* |
31
|
|
|
* @param Command $command |
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
8 |
|
public function configure(Command $command) |
35
|
|
|
{ |
36
|
|
|
$command |
37
|
8 |
|
->addOption('phpunit', null, InputOption::VALUE_REQUIRED, 'The PHPUnit binary to execute. <comment>(default: vendor/bin/phpunit)</comment>') |
38
|
8 |
|
->addOption('runner', null, InputOption::VALUE_REQUIRED, 'Runner or WrapperRunner. <comment>(default: Runner)</comment>') |
39
|
8 |
|
->addOption('bootstrap', null, InputOption::VALUE_REQUIRED, 'The bootstrap file to be used by PHPUnit.') |
40
|
8 |
|
->addOption('configuration', 'c', InputOption::VALUE_REQUIRED, 'The PHPUnit configuration file to use.') |
41
|
8 |
|
->addOption('group', 'g', InputOption::VALUE_REQUIRED, 'Only runs tests from the specified group(s).') |
42
|
8 |
|
->addOption('exclude-group', null, InputOption::VALUE_REQUIRED, 'Don\'t run tests from the specified group(s).') |
43
|
8 |
|
->addOption('stop-on-failure', null, InputOption::VALUE_NONE, 'Don\'t start any more processes after a failure.') |
44
|
8 |
|
->addOption('log-junit', null, InputOption::VALUE_REQUIRED, 'Log test execution in JUnit XML format to file.') |
45
|
8 |
|
->addOption('colors', null, InputOption::VALUE_NONE, 'Displays a colored bar as a test result.') |
46
|
8 |
|
->addOption('testsuite', null, InputOption::VALUE_OPTIONAL, 'Filter which testsuite to run') |
47
|
8 |
|
->addArgument('path', InputArgument::OPTIONAL, 'The path to a directory or file containing tests. <comment>(default: current directory)</comment>') |
48
|
8 |
|
->addOption('path', null, InputOption::VALUE_REQUIRED, 'An alias for the path argument.'); |
49
|
8 |
|
$this->command = $command; |
|
|
|
|
50
|
8 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Executes the PHPUnit Runner. Will Display help if no config and no path |
54
|
|
|
* supplied |
55
|
|
|
* |
56
|
|
|
* @param InputInterface $input |
57
|
|
|
* @param OutputInterface $output |
58
|
|
|
* @return int|mixed |
59
|
|
|
*/ |
60
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
61
|
|
|
{ |
62
|
|
|
if (!$this->hasConfig($input) && !$this->hasPath($input)) { |
63
|
|
|
$this->displayHelp($input, $output); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($input->getOption('runner') === 'WrapperRunner') { |
67
|
|
|
$runner = new WrapperRunner($this->getRunnerOptions($input)); |
68
|
|
|
} else { |
69
|
|
|
if ($input->getOption('runner') !== '') { |
70
|
|
|
// because we want to have to bootstrap script inherited before check/initialization |
71
|
|
|
$runnerOption = $this->getRunnerOptions($input); |
72
|
|
|
$runnerClass = $input->getOption('runner'); |
73
|
|
|
if (class_exists($runnerClass)) { |
74
|
|
|
$runner = new $runnerClass($runnerOption); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (!isset($runner)) { |
80
|
|
|
$runner = new Runner($this->getRunnerOptions($input)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$runner->run(); |
84
|
|
|
return $runner->getExitCode(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns whether or not a test path has been supplied |
89
|
|
|
* via option or regular input |
90
|
|
|
* |
91
|
|
|
* @param InputInterface $input |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
protected function hasPath(InputInterface $input) |
95
|
|
|
{ |
96
|
|
|
$argument = $input->getArgument('path'); |
97
|
|
|
$option = $input->getOption('path'); |
98
|
|
|
return $argument || $option; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Is there a PHPUnit xml configuration present |
103
|
|
|
* |
104
|
|
|
* @param InputInterface $input |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
4 |
|
protected function hasConfig(InputInterface $input) |
108
|
|
|
{ |
109
|
4 |
|
return (false !== $this->getConfig($input)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
114
|
|
|
* @return \ParaTest\Runners\PHPUnit\Configuration|boolean |
115
|
|
|
*/ |
116
|
4 |
|
protected function getConfig(InputInterface $input) |
117
|
|
|
{ |
118
|
4 |
|
$cwd = getcwd() . DIRECTORY_SEPARATOR; |
119
|
|
|
|
120
|
4 |
|
if ($input->getOption('configuration')) { |
121
|
|
|
$configFilename = $input->getOption('configuration'); |
122
|
4 |
|
} elseif (file_exists($cwd . 'phpunit.xml.dist')) { |
123
|
4 |
|
$configFilename = $cwd . 'phpunit.xml.dist'; |
124
|
4 |
|
} elseif (file_exists($cwd . 'phpunit.xml')) { |
125
|
|
|
$configFilename = $cwd . 'phpunit.xml'; |
126
|
|
|
} else { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
4 |
|
return new Configuration($configFilename); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
135
|
|
|
* @return array |
136
|
|
|
* @throws \RuntimeException |
137
|
|
|
*/ |
138
|
4 |
|
public function getRunnerOptions(InputInterface $input) |
139
|
|
|
{ |
140
|
4 |
|
$path = $input->getArgument('path'); |
141
|
4 |
|
$options = $this->getOptions($input); |
142
|
4 |
|
$bootstrap = $this->getBootstrapFile($input, $options); |
143
|
4 |
|
$this->requireBootstrap($bootstrap); |
144
|
|
|
|
145
|
4 |
|
if ($this->hasCoverage($options)) { |
146
|
2 |
|
$options['coverage-php'] = sys_get_temp_dir() . '/will_be_overwritten.php'; |
147
|
2 |
|
} |
148
|
|
|
|
149
|
4 |
|
if ($path) { |
150
|
4 |
|
$options = array_merge(array('path' => $path), $options); |
151
|
4 |
|
} |
152
|
|
|
|
153
|
4 |
|
return $options; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Require the bootstrap. If the file is specified, but does not exist |
158
|
|
|
* then an exception will be raised. |
159
|
|
|
* |
160
|
|
|
* @param $file |
161
|
|
|
* @throws \RuntimeException |
162
|
|
|
*/ |
163
|
5 |
|
public function requireBootstrap($file) |
164
|
|
|
{ |
165
|
5 |
|
if (! $file) { |
166
|
|
|
return; |
167
|
|
|
} |
168
|
|
|
|
169
|
5 |
|
if (! file_exists($file)) { |
170
|
|
|
$message = sprintf('Bootstrap specified but could not be found (%s)', $file); |
171
|
|
|
throw new \RuntimeException($message); |
172
|
|
|
} |
173
|
|
|
|
174
|
5 |
|
$this->scopedRequire($file); |
175
|
5 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* This function limits the scope of a required file |
179
|
|
|
* so that variables defined in it do not break |
180
|
|
|
* this object's configuration. |
181
|
|
|
*/ |
182
|
5 |
|
protected function scopedRequire($file) |
183
|
|
|
{ |
184
|
5 |
|
$cwd = getcwd(); |
185
|
5 |
|
require_once $file; |
186
|
5 |
|
chdir($cwd); |
187
|
5 |
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Return whether or not code coverage information should be collected. |
191
|
|
|
* |
192
|
|
|
* @param $options |
193
|
|
|
* @return bool |
194
|
|
|
*/ |
195
|
4 |
|
protected function hasCoverage($options) |
196
|
|
|
{ |
197
|
4 |
|
$isFileFormat = isset($options['coverage-html']) || isset($options['coverage-clover']); |
198
|
4 |
|
$isPHP = isset($options['coverage-php']); |
199
|
4 |
|
return $isFileFormat && ! $isPHP; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Fetch the path to the bootstrap file. |
204
|
|
|
* |
205
|
|
|
* @param InputInterface $input |
206
|
|
|
* @param array $options |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
4 |
|
protected function getBootstrapFile(InputInterface $input, array $options) |
210
|
|
|
{ |
211
|
4 |
|
if (isset($options['bootstrap'])) { |
212
|
|
|
return $options['bootstrap']; |
213
|
|
|
} |
214
|
|
|
|
215
|
4 |
|
if (! $this->hasConfig($input)) { |
216
|
|
|
return ''; |
217
|
|
|
} |
218
|
|
|
|
219
|
4 |
|
$config = $this->getConfig($input); |
220
|
4 |
|
$bootstrap = $config->getBootstrap(); |
221
|
|
|
|
222
|
4 |
|
return ($bootstrap) ? $config->getConfigDir() . $bootstrap : ''; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.