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