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