Completed
Push — focused-specs ( 446344 )
by Brian
01:57 queued 27s
created

Suite::define()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
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
     * {@inheritdoc}
27
     *
28
     * @return bool
29
     */
30
    public function getFocused()
31
    {
32
        foreach ($this->tests as $test) {
33
            if ($test->getFocused()) {
34
                return true;
35
            }
36
        }
37
38
        return $this->focused;
39
    }
40
41
    /**
42
     * Add a test to the suite
43
     *
44
     * @param Test $test
45
     */
46
    public function addTest(TestInterface $test)
47
    {
48
        $test->setParent($this);
49
        $this->tests[] = $test;
50
    }
51
52
    /**
53
     * Return collection of tests
54
     *
55
     * @return array
56
     */
57
    public function getTests()
58
    {
59
        return $this->tests;
60
    }
61
62
    /**
63
     * Set suite tests
64
     *
65
     * @param array $tests
66
     */
67
    public function setTests(array $tests)
68
    {
69
        $this->tests = $tests;
70
    }
71
72
    /**
73
     * Execute the Suite definition.
74
     *
75
     * @return void
76
     */
77
    public function define()
78
    {
79
        $this->eventEmitter->emit('suite.define', [$this]);
80
        call_user_func_array($this->getDefinition(), $this->getDefinitionArguments());
81
    }
82
83
    /**
84
     * Run all the specs belonging to the suite
85
     *
86
     * @param TestResult $result
87
     */
88
    public function run(TestResult $result)
89
    {
90
        $this->eventEmitter->emit('suite.start', [$this]);
91
        $this->eventEmitter->on('suite.halt', [$this, 'halt']);
92
93
        foreach ($this->focusedTests() as $test) {
94
            if ($this->halted) {
95
                break;
96
            }
97
98
            $this->runTest($test, $result);
99
        }
100
101
        $this->eventEmitter->emit('suite.end', [$this]);
102
    }
103
104
    /**
105
     * Put the Suite in a halted state. A halted Suite will not run or will
106
     * stop running if currently running.
107
     *
108
     * @return void
109
     */
110
    public function halt()
111
    {
112
        $this->halted = true;
113
    }
114
115
    /**
116
     * Run a test and track its results.
117
     *
118
     * @param TestInterface $test
119
     * @param TestResult $result
120
     */
121
    protected function runTest(TestInterface $test, TestResult $result)
122
    {
123
        if ($this->getPending() !== null) {
124
            $test->setPending($this->getPending());
125
        }
126
127
        $test->setEventEmitter($this->eventEmitter);
128
        $test->run($result);
129
    }
130
131
    private function focusedTests()
132
    {
133
        $tests = [];
134
        $hasFocusedTests = false;
135
136
        foreach ($this->tests as $test) {
137
            if ($test->getFocused()) {
138
                $hasFocusedTests = true;
139
                $tests[] = $test;
140
            }
141
        }
142
143
        if ($hasFocusedTests) {
144
            return $tests;
145
        }
146
147
        return $this->tests;
148
    }
149
}
150