-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathTestLifecycle.php
More file actions
392 lines (333 loc) · 11.9 KB
/
Copy pathTestLifecycle.php
File metadata and controls
392 lines (333 loc) · 11.9 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
declare(strict_types=1);
namespace Qameta\Allure\Codeception\Internal;
use Codeception\Step;
use Codeception\Test\Cept;
use Codeception\Test\Cest;
use Codeception\Test\Gherkin;
use Codeception\Test\TestCaseWrapper;
use Codeception\TestInterface;
use Qameta\Allure\AllureLifecycleInterface;
use Qameta\Allure\Codeception\Setup\ThreadDetectorInterface;
use Qameta\Allure\Io\DataSourceFactory;
use Qameta\Allure\Model\EnvProvider;
use Qameta\Allure\Model\ModelProviderChain;
use Qameta\Allure\Model\Parameter;
use Qameta\Allure\Model\ResultFactoryInterface;
use Qameta\Allure\Model\Status;
use Qameta\Allure\Model\StatusDetails;
use Qameta\Allure\Model\StepResult;
use Qameta\Allure\Model\TestResult;
use Qameta\Allure\Setup\LinkTemplateCollectionInterface;
use Qameta\Allure\Setup\StatusDetectorInterface;
use RuntimeException;
use Throwable;
use WeakMap;
use function array_filter;
use function file_exists;
use function is_file;
use function is_string;
final class TestLifecycle implements TestLifecycleInterface
{
private ?SuiteInfo $currentSuite = null;
private ?TestInfo $currentTest = null;
private ?TestStartInfo $currentTestStart = null;
private ?StepStartInfo $currentStepStart = null;
/**
* @psalm-var WeakMap<Step, StepStartInfo>
*/
private WeakMap $stepStarts;
public function __construct(
private string $rootDir,
private AllureLifecycleInterface $lifecycle,
private ResultFactoryInterface $resultFactory,
private StatusDetectorInterface $statusDetector,
private ThreadDetectorInterface $threadDetector,
private LinkTemplateCollectionInterface $linkTemplates,
private array $env,
) {
/** @psalm-var WeakMap<Step, StepStartInfo> $this->stepStarts */
$this->stepStarts = new WeakMap();
}
public function getCurrentSuite(): SuiteInfo
{
return $this->currentSuite ?? throw new RuntimeException("Current suite not found");
}
public function getCurrentTest(): TestInfo
{
return $this->currentTest ?? throw new RuntimeException("Current test not found");
}
public function getCurrentTestStart(): TestStartInfo
{
return $this->currentTestStart ?? throw new RuntimeException("Current test start not found");
}
public function getCurrentStepStart(): StepStartInfo
{
return $this->currentStepStart ?? throw new RuntimeException("Current step start not found");
}
#[\Override]
public function switchToSuite(SuiteInfo $suiteInfo): self
{
$this->currentSuite = $suiteInfo;
return $this;
}
#[\Override]
public function resetSuite(): self
{
$this->currentSuite = null;
return $this;
}
#[\Override]
public function switchToTest(object $test): self
{
$thread = $this->threadDetector->getThread();
$this->lifecycle->switchThread($thread);
$this->currentTest = $this
->getTestInfoBuilder($test)
->build(
$this->threadDetector->getHost(),
$thread,
);
return $this;
}
private function getTestInfoBuilder(object $test): TestInfoBuilderInterface
{
return match (true) {
$test instanceof Cest => new CestInfoBuilder($test),
$test instanceof Gherkin => new GherkinInfoBuilder($this->rootDir, $test),
$test instanceof Cept => new CeptInfoBuilder($this->rootDir, $test),
$test instanceof TestCaseWrapper => new UnitInfoBuilder($test),
default => new UnknownInfoBuilder($test),
};
}
#[\Override]
public function create(): self
{
$containerResult = $this->resultFactory->createContainer();
$this->lifecycle->startContainer($containerResult);
$testResult = $this->resultFactory->createTest();
$this->lifecycle->scheduleTest($testResult, $containerResult->getUuid());
$this->currentTestStart = new TestStartInfo(
containerUuid: $containerResult->getUuid(),
testUuid: $testResult->getUuid(),
);
return $this;
}
#[\Override]
public function updateTest(): self
{
$test = $this->getCurrentTest();
$provider = new ModelProviderChain(
new EnvProvider($this->env),
...SuiteProvider::createForChain($this->getCurrentSuite(), $this->linkTemplates),
...TestInfoProvider::createForChain($test),
...$this->createModelProvidersForTest($test->getOriginalTest()),
);
$this->lifecycle->updateTest(
fn (TestResult $t) => $t
->setName($provider->getDisplayName())
->setFullName($provider->getFullName())
->setTitlePath(...$test->getTitlePath())
->setDescription($provider->getDescription())
->setDescriptionHtml($provider->getDescriptionHtml())
->addLinks(...$provider->getLinks())
->addLabels(...$provider->getLabels())
->addParameters(...$provider->getParameters()),
$this->getCurrentTestStart()->getTestUuid(),
);
return $this;
}
private function createModelProvidersForTest(mixed $test): array
{
return match (true) {
$test instanceof Cest => CestProvider::createForChain($test, $this->linkTemplates),
$test instanceof Gherkin => GherkinProvider::createForChain($test),
$test instanceof Cept => CeptProvider::createForChain($test, $this->linkTemplates),
$test instanceof TestCaseWrapper => UnitProvider::createForChain($test, $this->linkTemplates),
default => [],
};
}
#[\Override]
public function startTest(): self
{
$this->lifecycle->startTest($this->getCurrentTestStart()->getTestUuid());
return $this;
}
#[\Override]
public function stopTest(): self
{
$testUuid = $this->getCurrentTestStart()->getTestUuid();
$this
->lifecycle
->stopTest($testUuid);
$this->lifecycle->writeTest($testUuid);
$containerUuid = $this->getCurrentTestStart()->getContainerUuid();
$this
->lifecycle
->stopContainer($containerUuid);
$this->lifecycle->writeContainer($containerUuid);
$this->currentTest = null;
$this->currentTestStart = null;
return $this;
}
#[\Override]
public function updateTestFailure(
Throwable $error,
?Status $status = null,
?StatusDetails $statusDetails = null,
): self {
$this->lifecycle->updateTest(
fn (TestResult $t) => $t
->setStatus($status ?? $this->statusDetector->getStatus($error))
->setStatusDetails($statusDetails ?? $this->statusDetector->getStatusDetails($error)),
);
return $this;
}
#[\Override]
public function updateTestSuccess(): self
{
$this->lifecycle->updateTest(
fn (TestResult $t) => $t->setStatus(Status::passed()),
);
return $this;
}
#[\Override]
public function attachReports(): self
{
$originalTest = $this->getCurrentTest()->getOriginalTest();
if ($originalTest instanceof TestInterface) {
$artifacts = $originalTest->getMetadata()->getReports();
/**
* @psalm-var mixed $artifact
*/
foreach ($artifacts as $name => $artifact) {
$attachment = $this
->resultFactory
->createAttachment()
->setName((string) $name);
if (!is_string($artifact)) {
continue;
}
$dataSource = @file_exists($artifact) && is_file($artifact)
? DataSourceFactory::fromFile($artifact)
: DataSourceFactory::fromString($artifact);
$this
->lifecycle
->addAttachment($attachment, $dataSource);
}
}
return $this;
}
#[\Override]
public function updateTestResult(): self
{
$this->lifecycle->updateTest(
fn (TestResult $t) => $t
->setTestCaseId($testCaseId = $this->buildTestCaseId($this->getCurrentTest(), ...$t->getParameters()))
->setHistoryId($this->buildHistoryId($testCaseId, $this->getCurrentTest(), ...$t->getParameters())),
$this->getCurrentTestStart()->getTestUuid(),
);
return $this;
}
private function buildTestCaseId(TestInfo $testInfo, Parameter ...$parameters): string
{
$parameterNames = implode(
'::',
array_map(
fn (Parameter $parameter): string => $parameter->getName(),
array_filter(
$parameters,
fn (Parameter $parameter): bool => !$parameter->getExcluded(),
),
),
);
return md5("{$testInfo->getSignature()}::$parameterNames");
}
private function buildHistoryId(string $testCaseId, TestInfo $testInfo, Parameter ...$parameters): string
{
$parameterNames = implode(
'::',
array_map(
fn (Parameter $parameter): string => $parameter->getValue() ?? '',
array_filter(
$parameters,
fn (Parameter $parameter): bool => !$parameter->getExcluded(),
),
),
);
return md5("$testCaseId::{$testInfo->getSignature()}::$parameterNames");
}
#[\Override]
public function startStep(Step $step): self
{
$stepResult = $this->resultFactory->createStep();
$this->lifecycle->startStep($stepResult);
$stepStart = new StepStartInfo(
$step,
$stepResult->getUuid(),
);
$this->stepStarts[$step] = $stepStart;
$this->currentStepStart = $stepStart;
return $this;
}
#[\Override]
public function switchToStep(Step $step): self
{
$this->currentStepStart =
$this->stepStarts[$step] ?? throw new RuntimeException("Step start info not found");
return $this;
}
#[\Override]
public function stopStep(): self
{
$stepStart = $this->getCurrentStepStart();
$this->lifecycle->stopStep($stepStart->getUuid());
/**
* @psalm-var Step $step
* @psalm-var StepStartInfo $storedStart
*/
foreach ($this->stepStarts as $step => $storedStart) {
if ($storedStart === $stepStart) {
unset($this->stepStarts[$step]);
}
}
$this->currentStepStart = null;
return $this;
}
#[\Override]
public function updateStep(): self
{
$stepStart = $this->getCurrentStepStart();
$step = $stepStart->getOriginalStep();
$params = [];
/** @psalm-var mixed $value */
foreach ($step->getArguments() as $name => $value) {
$params[] = new Parameter(
is_int($name) ? "#$name" : $name,
ArgumentAsString::get($value),
);
}
/** @var mixed $humanizedAction */
$humanizedAction = $step->getHumanizedActionWithoutArguments();
$this->lifecycle->updateStep(
fn (StepResult $s) => $s
->setName(is_string($humanizedAction) ? $humanizedAction : null)
->setParameters(...$params),
$stepStart->getUuid(),
);
return $this;
}
#[\Override]
public function updateStepResult(): self
{
$this->lifecycle->updateStep(
fn (StepResult $s) => $s
->setStatus(
$this->getCurrentStepStart()->getOriginalStep()->hasFailed()
? Status::failed()
: Status::passed(),
),
);
return $this;
}
}