Completed
Push — focused-specs ( 79f2d9...a26735 )
by Erin
02:09
created

AbstractTest::getFocused()   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
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
     * {@inheritdoc}
186
     *
187
     * @return array
188
     */
189
    public function getSetupFunctions()
190
    {
191
        return $this->setUpFns;
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     *
197
     * @return array
198
     */
199
    public function getTearDownFunctions()
200
    {
201
        return $this->tearDownFns;
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     *
207
     * @param callable $fn
208
     */
209
    public function forEachNodeBottomUp(callable $fn)
210
    {
211
        $node = $this;
212
        while ($node !== null) {
213
            $fn($node);
214
            $node = $node->getParent();
215
        }
216
    }
217
218
    /**
219
     * {@inheritdoc}
220
     *
221
     * @param callable $fn
222
     */
223
    public function forEachNodeTopDown(callable $fn)
224
    {
225
        $node = $this;
226
        $nodes = [];
227
        while ($node !== null) {
228
            array_unshift($nodes, $node);
229
            $node = $node->getParent();
230
        }
231
        foreach ($nodes as $node) {
232
            $fn($node);
233
        }
234
    }
235
236
    /**
237
     * {@inheritdoc}
238
     *
239
     * @return Scope
240
     */
241
    public function getScope()
242
    {
243
        return $this->scope;
244
    }
245
246
    /**
247
     * {@inheritdoc}
248
     *
249
     * @param Scope $scope
250
     * @return mixed
251
     */
252
    public function setScope(Scope $scope)
253
    {
254
        $this->scope = $scope;
255
        return $this;
256
    }
257
258
    /**
259
     * Get the file this test belongs to.
260
     *
261
     * @return string
262
     */
263
    public function getFile()
264
    {
265
        return $this->file;
266
    }
267
268
    /**
269
     * Set the file this test belongs to.
270
     *
271
     * @param string $file
272
     */
273
    public function setFile($file)
274
    {
275
        $this->file = $file;
276
        return $this;
277
    }
278
279
    /**
280
     * {@inheritdoc}
281
     *
282
     * @param array $args
283
     * @return $this
284
     */
285
    public function setDefinitionArguments(array $args)
286
    {
287
        $this->definitionArguments = $args;
288
    }
289
290
    /**
291
     * {@inheritdoc}
292
     *
293
     * @return array
294
     */
295
    public function getDefinitionArguments()
296
    {
297
        return $this->definitionArguments;
298
    }
299
}
300