|
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 self)->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
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->phpUnitOptions[$name] = $valueWithSeparator; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns a string containing all PHPUnit options with corresponding values in the correct format. |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function phpUnitArguments() |
|
60
|
|
|
{ |
|
61
|
|
|
$arguments = []; |
|
62
|
|
|
|
|
63
|
|
|
foreach ($this->phpUnitOptions as $name => $optionValue) { |
|
64
|
|
|
if (is_null($optionValue)) { |
|
65
|
|
|
$arguments[] = $name; |
|
66
|
|
|
continue; |
|
67
|
|
|
} |
|
68
|
|
|
$arguments[] = $name.$optionValue['separator'].$optionValue['value']; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (! empty($this->testFile)) { |
|
72
|
|
|
$arguments[] = $this->testFile; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return implode(' ', $arguments); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Returns an array representation containing application options, PHPUnit options and the test file. |
|
80
|
|
|
* |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
|
|
public function toArray() |
|
84
|
|
|
{ |
|
85
|
|
|
return array_merge( |
|
86
|
|
|
$this->applicationOptions, |
|
87
|
|
|
$this->phpUnitOptions, |
|
88
|
|
|
empty($this->testFile) ? [] : [$this->testFile => null] |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Parses a raw arguments string. |
|
94
|
|
|
* |
|
95
|
|
|
* The following types of arguments will be parsed: space separated arguments, key/value and boolean arguments, |
|
96
|
|
|
* and test file name arguments. Space separated arguments are key/value arguments separated by a space. Boolean |
|
97
|
|
|
* and key/value arguments are regular options that either take no parameters or take a =-separated parameter. |
|
98
|
|
|
* Test file name arguments are file names (without option flags). |
|
99
|
|
|
* |
|
100
|
|
|
* @param $argumentsInput |
|
101
|
|
|
* @return $this |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function parse($argumentsInput) |
|
104
|
|
|
{ |
|
105
|
|
|
$arguments = explode(' ', $argumentsInput); |
|
106
|
|
|
|
|
107
|
|
|
// Keeps track of option name belonging to value when option name and value are space separated |
|
108
|
|
|
$nextArgumentBelongsTo = false; |
|
109
|
|
|
|
|
110
|
|
|
// PHPUnit only uses first file when multiple are given |
|
111
|
|
|
$fileSet = false; |
|
112
|
|
|
|
|
113
|
|
|
foreach ($arguments as $argument) { |
|
114
|
|
|
if ($nextArgumentBelongsTo) { |
|
115
|
|
|
$this->addArgument($nextArgumentBelongsTo, $argument, ' '); |
|
116
|
|
|
$nextArgumentBelongsTo = false; |
|
117
|
|
|
continue; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if ($this->isOption($argument) && $this->isOptionWithSpaceSeparatedArgument($this->optionName($argument))) { |
|
121
|
|
|
$nextArgumentBelongsTo = $argument; |
|
122
|
|
|
continue; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if ($this->isOption($argument)) { |
|
126
|
|
|
$this->parseOption($argument); |
|
127
|
|
|
continue; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if (! $fileSet) { |
|
131
|
|
|
$this->testFile = $argument; |
|
132
|
|
|
$fileSet = true; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Parses an argument string of an option (either key/value or boolean) and adds it to the list of arguments. |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $argument |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function parseOption($argument) |
|
145
|
|
|
{ |
|
146
|
|
|
if (strpos($argument, '=') !== false) { |
|
147
|
|
|
list($name, $value) = explode('=', $argument); |
|
148
|
|
|
|
|
149
|
|
|
$this->addArgument($name, $value); |
|
150
|
|
|
|
|
151
|
|
|
return; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$this->addArgument($argument); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Converts an argument string into the name of the option. |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $argument |
|
161
|
|
|
* @return mixed |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function optionName($argument) |
|
164
|
|
|
{ |
|
165
|
|
|
// String starts with -- |
|
166
|
|
|
if (substr($argument, 0, 2) == '--') { |
|
167
|
|
|
$argument = substr($argument, 2); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
// String starts with - |
|
171
|
|
|
if (substr($argument, 0, 1) == '-') { |
|
172
|
|
|
$argument = substr($argument, 1); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
// Remove everything after (and including) equals sign |
|
176
|
|
|
if (strpos($argument, '=') !== false) { |
|
177
|
|
|
$argument = substr($argument, 0, strpos($argument, '=')); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $argument; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Determines if the given argument string is an option. |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $argument |
|
187
|
|
|
* @return bool |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function isOption($argument) |
|
190
|
|
|
{ |
|
191
|
|
|
return substr($argument, 0, 1) == '-'; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Determines if the given option name is an application option. |
|
196
|
|
|
* |
|
197
|
|
|
* @param string $option |
|
198
|
|
|
* @return bool |
|
199
|
|
|
*/ |
|
200
|
|
|
protected function isApplicationOption($option) |
|
201
|
|
|
{ |
|
202
|
|
|
return in_array($option, self::APPLICATION_OPTIONS); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Determines if the given option name belongs to an option that takes a space separated argument. |
|
207
|
|
|
* |
|
208
|
|
|
* @param string $option |
|
209
|
|
|
* @return bool |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function isOptionWithSpaceSeparatedArgument($option) |
|
212
|
|
|
{ |
|
213
|
|
|
return in_array($option, PhpUnitCommand::optionsWithArguments()); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|