-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathFacade.php
More file actions
342 lines (297 loc) · 11.1 KB
/
Copy pathFacade.php
File metadata and controls
342 lines (297 loc) · 11.1 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Event;
use function assert;
use function interface_exists;
use PHPUnit\Event\Telemetry\HRTime;
use PHPUnit\Event\Telemetry\SystemGarbageCollectorStatusProvider;
use PHPUnit\Runner\DeprecationCollector\Facade as DeprecationCollector;
/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class Facade
{
private static ?self $instance = null;
private Emitter $emitter;
private ?TypeMap $typeMap = null;
private ?DeferringDispatcher $deferringDispatcher = null;
private ?CollectingDispatcher $isolationDispatcher = null;
private bool $sealed = false;
public static function instance(): self
{
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
}
public static function emitter(): Emitter
{
return self::instance()->emitter;
}
public function __construct()
{
$this->emitter = $this->createDispatchingEmitter();
}
/**
* @throws EventFacadeIsSealedException
* @throws UnknownSubscriberTypeException
*/
public function registerSubscribers(Subscriber ...$subscribers): void
{
foreach ($subscribers as $subscriber) {
$this->registerSubscriber($subscriber);
}
}
/**
* @throws EventFacadeIsSealedException
* @throws UnknownSubscriberTypeException
*/
public function registerSubscriber(Subscriber $subscriber): void
{
if ($this->sealed) {
throw new EventFacadeIsSealedException;
}
$this->deferredDispatcher()->registerSubscriber($subscriber);
}
/**
* @throws EventFacadeIsSealedException
*/
public function registerTracer(Tracer\Tracer $tracer): void
{
if ($this->sealed) {
throw new EventFacadeIsSealedException;
}
$this->deferredDispatcher()->registerTracer($tracer);
}
/**
* @codeCoverageIgnore
*
* @noinspection PhpUnused
*/
public function initForIsolation(HRTime $offset): CollectingDispatcher
{
DeprecationCollector::initForIsolation();
$dispatcher = new CollectingDispatcher(
new DirectDispatcher($this->typeMap()),
);
$this->emitter = new DispatchingEmitter(
$dispatcher,
new Telemetry\System(
new Telemetry\SystemStopWatchWithOffset($offset),
new Telemetry\SystemMemoryMeter,
new SystemGarbageCollectorStatusProvider,
new Telemetry\SystemCpuTimeMeter,
),
);
$this->sealed = true;
$this->isolationDispatcher = $dispatcher;
return $dispatcher;
}
/**
* Mint an emitter that collects its events into an independent collection,
* without sealing this facade or replacing its emitter. Unlike
* initForIsolation(), which can only establish one isolated emitter for the
* whole process, any number of these can be in use at once; the parallel
* test runner uses one per concurrently running PHPT test.
*/
public function collectingEmitter(): CollectingEmitter
{
$dispatcher = new CollectingDispatcher(
new DirectDispatcher($this->typeMap()),
);
return new CollectingEmitter(
new DispatchingEmitter(
$dispatcher,
$this->createTelemetrySystem(),
),
$dispatcher,
);
}
public function forward(EventCollection $events): void
{
if ($this->isolationDispatcher !== null) {
$dispatcher = $this->isolationDispatcher;
} else {
$dispatcher = $this->deferredDispatcher();
}
foreach ($events as $event) {
$dispatcher->dispatch($event);
}
}
/**
* In a process whose event facade was initialized for isolation — a
* parallel test runner worker, for example — the collection window is
* opened on the isolation dispatcher, because that is the dispatcher the
* emitter dispatches to there; the deferring dispatcher is unused in such
* a process.
*/
public function startCollectingEvents(): void
{
if ($this->isolationDispatcher !== null) {
$this->isolationDispatcher->startCollectingEvents();
return;
}
$this->deferredDispatcher()->startCollectingEvents();
}
public function stopCollectingEvents(): EventCollection
{
if ($this->isolationDispatcher !== null) {
return $this->isolationDispatcher->stopCollectingEvents();
}
return $this->deferredDispatcher()->stopCollectingEvents();
}
public function seal(): void
{
$this->deferredDispatcher()->flush();
$this->sealed = true;
$this->emitter->testRunnerEventFacadeSealed();
}
private function createDispatchingEmitter(): DispatchingEmitter
{
return new DispatchingEmitter(
$this->deferredDispatcher(),
$this->createTelemetrySystem(),
);
}
private function createTelemetrySystem(): Telemetry\System
{
return new Telemetry\System(
new Telemetry\SystemStopWatch,
new Telemetry\SystemMemoryMeter,
new SystemGarbageCollectorStatusProvider,
new Telemetry\SystemCpuTimeMeter,
);
}
private function deferredDispatcher(): DeferringDispatcher
{
if ($this->deferringDispatcher === null) {
$this->deferringDispatcher = new DeferringDispatcher(
new DirectDispatcher($this->typeMap()),
);
}
return $this->deferringDispatcher;
}
private function typeMap(): TypeMap
{
if ($this->typeMap === null) {
$typeMap = new TypeMap;
$this->registerDefaultTypes($typeMap);
$this->typeMap = $typeMap;
}
return $this->typeMap;
}
private function registerDefaultTypes(TypeMap $typeMap): void
{
$defaultEvents = [
Application\Started::class,
Application\Finished::class,
Test\DataProviderMethodCalled::class,
Test\DataProviderMethodFinished::class,
Test\MarkedIncomplete::class,
Test\AfterLastTestMethodCalled::class,
Test\AfterLastTestMethodErrored::class,
Test\AfterLastTestMethodFailed::class,
Test\AfterLastTestMethodFinished::class,
Test\AfterTestMethodCalled::class,
Test\AfterTestMethodErrored::class,
Test\AfterTestMethodFailed::class,
Test\AfterTestMethodFinished::class,
Test\BeforeFirstTestMethodCalled::class,
Test\BeforeFirstTestMethodErrored::class,
Test\BeforeFirstTestMethodFailed::class,
Test\BeforeFirstTestMethodFinished::class,
Test\BeforeTestMethodCalled::class,
Test\BeforeTestMethodErrored::class,
Test\BeforeTestMethodFailed::class,
Test\BeforeTestMethodFinished::class,
Test\AdditionalInformationProvided::class,
Test\ComparatorRegistered::class,
Test\CustomTestMethodInvocationUsed::class,
Test\ConsideredRisky::class,
Test\AttemptErrored::class,
Test\AttemptFailed::class,
Test\DeprecationTriggered::class,
Test\Errored::class,
Test\ErrorTriggered::class,
Test\Failed::class,
Test\Finished::class,
Test\NoticeTriggered::class,
Test\Passed::class,
Test\PhpDeprecationTriggered::class,
Test\PhpNoticeTriggered::class,
Test\PhpunitDeprecationTriggered::class,
Test\PhpunitNoticeTriggered::class,
Test\PhpunitErrorTriggered::class,
Test\PhpunitWarningTriggered::class,
Test\PhpWarningTriggered::class,
Test\PostConditionCalled::class,
Test\PostConditionErrored::class,
Test\PostConditionFailed::class,
Test\PostConditionFinished::class,
Test\PreConditionCalled::class,
Test\PreConditionErrored::class,
Test\PreConditionFailed::class,
Test\PreConditionFinished::class,
Test\PreparationStarted::class,
Test\Prepared::class,
Test\PreparationErrored::class,
Test\PreparationFailed::class,
Test\PrintedUnexpectedOutput::class,
Test\Skipped::class,
Test\WarningTriggered::class,
Test\MockObjectCreated::class,
Test\MockObjectForIntersectionOfInterfacesCreated::class,
Test\PartialMockObjectCreated::class,
Test\TestStubCreated::class,
Test\TestStubForIntersectionOfInterfacesCreated::class,
TestRunner\BootstrapFinished::class,
TestRunner\Configured::class,
TestRunner\EventFacadeSealed::class,
TestRunner\ExecutionAborted::class,
TestRunner\ExecutionFinished::class,
TestRunner\ExecutionStarted::class,
TestRunner\ExtensionLoadedFromPhar::class,
TestRunner\ExtensionBootstrapped::class,
TestRunner\Finished::class,
TestRunner\Started::class,
TestRunner\DeprecationTriggered::class,
TestRunner\NoticeTriggered::class,
TestRunner\WarningTriggered::class,
TestRunner\Issue\DeprecationTriggered::class,
TestRunner\ErrorTriggered::class,
TestRunner\Issue\NoticeTriggered::class,
TestRunner\PhpDeprecationTriggered::class,
TestRunner\PhpNoticeTriggered::class,
TestRunner\PhpWarningTriggered::class,
TestRunner\Issue\WarningTriggered::class,
TestRunner\GarbageCollectionDisabled::class,
TestRunner\GarbageCollectionTriggered::class,
TestRunner\GarbageCollectionEnabled::class,
TestRunner\ChildProcessStarted::class,
TestRunner\ChildProcessErrored::class,
TestRunner\ChildProcessFinished::class,
TestRunner\StaticAnalysisForCodeCoverageFinished::class,
TestRunner\StaticAnalysisForCodeCoverageStarted::class,
TestSuite\Filtered::class,
TestSuite\Finished::class,
TestSuite\Loaded::class,
TestSuite\Skipped::class,
TestSuite\Sorted::class,
TestSuite\Started::class,
];
foreach ($defaultEvents as $eventClass) {
$subscriberInterface = $eventClass . 'Subscriber';
assert(interface_exists($subscriberInterface));
$typeMap->addMapping($subscriberInterface, $eventClass);
}
}
}