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