1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\PhpUnitWatcher; |
4
|
|
|
|
5
|
|
|
use Spatie\PhpUnitWatcher\PhpUnit\Command as PhpUnitCommand; |
6
|
|
|
|
7
|
|
|
class Arguments |
8
|
|
|
{ |
9
|
|
|
use ArgumentAccessors; |
10
|
|
|
|
11
|
|
|
const APPLICATION_OPTIONS = []; |
12
|
|
|
|
13
|
|
|
protected $testFile; |
14
|
|
|
protected $phpUnitOptions = []; |
15
|
|
|
protected $applicationOptions = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Parses the given arguments string and instantiates a new object containing the arguments. |
19
|
|
|
* |
20
|
|
|
* @param string $argumentsInput |
21
|
|
|
* @return $this |
22
|
|
|
*/ |
23
|
|
|
public static function fromString($argumentsInput) |
24
|
|
|
{ |
25
|
|
|
return (new static)->parse($argumentsInput); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Adds an argument if it does not yet exist, otherwise the argument value is replaced. |
30
|
|
|
* |
31
|
|
|
* In case $value is left empty, the argument will be treated as a boolean option. If the |
32
|
|
|
* name and value are not separated by a space, pass the correct separator as a third |
33
|
|
|
* parameter. |
34
|
|
|
* |
35
|
|
|
* Unless an option is registered (in this class) as an application option, we will assume |
36
|
|
|
* the option is meant to be passed through to PHPUnit. |
37
|
|
|
* |
38
|
|
|
* @param string $name |
39
|
|
|
* @param mixed $value |
40
|
|
|
* @param string $separator |
41
|
|
|
*/ |
42
|
|
|
public function addArgument($name, $value = null, $separator = '=') |
43
|
|
|
{ |
44
|
|
|
$valueWithSeparator = is_null($value) ? $value : ['value' => $value, 'separator' => $separator]; |
45
|
|
|
|
46
|
|
|
if ($this->isApplicationOption($this->optionName($name))) { |
47
|
|
|
$this->applicationOptions[$name] = $valueWithSeparator; |
48
|
|
|
|
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->phpUnitOptions[$name] = $valueWithSeparator; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns a string containing all PHPUnit options with corresponding values in the correct format. |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function phpUnitArguments() |
61
|
|
|
{ |
62
|
|
|
$arguments = []; |
63
|
|
|
|
64
|
|
|
foreach ($this->phpUnitOptions as $name => $optionValue) { |
65
|
|
|
if (is_null($optionValue)) { |
66
|
|
|
$arguments[] = $name; |
67
|
|
|
continue; |
68
|
|
|
} |
69
|
|
|
$arguments[] = $name.$optionValue['separator'].$optionValue['value']; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (! empty($this->testFile)) { |
73
|
|
|
$arguments[] = $this->testFile; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return implode(' ', $arguments); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns an array representation containing application options, PHPUnit options and the test file. |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function toArray() |
85
|
|
|
{ |
86
|
|
|
return array_merge( |
87
|
|
|
$this->applicationOptions, |
88
|
|
|
$this->phpUnitOptions, |
89
|
|
|
empty($this->testFile) ? [] : [$this->testFile => null] |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Parses a raw arguments string. |
95
|
|
|
* |
96
|
|
|
* The following types of arguments will be parsed: space separated arguments, key/value and boolean arguments, |
97
|
|
|
* and test file name arguments. Space separated arguments are key/value arguments separated by a space. Boolean |
98
|
|
|
* and key/value arguments are regular options that either take no parameters or take a =-separated parameter. |
99
|
|
|
* Test file name arguments are file names (without option flags). |
100
|
|
|
* |
101
|
|
|
* @param $argumentsInput |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
protected function parse($argumentsInput) |
105
|
|
|
{ |
106
|
|
|
$arguments = explode(' ', $argumentsInput); |
107
|
|
|
|
108
|
|
|
// Keeps track of option name belonging to value when option name and value are space separated |
109
|
|
|
$nextArgumentBelongsTo = false; |
110
|
|
|
|
111
|
|
|
// PHPUnit only uses first file when multiple are given |
112
|
|
|
$fileSet = false; |
113
|
|
|
|
114
|
|
|
foreach ($arguments as $argument) { |
115
|
|
|
if ($nextArgumentBelongsTo) { |
116
|
|
|
$this->addArgument($nextArgumentBelongsTo, $argument, ' '); |
117
|
|
|
$nextArgumentBelongsTo = false; |
118
|
|
|
continue; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($this->isOption($argument) && $this->isOptionWithSpaceSeparatedArgument($this->optionName($argument))) { |
122
|
|
|
$nextArgumentBelongsTo = $argument; |
123
|
|
|
continue; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($this->isOption($argument)) { |
127
|
|
|
$this->parseOption($argument); |
128
|
|
|
continue; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (! $fileSet) { |
132
|
|
|
$this->testFile = $argument; |
133
|
|
|
$fileSet = true; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Parses an argument string of an option (either key/value or boolean) and adds it to the list of arguments. |
142
|
|
|
* |
143
|
|
|
* @param string $argument |
144
|
|
|
*/ |
145
|
|
|
protected function parseOption($argument) |
146
|
|
|
{ |
147
|
|
|
if (strpos($argument, '=') !== false) { |
148
|
|
|
list($name, $value) = explode('=', $argument); |
149
|
|
|
|
150
|
|
|
$this->addArgument($name, $value); |
151
|
|
|
|
152
|
|
|
return; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$this->addArgument($argument); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Converts an argument string into the name of the option. |
160
|
|
|
* |
161
|
|
|
* @param string $argument |
162
|
|
|
* @return mixed |
163
|
|
|
*/ |
164
|
|
|
protected function optionName($argument) |
165
|
|
|
{ |
166
|
|
|
// String starts with -- |
167
|
|
|
if (substr($argument, 0, 2) == '--') { |
168
|
|
|
$argument = substr($argument, 2); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
// String starts with - |
172
|
|
|
if (substr($argument, 0, 1) == '-') { |
173
|
|
|
$argument = substr($argument, 1); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// Remove everything after (and including) equals sign |
177
|
|
|
if (strpos($argument, '=') !== false) { |
178
|
|
|
$argument = substr($argument, 0, strpos($argument, '=')); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $argument; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Determines if the given argument string is an option. |
186
|
|
|
* |
187
|
|
|
* @param string $argument |
188
|
|
|
* @return bool |
189
|
|
|
*/ |
190
|
|
|
protected function isOption($argument) |
191
|
|
|
{ |
192
|
|
|
return substr($argument, 0, 1) == '-'; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Determines if the given option name is an application option. |
197
|
|
|
* |
198
|
|
|
* @param string $option |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
|
|
protected function isApplicationOption($option) |
202
|
|
|
{ |
203
|
|
|
return in_array($option, self::APPLICATION_OPTIONS); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Determines if the given option name belongs to an option that takes a space separated argument. |
208
|
|
|
* |
209
|
|
|
* @param string $option |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
|
|
protected function isOptionWithSpaceSeparatedArgument($option) |
213
|
|
|
{ |
214
|
|
|
return in_array($option, PhpUnitCommand::optionsWithArguments()); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|