AbstractTest::applyFocusPatterns()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 2
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace Peridot\Core;
4
5
/**
6
 * Base class for Peridot Suites and Tests
7
 *
8
 * @package Peridot\Core
9
 */
10
abstract class AbstractTest implements TestInterface
11
{
12
    use HasEventEmitterTrait;
13
14
    /**
15
     * The test definition as a callable.
16
     *
17
     * @var callable
18
     */
19
    protected $definition;
20
21
    /**
22
     * A collection of functions to run before tests execute.
23
     *
24
     * @var array
25
     */
26
    protected $setUpFns = [];
27
28
    /**
29
     * A collection of functions to run after tests execute.
30
     *
31
     * @var array
32
     */
33
    protected $tearDownFns = [];
34
35
    /**
36
     * @var string
37
     */
38
    protected $description;
39
40
    /**
41
     * @var TestInterface
42
     */
43
    protected $parent;
44
45
    /**
46
     * @var bool|null
47
     */
48
    protected $pending = null;
49
50
    /**
51
     * @var bool
52
     */
53
    protected $focused;
54
55
    /**
56
     * @var Scope
57
     */
58
    protected $scope;
59
60
    /**
61
     * @var string
62
     */
63
    protected $file;
64
65
    /**
66
     * @var array
67
     */
68
    protected $definitionArguments = [];
69
70
    /**
71
     * @param string   $description
72
     * @param callable $definition
73
     * @param bool     $focused
74
     */
75
    public function __construct($description, callable $definition, $focused = false)
76
    {
77
        $this->definition = $definition;
78
        $this->description = $description;
79
        $this->focused = $focused;
80
        $this->scope = new Scope();
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     *
86
     * @param callable $setupFn
87
     */
88
    public function addSetupFunction(callable $setupFn)
89
    {
90
        $fn = $this->getScope()->peridotBindTo($setupFn);
91
        array_push($this->setUpFns, $fn);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     *
97
     * @param callable $tearDownFn
98
     */
99
    public function addTearDownFunction(callable $tearDownFn)
100
    {
101
        $fn = $this->getScope()->peridotBindTo($tearDownFn);
102
        array_push($this->tearDownFns, $fn);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     *
108
     * @return string
109
     */
110
    public function getDescription()
111
    {
112
        return $this->description;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     *
118
     * @return callable
119
     */
120
    public function getDefinition()
121
    {
122
        return $this->scope->peridotBindTo($this->definition);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     *
128
     * @param  TestInterface $parent
129
     * @return mixed|void
130
     */
131
    public function setParent(TestInterface $parent)
132
    {
133
        $this->parent = $parent;
134
        $this->setScope($parent->getScope());
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     *
140
     * @return TestInterface
141
     */
142
    public function getParent()
143
    {
144
        return $this->parent;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     *
150
     * @return string
151
     */
152
    public function getTitle()
153
    {
154
        $parts = [];
155
        $node = $this;
156
        while ($node != null) {
157
            array_unshift($parts, $node->getDescription());
158
            $node = $node->getParent();
159
        }
160
161
        return implode(' ', $parts);
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     *
167
     * @return bool|null
168
     */
169
    public function getPending()
170
    {
171
        return $this->pending;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     *
177
     * @param bool $state
178
     */
179
    public function setPending($state)
180
    {
181
        $this->pending = (bool) $state;
182
    }
183
184
    /**
185
     * Set the focused status of the test and its children according to the
186
     * supplied focus pattern and/or skip pattern
187
     *
188
     * @param string|null $focusPattern
189
     * @param string|null $skipPattern
190
     */
191
    public function applyFocusPatterns($focusPattern, $skipPattern = null)
192
    {
193
        $title = $this->getTitle();
194
195
        if ($skipPattern !== null && preg_match($skipPattern, $title)) {
196
            $this->focused = false;
197
198
            return;
199
        }
200
201
        if ($focusPattern === null) {
202
            $this->focused = true;
203
        } else {
204
            $this->focused = (bool) preg_match($focusPattern, $title);
205
        }
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     *
211
     * @return array
212
     */
213
    public function getSetupFunctions()
214
    {
215
        return $this->setUpFns;
216
    }
217
218
    /**
219
     * {@inheritdoc}
220
     *
221
     * @return array
222
     */
223
    public function getTearDownFunctions()
224
    {
225
        return $this->tearDownFns;
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     *
231
     * @param callable $fn
232
     */
233
    public function forEachNodeBottomUp(callable $fn)
234
    {
235
        $node = $this;
236
        while ($node !== null) {
237
            $fn($node);
238
            $node = $node->getParent();
239
        }
240
    }
241
242
    /**
243
     * {@inheritdoc}
244
     *
245
     * @param callable $fn
246
     */
247
    public function forEachNodeTopDown(callable $fn)
248
    {
249
        $node = $this;
250
        $nodes = [];
251
        while ($node !== null) {
252
            array_unshift($nodes, $node);
253
            $node = $node->getParent();
254
        }
255
        foreach ($nodes as $node) {
256
            $fn($node);
257
        }
258
    }
259
260
    /**
261
     * {@inheritdoc}
262
     *
263
     * @return Scope
264
     */
265
    public function getScope()
266
    {
267
        return $this->scope;
268
    }
269
270
    /**
271
     * {@inheritdoc}
272
     *
273
     * @param Scope $scope
274
     * @return mixed
275
     */
276
    public function setScope(Scope $scope)
277
    {
278
        $this->scope = $scope;
279
        return $this;
280
    }
281
282
    /**
283
     * Get the file this test belongs to.
284
     *
285
     * @return string
286
     */
287
    public function getFile()
288
    {
289
        return $this->file;
290
    }
291
292
    /**
293
     * Set the file this test belongs to.
294
     *
295
     * @param string $file
296
     */
297
    public function setFile($file)
298
    {
299
        $this->file = $file;
300
        return $this;
301
    }
302
303
    /**
304
     * {@inheritdoc}
305
     *
306
     * @param array $args
307
     * @return $this
308
     */
309
    public function setDefinitionArguments(array $args)
310
    {
311
        $this->definitionArguments = $args;
312
    }
313
314
    /**
315
     * {@inheritdoc}
316
     *
317
     * @return array
318
     */
319
    public function getDefinitionArguments()
320
    {
321
        return $this->definitionArguments;
322
    }
323
}
324