ParallelTesting.php
6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
namespace Illuminate\Testing;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Str;
class ParallelTesting
{
/**
* The container instance.
*
* @var \Illuminate\Contracts\Container\Container
*/
protected $container;
/**
* The options resolver callback.
*
* @var \Closure|null
*/
protected $optionsResolver;
/**
* The token resolver callback.
*
* @var \Closure|null
*/
protected $tokenResolver;
/**
* All of the registered "setUp" process callbacks.
*
* @var array
*/
protected $setUpProcessCallbacks = [];
/**
* All of the registered "setUp" test case callbacks.
*
* @var array
*/
protected $setUpTestCaseCallbacks = [];
/**
* All of the registered "setUp" test database callbacks.
*
* @var array
*/
protected $setUpTestDatabaseCallbacks = [];
/**
* All of the registered "tearDown" process callbacks.
*
* @var array
*/
protected $tearDownProcessCallbacks = [];
/**
* All of the registered "tearDown" test case callbacks.
*
* @var array
*/
protected $tearDownTestCaseCallbacks = [];
/**
* Create a new parallel testing instance.
*
* @param \Illuminate\Contracts\Container\Container $container
* @return void
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* Set a callback that should be used when resolving options.
*
* @param \Closure|null $callback
* @return void
*/
public function resolveOptionsUsing($resolver)
{
$this->optionsResolver = $resolver;
}
/**
* Set a callback that should be used when resolving the unique process token.
*
* @param \Closure|null $callback
* @return void
*/
public function resolveTokenUsing($resolver)
{
$this->tokenResolver = $resolver;
}
/**
* Register a "setUp" process callback.
*
* @param callable $callback
* @return void
*/
public function setUpProcess($callback)
{
$this->setUpProcessCallbacks[] = $callback;
}
/**
* Register a "setUp" test case callback.
*
* @param callable $callback
* @return void
*/
public function setUpTestCase($callback)
{
$this->setUpTestCaseCallbacks[] = $callback;
}
/**
* Register a "setUp" test database callback.
*
* @param callable $callback
* @return void
*/
public function setUpTestDatabase($callback)
{
$this->setUpTestDatabaseCallbacks[] = $callback;
}
/**
* Register a "tearDown" process callback.
*
* @param callable $callback
* @return void
*/
public function tearDownProcess($callback)
{
$this->tearDownProcessCallbacks[] = $callback;
}
/**
* Register a "tearDown" test case callback.
*
* @param callable $callback
* @return void
*/
public function tearDownTestCase($callback)
{
$this->tearDownTestCaseCallbacks[] = $callback;
}
/**
* Call all of the "setUp" process callbacks.
*
* @return void
*/
public function callSetUpProcessCallbacks()
{
$this->whenRunningInParallel(function () {
foreach ($this->setUpProcessCallbacks as $callback) {
$this->container->call($callback, [
'token' => $this->token(),
]);
}
});
}
/**
* Call all of the "setUp" test case callbacks.
*
* @param \Illuminate\Foundation\Testing\TestCase $testCase
* @return void
*/
public function callSetUpTestCaseCallbacks($testCase)
{
$this->whenRunningInParallel(function () use ($testCase) {
foreach ($this->setUpTestCaseCallbacks as $callback) {
$this->container->call($callback, [
'testCase' => $testCase,
'token' => $this->token(),
]);
}
});
}
/**
* Call all of the "setUp" test database callbacks.
*
* @param string $database
* @return void
*/
public function callSetUpTestDatabaseCallbacks($database)
{
$this->whenRunningInParallel(function () use ($database) {
foreach ($this->setUpTestDatabaseCallbacks as $callback) {
$this->container->call($callback, [
'database' => $database,
'token' => $this->token(),
]);
}
});
}
/**
* Call all of the "tearDown" process callbacks.
*
* @return void
*/
public function callTearDownProcessCallbacks()
{
$this->whenRunningInParallel(function () {
foreach ($this->tearDownProcessCallbacks as $callback) {
$this->container->call($callback, [
'token' => $this->token(),
]);
}
});
}
/**
* Call all of the "tearDown" test case callbacks.
*
* @param \Illuminate\Foundation\Testing\TestCase $testCase
* @return void
*/
public function callTearDownTestCaseCallbacks($testCase)
{
$this->whenRunningInParallel(function () use ($testCase) {
foreach ($this->tearDownTestCaseCallbacks as $callback) {
$this->container->call($callback, [
'testCase' => $testCase,
'token' => $this->token(),
]);
}
});
}
/**
* Get a parallel testing option.
*
* @param string $option
* @return mixed
*/
public function option($option)
{
$optionsResolver = $this->optionsResolver ?: function ($option) {
$option = 'LARAVEL_PARALLEL_TESTING_'.Str::upper($option);
return $_SERVER[$option] ?? false;
};
return call_user_func($optionsResolver, $option);
}
/**
* Gets a unique test token.
*
* @return string|false
*/
public function token()
{
return $token = $this->tokenResolver
? call_user_func($this->tokenResolver)
: ($_SERVER['TEST_TOKEN'] ?? false);
}
/**
* Apply the callback if tests are running in parallel.
*
* @param callable $callback
* @return void
*/
protected function whenRunningInParallel($callback)
{
if ($this->inParallel()) {
$callback();
}
}
/**
* Indicates if the current tests are been run in parallel.
*
* @return bool
*/
protected function inParallel()
{
return ! empty($_SERVER['LARAVEL_PARALLEL_TESTING']) && $this->token();
}
}