|
1
|
|
|
<?php |
|
2
|
|
|
namespace Peridot\Runner; |
|
3
|
|
|
|
|
4
|
|
|
use Evenement\EventEmitter; |
|
5
|
|
|
use Peridot\Core\HasEventEmitterTrait; |
|
6
|
|
|
use Peridot\Core\Test; |
|
7
|
|
|
use Peridot\Core\Suite; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Context tracks the state of the runner - i.e the current Suite, and provides access to |
|
11
|
|
|
* Peridot's global state. |
|
12
|
|
|
* |
|
13
|
|
|
* @package Peridot\Runner |
|
14
|
|
|
*/ |
|
15
|
|
|
final class Context |
|
16
|
|
|
{ |
|
17
|
|
|
use HasEventEmitterTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $suites; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $file; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Context |
|
31
|
|
|
*/ |
|
32
|
|
|
private static $instance = null; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Private constructor |
|
36
|
|
|
*/ |
|
37
|
|
|
private function __construct() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->clear(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Clear the internal suite structure. |
|
44
|
|
|
* |
|
45
|
|
|
* @return void |
|
46
|
|
|
*/ |
|
47
|
|
|
public function clear() |
|
48
|
|
|
{ |
|
49
|
|
|
$this->suites = [new Suite("", function () { |
|
50
|
|
|
//noop |
|
51
|
|
|
}, false)]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set the file for the context. This file |
|
56
|
|
|
* generally represents the current file being used |
|
57
|
|
|
* to load suites. |
|
58
|
|
|
* |
|
59
|
|
|
* @param $path |
|
60
|
|
|
* @return void |
|
61
|
|
|
*/ |
|
62
|
|
|
public function setFile($path) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->file = $path; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getFile() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->file; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return \Peridot\Core\Suite |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getCurrentSuite() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->suites[0]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Creates a suite and adds it to the current suite. The newly |
|
85
|
|
|
* created suite will become the new "current" suite |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $description |
|
88
|
|
|
* @param callable $fn |
|
89
|
|
|
* @param bool|null $pending |
|
90
|
|
|
* @param bool $focused |
|
91
|
|
|
*/ |
|
92
|
|
|
public function addSuite($description, callable $fn, $pending = null, $focused = false) |
|
93
|
|
|
{ |
|
94
|
|
|
$suite = $this->createSuite($description, $fn, $pending, $focused); |
|
95
|
|
|
|
|
96
|
|
|
$this->getCurrentSuite()->addTest($suite); |
|
97
|
|
|
array_unshift($this->suites, $suite); |
|
98
|
|
|
$suite->define(); |
|
99
|
|
|
array_shift($this->suites); |
|
100
|
|
|
|
|
101
|
|
|
return $suite; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Create a test and add it to the current suite |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $description |
|
108
|
|
|
* @param callable $fn |
|
109
|
|
|
* @param bool|null $pending |
|
110
|
|
|
* @param bool $focused |
|
111
|
|
|
*/ |
|
112
|
|
|
public function addTest($description, callable $fn = null, $pending = null, $focused = false) |
|
113
|
|
|
{ |
|
114
|
|
|
$test = new Test($description, $fn, $focused); |
|
115
|
|
|
if ($pending !== null) { |
|
116
|
|
|
$test->setPending($pending); |
|
117
|
|
|
} |
|
118
|
|
|
$test->setFile($this->file); |
|
119
|
|
|
$this->getCurrentSuite()->addTest($test); |
|
120
|
|
|
|
|
121
|
|
|
return $test; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Add a setup function for all tests in the |
|
126
|
|
|
* current suite |
|
127
|
|
|
* |
|
128
|
|
|
* @param callable $fn |
|
129
|
|
|
*/ |
|
130
|
|
|
public function addSetupFunction(callable $fn) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->getCurrentSuite()->addSetupFunction($fn); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Add a tear down function for all tests in the current suite |
|
137
|
|
|
* |
|
138
|
|
|
* @param callable $fn |
|
139
|
|
|
*/ |
|
140
|
|
|
public function addTearDownFunction(callable $fn) |
|
141
|
|
|
{ |
|
142
|
|
|
$this->getCurrentSuite()->addTearDownFunction($fn); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Singleton access to Context |
|
147
|
|
|
* |
|
148
|
|
|
* @return Context |
|
149
|
|
|
*/ |
|
150
|
|
|
public static function getInstance() |
|
151
|
|
|
{ |
|
152
|
|
|
if (self::$instance === null) { |
|
153
|
|
|
self::$instance = new Context(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return self::$instance; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Create a Suite based on the state of the Context. |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $description |
|
163
|
|
|
* @param callable $fn |
|
164
|
|
|
* @param bool|null $pending |
|
165
|
|
|
* @param bool $focused |
|
166
|
|
|
* @return Suite |
|
167
|
|
|
*/ |
|
168
|
|
|
private function createSuite($description, callable $fn, $pending, $focused) |
|
169
|
|
|
{ |
|
170
|
|
|
$suite = new Suite($description, $fn, $focused); |
|
171
|
|
|
if ($pending !== null) { |
|
172
|
|
|
$suite->setPending($pending); |
|
173
|
|
|
} |
|
174
|
|
|
$suite->setFile($this->file); |
|
175
|
|
|
$suite->setEventEmitter($this->getEventEmitter()); |
|
176
|
|
|
return $suite; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|