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

Suite::focusedTests()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 6
nop 0
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
1
<?php
2
namespace Peridot\Core;
3
4
/**
5
 * Suites organize tests and other suites.
6
 *
7
 * @package Peridot\Core
8
 */
9
class Suite extends AbstractTest
10
{
11
    /**
12
     * Tests belonging to this suite
13
     *
14
     * @var array
15
     */
16
    protected $tests = [];
17
18
    /**
19
     * Has the suite been halted
20
     *
21
     * @var bool
22
     */
23
    protected $halted = false;
24
25
    /**
26
     * Add a test to the suite
27
     *
28
     * @param Test $test
29
     */
30
    public function addTest(TestInterface $test)
31
    {
32
        $test->setParent($this);
33
        $this->tests[] = $test;
34
    }
35
36
    /**
37
     * Return collection of tests
38
     *
39
     * @return array
40
     */
41
    public function getTests()
42
    {
43
        return $this->tests;
44
    }
45
46
    /**
47
     * Set suite tests
48
     *
49
     * @param array $tests
50
     */
51
    public function setTests(array $tests)
52
    {
53
        $this->tests = $tests;
54
    }
55
56
    /**
57
     * Execute the Suite definition.
58
     *
59
     * @return void
60
     */
61
    public function define()
62
    {
63
        $this->eventEmitter->emit('suite.define', [$this]);
64
        call_user_func_array($this->getDefinition(), $this->getDefinitionArguments());
65
    }
66
67
    /**
68
     * Run all the specs belonging to the suite
69
     *
70
     * @param TestResult $result
71
     */
72
    public function run(TestResult $result)
73
    {
74
        $this->eventEmitter->emit('suite.start', [$this]);
75
        $this->eventEmitter->on('suite.halt', [$this, 'halt']);
76
77
        foreach ($this->focusedTests() as $test) {
78
            if ($this->halted) {
79
                break;
80
            }
81
82
            $this->runTest($test, $result);
83
        }
84
        $this->eventEmitter->emit('suite.end', [$this]);
85
    }
86
87
    /**
88
     * Put the Suite in a halted state. A halted Suite will not run or will
89
     * stop running if currently running.
90
     *
91
     * @return void
92
     */
93
    public function halt()
94
    {
95
        $this->halted = true;
96
    }
97
98
    /**
99
     * Run a test and track its results.
100
     *
101
     * @param TestInterface $test
102
     * @param TestResult $result
103
     */
104
    protected function runTest(TestInterface $test, TestResult $result)
105
    {
106
        if ($this->getPending() !== null) {
107
            $test->setPending($this->getPending());
108
        }
109
110
        $test->setEventEmitter($this->eventEmitter);
111
        $test->run($result);
112
    }
113
114
    private function focusedTests()
115
    {
116
        $tests = [];
117
        $hasFocusedTests = false;
118
119
        foreach ($this->tests as $test) {
120
            if ($test->getFocused()) {
121
                $hasFocusedTests = true;
122
                $tests[] = $test;
123
            }
124
        }
125
126
        if ($hasFocusedTests) {
127
            return $tests;
128
        }
129
130
        return $this->tests;
131
    }
132
}
133