Completed
Push — focused-specs-cli ( bbdcb1...667522 )
by Erin
03:21 queued 01:48
created

Configuration::setSkipPattern()   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 1
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 string
39
     */
40
    protected $reporter = '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->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->write('path', $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->path;
172
    }
173
174
    /**
175
     * Disable output colors
176
     *
177
     * @return $this
178
     */
179
    public function disableColors()
180
    {
181
        if ( $this->colorsEnableExplicit ) {
182
            return $this;
183
        }
184
185
        return $this->write('colorsEnabled', false);
186
    }
187
188
    /**
189
     * Force output colors even without TTY support.
190
     *
191
     * @return $this
192
     */
193
    public function enableColorsExplicit()
194
    {
195
        return $this
196
            ->write('colorsEnableExplicit', true)
197
            ->write('colorsEnabled', true);
198
    }
199
200
    /**
201
     * Check if output colors are disabled
202
     *
203
     * @return boolean
204
     */
205
    public function areColorsEnabled()
206
    {
207
        return $this->colorsEnableExplicit || $this->colorsEnabled;
208
    }
209
210
    /**
211
     * Check if output colors are explicitly enabled.
212
     *
213
     * @return boolean
214
     */
215
    public function areColorsEnabledExplicit()
216
    {
217
        return $this->colorsEnableExplicit;
218
    }
219
220
    /**
221
     * Stop the suite runner when a failure occurs
222
     *
223
     * @return $this
224
     */
225
    public function stopOnFailure()
226
    {
227
        return $this->write('stopOnFailure', true);
228
    }
229
230
    /**
231
     * Check if the suite runner should stop on failure
232
     *
233
     * @return bool
234
     */
235
    public function shouldStopOnFailure()
236
    {
237
        return $this->stopOnFailure;
238
    }
239
240
    /**
241
     * Set the path to a Peridot configuration file
242
     *
243
     * @param string $configurationFile
244
     * @return $this
245
     */
246
    public function setConfigurationFile($configurationFile)
247
    {
248
        $search = [$configurationFile, getcwd() . DIRECTORY_SEPARATOR . $configurationFile];
249
        $found = array_filter($search, 'file_exists');
250
251
        if (count($found) == 0) {
252
            throw new \RuntimeException("Configuration file specified but does not exist");
253
        }
254
255
        $this->write('configurationFile', $found[0]);
256
257
        return $this;
258
    }
259
260
    /**
261
     * Return the path to the Peridot configuration file. Returns a relative
262
     * path if it exists, otherwise return the provided value
263
     *
264
     * @return string
265
     */
266
    public function getConfigurationFile()
267
    {
268
        return $this->configurationFile;
269
    }
270
271
    /**
272
     * Set the path to a DSL file for defining
273
     * the test language used
274
     *
275
     * @param string $dsl
276
     * @return $this
277
     */
278
    public function setDsl($dsl)
279
    {
280
        return $this->write('dsl', $dsl);
281
    }
282
283
    /**
284
     * Get the path to a DSL file containing
285
     * test functions to use
286
     *
287
     * @return string
288
     */
289
    public function getDsl()
290
    {
291
        return $this->dsl;
292
    }
293
294
    /**
295
     * Write a configuration value and persist it to the current
296
     * environment.
297
     *
298
     * @param $varName
299
     * @param $value
300
     * @return $this
301
     */
302
    protected function write($varName, $value)
303
    {
304
        $this->$varName = $value;
305
        $parts = preg_split('/(?=[A-Z])/', $varName);
306
        $env = 'PERIDOT_' . strtoupper(join('_', $parts));
307
        putenv($env . '=' . $value);
308
        return $this;
309
    }
310
311
    /**
312
     * Normalize the supplied regular expression pattern.
313
     *
314
     * @param string $pattern
315
     * @return string
316
     */
317
    protected function normalizeRegexPattern($pattern)
318
    {
319
        if (false !== @preg_match($pattern, null)) {
320
            return $pattern;
321
        }
322
323
        return '~\b' . str_replace('~', '\~', $pattern) . '\b~';
324
    }
325
}
326