Completed
Push — focused-specs ( 294f34 )
by Erin
04:16
created

AbstractTest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
rs 9.4285
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|null
52
     */
53
    protected $focused = null;
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
     */
74
    public function __construct($description, callable $definition)
75
    {
76
        $this->definition = $definition;
77
        $this->description = $description;
78
        $this->scope = new Scope();
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     *
84
     * @param callable $setupFn
85
     */
86
    public function addSetupFunction(callable $setupFn)
87
    {
88
        $fn = $this->getScope()->peridotBindTo($setupFn);
89
        array_push($this->setUpFns, $fn);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     *
95
     * @param callable $tearDownFn
96
     */
97
    public function addTearDownFunction(callable $tearDownFn)
98
    {
99
        $fn = $this->getScope()->peridotBindTo($tearDownFn);
100
        array_push($this->tearDownFns, $fn);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     *
106
     * @return string
107
     */
108
    public function getDescription()
109
    {
110
        return $this->description;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     *
116
     * @return callable
117
     */
118
    public function getDefinition()
119
    {
120
        return $this->scope->peridotBindTo($this->definition);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     *
126
     * @param  TestInterface $parent
127
     * @return mixed|void
128
     */
129
    public function setParent(TestInterface $parent)
130
    {
131
        $this->parent = $parent;
132
        $this->setScope($parent->getScope());
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     *
138
     * @return TestInterface
139
     */
140
    public function getParent()
141
    {
142
        return $this->parent;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     *
148
     * @return string
149
     */
150
    public function getTitle()
151
    {
152
        $parts = [];
153
        $node = $this;
154
        while ($node != null) {
155
            array_unshift($parts, $node->getDescription());
156
            $node = $node->getParent();
157
        }
158
159
        return implode(' ', $parts);
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     *
165
     * @return bool|null
166
     */
167
    public function getPending()
168
    {
169
        return $this->pending;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     *
175
     * @param bool $state
176
     */
177
    public function setPending($state)
178
    {
179
        $this->pending = (bool) $state;
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     *
185
     * @return bool|null
186
     */
187
    public function getFocused()
188
    {
189
        return $this->focused;
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     *
195
     * @param bool $state
196
     */
197
    public function setFocused($state)
198
    {
199
        $this->focused = (bool) $state;
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     *
205
     * @return array
206
     */
207
    public function getSetupFunctions()
208
    {
209
        return $this->setUpFns;
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     *
215
     * @return array
216
     */
217
    public function getTearDownFunctions()
218
    {
219
        return $this->tearDownFns;
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     *
225
     * @param callable $fn
226
     */
227
    public function forEachNodeBottomUp(callable $fn)
228
    {
229
        $node = $this;
230
        while ($node !== null) {
231
            $fn($node);
232
            $node = $node->getParent();
233
        }
234
    }
235
236
    /**
237
     * {@inheritdoc}
238
     *
239
     * @param callable $fn
240
     */
241
    public function forEachNodeTopDown(callable $fn)
242
    {
243
        $node = $this;
244
        $nodes = [];
245
        while ($node !== null) {
246
            array_unshift($nodes, $node);
247
            $node = $node->getParent();
248
        }
249
        foreach ($nodes as $node) {
250
            $fn($node);
251
        }
252
    }
253
254
    /**
255
     * {@inheritdoc}
256
     *
257
     * @return Scope
258
     */
259
    public function getScope()
260
    {
261
        return $this->scope;
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     *
267
     * @param Scope $scope
268
     * @return mixed
269
     */
270
    public function setScope(Scope $scope)
271
    {
272
        $this->scope = $scope;
273
        return $this;
274
    }
275
276
    /**
277
     * Get the file this test belongs to.
278
     *
279
     * @return string
280
     */
281
    public function getFile()
282
    {
283
        return $this->file;
284
    }
285
286
    /**
287
     * Set the file this test belongs to.
288
     *
289
     * @param string $file
290
     */
291
    public function setFile($file)
292
    {
293
        $this->file = $file;
294
        return $this;
295
    }
296
297
    /**
298
     * {@inheritdoc}
299
     *
300
     * @param array $args
301
     * @return $this
302
     */
303
    public function setDefinitionArguments(array $args)
304
    {
305
        $this->definitionArguments = $args;
306
    }
307
308
    /**
309
     * {@inheritdoc}
310
     *
311
     * @return array
312
     */
313
    public function getDefinitionArguments()
314
    {
315
        return $this->definitionArguments;
316
    }
317
}
318