-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathPhpUnitOptions.php
More file actions
284 lines (247 loc) · 7.4 KB
/
Copy pathPhpUnitOptions.php
File metadata and controls
284 lines (247 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
namespace PHPCensor\Plugin\Option;
use PHPCensor\Common\Application\ConfigurationInterface;
/**
* Class PhpUnitOptions validates and parse the option for the PhpUnitV2 plugin
*
* @package PHP Censor
* @subpackage Application
*
* @author Pablo Tejada <pablo@ptejada.com>
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class PhpUnitOptions
{
protected array $options;
protected string $location;
protected array $arguments = [];
protected ConfigurationInterface $configuration;
public function __construct(ConfigurationInterface $configuration, array $options, string $location)
{
$this->configuration = $configuration;
$this->options = $options;
$this->location = $location;
}
/**
* Remove a command argument
*
*
* @return $this
*/
public function removeArgument($argumentName)
{
unset($this->arguments[$argumentName]);
return $this;
}
/**
* Combine all the argument into a string for the phpunit command
*
* @return string
*/
public function buildArgumentString()
{
$argumentString = '';
foreach ($this->getCommandArguments() as $argumentName => $argumentValues) {
$prefix = $argumentName[0] === '-' ? '' : '--';
if (!\is_array($argumentValues)) {
$argumentValues = [$argumentValues];
}
foreach ($argumentValues as $argValue) {
$postfix = ' ';
if (!empty($argValue)) {
$postfix = ' "' . $argValue . '" ';
}
$argumentString .= $prefix . $argumentName . $postfix;
}
}
return $argumentString;
}
/**
* Get all the command arguments
*
* @return string[]
*/
public function getCommandArguments()
{
/*
* Return the full list of arguments
*/
return $this->parseArguments()->arguments;
}
/**
* Parse the arguments from the config options
*
* @return $this
*/
private function parseArguments()
{
if (empty($this->arguments)) {
/*
* Parse the arguments from the YML options file
*/
if (isset($this->options['args'])) {
$rawArgs = $this->options['args'];
if (\is_array($rawArgs)) {
$this->arguments = $rawArgs;
} else {
/*
* Try to parse old arguments in a single string
*/
\preg_match_all('@--([a-z\-]+)([\s=]+)?[\'"]?((?!--)[-\w/.,\\\]+)?[\'"]?@', (string)$rawArgs, $argsMatch);
if (!empty($argsMatch) && \sizeof($argsMatch) > 2) {
foreach ($argsMatch[1] as $index => $argName) {
$this->addArgument($argName, $argsMatch[3][$index]);
}
}
}
}
/*
* Handles command aliases outside of the args option
*/
if (isset($this->options['coverage']) && $this->options['coverage']) {
$allowPublicArtifacts = (bool)$this->configuration->get(
'php-censor.build.allow_public_artifacts',
false
);
if ($allowPublicArtifacts) {
$this->addArgument('coverage-html', $this->location);
}
$this->addArgument('coverage-text');
}
/*
* Handles command aliases outside of the args option
*/
if (isset($this->options['config'])) {
$this->addArgument('configuration', $this->options['config']);
}
}
return $this;
}
/**
* Add an argument to the collection
* Note: adding argument before parsing the options will prevent the other options from been parsed.
*
* @param string $argumentName
* @param string $argumentValue
*/
public function addArgument($argumentName, $argumentValue = null)
{
if (isset($this->arguments[$argumentName])) {
if (!\is_array($this->arguments[$argumentName])) {
// Convert existing argument values into an array
$this->arguments[$argumentName] = [$this->arguments[$argumentName]];
}
// Appends the new argument to the list
$this->arguments[$argumentName][] = $argumentValue;
} else {
// Adds new argument
$this->arguments[$argumentName] = $argumentValue;
}
}
/**
* Get the list of directory to run phpunit in
*
* @return string[] List of directories
*/
public function getDirectories()
{
$directories = $this->getOption('directories');
if (\is_string($directories)) {
$directories = [$directories];
} else {
if (\is_null($directories)) {
$directories = [];
}
}
return \is_array($directories) ? $directories : [$directories];
}
/**
* Get an option if defined
*
*
* @return string|string[]|null
*/
public function getOption($optionName)
{
if (isset($this->options[$optionName])) {
return $this->options[$optionName];
}
return null;
}
/**
* Get the directory to execute the command from
*
* @return mixed|null
*/
public function getRunFrom()
{
return $this->getOption('run_from');
}
/**
* Ge the directory name where tests file reside
*
* @return string|null
*/
public function getTestsPath()
{
return $this->getOption('path');
}
/**
* Get the PHPUnit configuration from the options, or the optional path
*
* @param string $altPath
*
* @return string[] path of files
*/
public function getConfigFiles($altPath = null)
{
$configFiles = $this->getArgument('configuration');
if (empty($configFiles) && $altPath) {
$configFile = self::findConfigFile($altPath);
if ($configFile) {
$configFiles[] = $configFile;
}
}
return $configFiles;
}
/**
* Get options for a given argument
*
*
* @return string[] All the options for given argument
*/
public function getArgument($argumentName)
{
$this->parseArguments();
if (isset($this->arguments[$argumentName])) {
return \is_array(
$this->arguments[$argumentName]
) ? $this->arguments[$argumentName] : [$this->arguments[$argumentName]];
}
return [];
}
/**
* Find a PHPUnit configuration file in a directory
*
* @param string $buildPath The path to configuration file
*
* @return string|null
*/
public static function findConfigFile($buildPath)
{
$files = [
'phpunit.xml',
'phpunit.mysql.xml',
'phpunit.pgsql.xml',
'phpunit.xml.dist',
'tests/phpunit.xml',
'tests/phpunit.xml.dist',
];
foreach ($files as $file) {
if (\file_exists($buildPath . $file)) {
return $file;
}
}
return null;
}
}