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 isFocused() |
31
|
|
|
{ |
32
|
|
|
if ($this->focused === true) { |
33
|
|
|
return $this->focused; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
foreach ($this->tests as $test) { |
37
|
|
|
if ($test->isFocused()) { |
38
|
|
|
return true; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return false; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set the focused status of the test and its children according to the |
47
|
|
|
* supplied focus pattern and/or skip pattern |
48
|
|
|
* |
49
|
|
|
* @param string|null $focusPattern |
50
|
|
|
* @param string|null $skipPattern |
51
|
|
|
*/ |
52
|
|
|
public function applyFocusPatterns($focusPattern, $skipPattern = null) |
53
|
|
|
{ |
54
|
|
|
parent::applyFocusPatterns($focusPattern, $skipPattern); |
55
|
|
|
|
56
|
|
|
foreach ($this->tests as $test) { |
57
|
|
|
$test->applyFocusPatterns($focusPattern, $skipPattern); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Add a test to the suite |
63
|
|
|
* |
64
|
|
|
* @param Test $test |
65
|
|
|
*/ |
66
|
|
|
public function addTest(TestInterface $test) |
67
|
|
|
{ |
68
|
|
|
$test->setParent($this); |
69
|
|
|
$this->tests[] = $test; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return collection of tests |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public function getTests() |
78
|
|
|
{ |
79
|
|
|
return $this->tests; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set suite tests |
84
|
|
|
* |
85
|
|
|
* @param array $tests |
86
|
|
|
*/ |
87
|
|
|
public function setTests(array $tests) |
88
|
|
|
{ |
89
|
|
|
$this->tests = $tests; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Execute the Suite definition. |
94
|
|
|
* |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
public function define() |
98
|
|
|
{ |
99
|
|
|
$this->eventEmitter->emit('suite.define', [$this]); |
100
|
|
|
call_user_func_array($this->getDefinition(), $this->getDefinitionArguments()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Run all the specs belonging to the suite |
105
|
|
|
* |
106
|
|
|
* @param TestResult $result |
107
|
|
|
*/ |
108
|
|
|
public function run(TestResult $result) |
109
|
|
|
{ |
110
|
|
|
$this->eventEmitter->emit('suite.start', [$this]); |
111
|
|
|
$this->eventEmitter->on('suite.halt', [$this, 'halt']); |
112
|
|
|
|
113
|
|
|
foreach ($this->getTestsToRun() as $test) { |
114
|
|
|
if ($this->halted) { |
115
|
|
|
break; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->runTest($test, $result); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$this->eventEmitter->emit('suite.end', [$this]); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Put the Suite in a halted state. A halted Suite will not run or will |
126
|
|
|
* stop running if currently running. |
127
|
|
|
* |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
public function halt() |
131
|
|
|
{ |
132
|
|
|
$this->halted = true; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Run a test and track its results. |
137
|
|
|
* |
138
|
|
|
* @param TestInterface $test |
139
|
|
|
* @param TestResult $result |
140
|
|
|
*/ |
141
|
|
|
protected function runTest(TestInterface $test, TestResult $result) |
142
|
|
|
{ |
143
|
|
|
if ($this->getPending() !== null) { |
144
|
|
|
$test->setPending($this->getPending()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$test->setEventEmitter($this->eventEmitter); |
148
|
|
|
$test->run($result); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the subset of the defined tests that should actually be run. |
153
|
|
|
* |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
protected function getTestsToRun() |
157
|
|
|
{ |
158
|
|
|
$tests = array_filter($this->tests, function (TestInterface $test) { |
159
|
|
|
return $test->isFocused(); |
160
|
|
|
}); |
161
|
|
|
|
162
|
|
|
return empty($tests) ? $this->tests : $tests; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|