|
1
|
|
|
<?php |
|
2
|
|
|
namespace Peridot; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Configuration stores configured values used through the Peridot application |
|
6
|
|
|
* lifecycle. |
|
7
|
|
|
* |
|
8
|
|
|
* @package Peridot |
|
9
|
|
|
*/ |
|
10
|
|
|
class Configuration |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var boolean |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $colorsEnabled = true; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var boolean |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $colorsEnableExplicit = false; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string|null |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $focusPattern; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string|null |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $skipPattern; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $grep = '*.spec.php'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $reporter = 'spec'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $paths; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $configurationFile; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $dsl; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var bool |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $stopOnFailure = false; |
|
61
|
|
|
|
|
62
|
|
|
public function __construct() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->paths = [getcwd()]; |
|
65
|
|
|
$this->configurationFile = getcwd() . DIRECTORY_SEPARATOR . 'peridot.php'; |
|
66
|
|
|
$this->dsl = __DIR__ . DIRECTORY_SEPARATOR . 'Dsl.php'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set the pattern used to load tests |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $grep |
|
73
|
|
|
* @return $this |
|
74
|
|
|
*/ |
|
75
|
|
|
public function setGrep($grep) |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->write('grep', $grep); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns the pattern used to load tests |
|
82
|
|
|
* |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getGrep() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->grep; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Set the pattern used to focus tests |
|
92
|
|
|
* |
|
93
|
|
|
* @param string|null $pattern |
|
94
|
|
|
* @return $this |
|
95
|
|
|
*/ |
|
96
|
|
|
public function setFocusPattern($pattern) |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->write('focusPattern', $this->normalizeRegexPattern($pattern)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns the pattern used to focus tests |
|
103
|
|
|
* |
|
104
|
|
|
* @return string|null |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getFocusPattern() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->focusPattern; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Set the pattern used to skip tests |
|
113
|
|
|
* |
|
114
|
|
|
* @param string|null $pattern |
|
115
|
|
|
* @return $this |
|
116
|
|
|
*/ |
|
117
|
|
|
public function setSkipPattern($pattern) |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->write('skipPattern', $this->normalizeRegexPattern($pattern)); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Returns the pattern used to skip tests |
|
124
|
|
|
* |
|
125
|
|
|
* @return string|null |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getSkipPattern() |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->skipPattern; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Set the name of the reporter to use |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $reporter |
|
136
|
|
|
* @return $this |
|
137
|
|
|
*/ |
|
138
|
|
|
public function setReporter($reporter) |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->write('reporter', $reporter); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Return the name of the reporter configured for use |
|
145
|
|
|
* |
|
146
|
|
|
* @return string |
|
147
|
|
|
*/ |
|
148
|
|
|
public function getReporter() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->reporter; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Set the path to load tests from |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $path |
|
157
|
|
|
* @return $this |
|
158
|
|
|
*/ |
|
159
|
|
|
public function setPath($path) |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->writePaths([$path]); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Return the path being searched for tests |
|
166
|
|
|
* |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getPath() |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->paths[0]; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Set the paths to load tests from |
|
176
|
|
|
* |
|
177
|
|
|
* @param array $paths |
|
178
|
|
|
* @return $this |
|
179
|
|
|
*/ |
|
180
|
|
|
public function setPaths(array $paths) |
|
181
|
|
|
{ |
|
182
|
|
|
if (empty($paths)) { |
|
183
|
|
|
throw new \InvalidArgumentException('Paths cannot be empty.'); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return $this->writePaths($paths); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Return the paths being searched for tests |
|
191
|
|
|
* |
|
192
|
|
|
* @return array |
|
193
|
|
|
*/ |
|
194
|
|
|
public function getPaths() |
|
195
|
|
|
{ |
|
196
|
|
|
return $this->paths; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Disable output colors |
|
201
|
|
|
* |
|
202
|
|
|
* @return $this |
|
203
|
|
|
*/ |
|
204
|
|
|
public function disableColors() |
|
205
|
|
|
{ |
|
206
|
|
|
if ( $this->colorsEnableExplicit ) { |
|
207
|
|
|
return $this; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
return $this->write('colorsEnabled', false); |
|
|
|
|
|
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Force output colors even without TTY support. |
|
215
|
|
|
* |
|
216
|
|
|
* @return $this |
|
217
|
|
|
*/ |
|
218
|
|
|
public function enableColorsExplicit() |
|
219
|
|
|
{ |
|
220
|
|
|
return $this |
|
221
|
|
|
->write('colorsEnableExplicit', true) |
|
|
|
|
|
|
222
|
|
|
->write('colorsEnabled', true); |
|
|
|
|
|
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Check if output colors are disabled |
|
227
|
|
|
* |
|
228
|
|
|
* @return boolean |
|
229
|
|
|
*/ |
|
230
|
|
|
public function areColorsEnabled() |
|
231
|
|
|
{ |
|
232
|
|
|
return $this->colorsEnableExplicit || $this->colorsEnabled; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Check if output colors are explicitly enabled. |
|
237
|
|
|
* |
|
238
|
|
|
* @return boolean |
|
239
|
|
|
*/ |
|
240
|
|
|
public function areColorsEnabledExplicit() |
|
241
|
|
|
{ |
|
242
|
|
|
return $this->colorsEnableExplicit; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Stop the suite runner when a failure occurs |
|
247
|
|
|
* |
|
248
|
|
|
* @return $this |
|
249
|
|
|
*/ |
|
250
|
|
|
public function stopOnFailure() |
|
251
|
|
|
{ |
|
252
|
|
|
return $this->write('stopOnFailure', true); |
|
|
|
|
|
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Check if the suite runner should stop on failure |
|
257
|
|
|
* |
|
258
|
|
|
* @return bool |
|
259
|
|
|
*/ |
|
260
|
|
|
public function shouldStopOnFailure() |
|
261
|
|
|
{ |
|
262
|
|
|
return $this->stopOnFailure; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Set the path to a Peridot configuration file |
|
267
|
|
|
* |
|
268
|
|
|
* @param string $configurationFile |
|
269
|
|
|
* @return $this |
|
270
|
|
|
*/ |
|
271
|
|
|
public function setConfigurationFile($configurationFile) |
|
272
|
|
|
{ |
|
273
|
|
|
$search = [$configurationFile, getcwd() . DIRECTORY_SEPARATOR . $configurationFile]; |
|
274
|
|
|
$found = array_filter($search, 'file_exists'); |
|
275
|
|
|
|
|
276
|
|
|
if (count($found) == 0) { |
|
277
|
|
|
throw new \RuntimeException("Configuration file specified but does not exist"); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
$this->write('configurationFile', $found[0]); |
|
281
|
|
|
|
|
282
|
|
|
return $this; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* Return the path to the Peridot configuration file. Returns a relative |
|
287
|
|
|
* path if it exists, otherwise return the provided value |
|
288
|
|
|
* |
|
289
|
|
|
* @return string |
|
290
|
|
|
*/ |
|
291
|
|
|
public function getConfigurationFile() |
|
292
|
|
|
{ |
|
293
|
|
|
return $this->configurationFile; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Set the path to a DSL file for defining |
|
298
|
|
|
* the test language used |
|
299
|
|
|
* |
|
300
|
|
|
* @param string $dsl |
|
301
|
|
|
* @return $this |
|
302
|
|
|
*/ |
|
303
|
|
|
public function setDsl($dsl) |
|
304
|
|
|
{ |
|
305
|
|
|
return $this->write('dsl', $dsl); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Get the path to a DSL file containing |
|
310
|
|
|
* test functions to use |
|
311
|
|
|
* |
|
312
|
|
|
* @return string |
|
313
|
|
|
*/ |
|
314
|
|
|
public function getDsl() |
|
315
|
|
|
{ |
|
316
|
|
|
return $this->dsl; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* Write a configuration value and persist it to the current |
|
321
|
|
|
* environment. |
|
322
|
|
|
* |
|
323
|
|
|
* @param string $varName |
|
324
|
|
|
* @param string $value |
|
325
|
|
|
* @return $this |
|
326
|
|
|
*/ |
|
327
|
|
|
protected function write($varName, $value) |
|
328
|
|
|
{ |
|
329
|
|
|
$this->$varName = $value; |
|
330
|
|
|
$parts = preg_split('/(?=[A-Z])/', $varName); |
|
331
|
|
|
$env = 'PERIDOT_' . strtoupper(join('_', $parts)); |
|
332
|
|
|
putenv($env . '=' . $value); |
|
333
|
|
|
return $this; |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
/** |
|
337
|
|
|
* Write the paths and persist them to the current environment. |
|
338
|
|
|
* |
|
339
|
|
|
* @param array $paths |
|
340
|
|
|
* @return $this |
|
341
|
|
|
*/ |
|
342
|
|
|
protected function writePaths(array $paths) |
|
343
|
|
|
{ |
|
344
|
|
|
$this->paths = $paths; |
|
345
|
|
|
putenv('PERIDOT_PATH=' . $paths[0]); |
|
346
|
|
|
putenv('PERIDOT_PATHS=' . implode(PATH_SEPARATOR, $paths)); |
|
347
|
|
|
return $this; |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
/** |
|
351
|
|
|
* Normalize the supplied regular expression pattern. |
|
352
|
|
|
* |
|
353
|
|
|
* @param string $pattern |
|
354
|
|
|
* @return string |
|
355
|
|
|
*/ |
|
356
|
|
|
protected function normalizeRegexPattern($pattern) |
|
357
|
|
|
{ |
|
358
|
|
|
if (false !== @preg_match($pattern, null)) { |
|
359
|
|
|
return $pattern; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
$boundedPattern = '~\b' . str_replace('~', '\~', $pattern) . '\b~'; |
|
363
|
|
|
|
|
364
|
|
|
if (false !== @preg_match($boundedPattern, null)) { |
|
365
|
|
|
return $boundedPattern; |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
return '~\b' . preg_quote($pattern, '~') . '\b~'; |
|
369
|
|
|
} |
|
370
|
|
|
} |
|
371
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: