Completed
Push — master ( 25ec1c...ee1026 )
by Erin
01:51
created

Configuration::getReporters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 array
39
     */
40
    protected $reporters = ['spec'];
41
42
    /**
43
     * @var string
44
     */
45
    protected $path;
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->path = 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->writeReporters([$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->reporters[0];
151
    }
152
153
    /**
154
     * Set the names of the reporters to use
155
     *
156
     * @param array $reporters
157
     * @return $this
158
     */
159
    public function setReporters(array $reporters)
160
    {
161
        if (empty($reporters)) {
162
            throw new \InvalidArgumentException('Reporters cannot be empty.');
163
        }
164
165
        return $this->writeReporters($reporters);
166
    }
167
168
    /**
169
     * Return the names of the reporters configured for use
170
     *
171
     * @return array
172
     */
173
    public function getReporters()
174
    {
175
        return $this->reporters;
176
    }
177
178
    /**
179
     * Set the path to load tests from
180
     *
181
     * @param string $path
182
     * @return $this
183
     */
184
    public function setPath($path)
185
    {
186
        return $this->write('path', $path);
187
    }
188
189
    /**
190
     * Return the path being searched for tests
191
     *
192
     * @return string
193
     */
194
    public function getPath()
195
    {
196
        return $this->path;
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 $varName
324
     * @param $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
     * Normalize the supplied regular expression pattern.
338
     *
339
     * @param string $pattern
340
     * @return string
341
     */
342
    protected function normalizeRegexPattern($pattern)
343
    {
344
        if (false !== @preg_match($pattern, null)) {
345
            return $pattern;
346
        }
347
348
        $boundedPattern = '~\b' . str_replace('~', '\~', $pattern) . '\b~';
349
350
        if (false !== @preg_match($boundedPattern, null)) {
351
            return $boundedPattern;
352
        }
353
354
        return '~\b' . preg_quote($pattern, '~') . '\b~';
355
    }
356
357
    /**
358
     * Write the reporters and persist them to the current environment.
359
     *
360
     * @param array $reporters
361
     * @return $this
362
     */
363
    protected function writeReporters(array $reporters)
364
    {
365
        $this->reporters = $reporters;
366
        putenv('PERIDOT_REPORTER=' . $reporters[0]);
367
        putenv('PERIDOT_REPORTERS=' . implode(',', $reporters));
368
        return $this;
369
    }
370
}
371