Completed
Push — focused-specs ( 294f34...79f2d9 )
by Erin
02:00
created

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