-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathAllureCodeception.php
More file actions
321 lines (289 loc) · 9.04 KB
/
AllureCodeception.php
File metadata and controls
321 lines (289 loc) · 9.04 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
<?php
declare(strict_types=1);
namespace Qameta\Allure\Codeception;
use Codeception\Configuration;
use Codeception\Extension;
use Codeception\Event\FailEvent;
use Codeception\Event\StepEvent;
use Codeception\Event\SuiteEvent;
use Codeception\Event\TestEvent;
use Codeception\Events;
use Codeception\Exception\ConfigurationException;
use Qameta\Allure\Allure;
use Qameta\Allure\Allure as QametaAllure;
use Qameta\Allure\Codeception\Internal\DefaultThreadDetector;
use Qameta\Allure\Codeception\Internal\SuiteInfo;
use Qameta\Allure\Codeception\Internal\TestLifecycle;
use Qameta\Allure\Codeception\Internal\TestLifecycleInterface;
use Qameta\Allure\Codeception\Setup\ThreadDetectorInterface;
use Qameta\Allure\Model\LinkType;
use Qameta\Allure\Model\Status;
use Qameta\Allure\Model\StatusDetails;
use Qameta\Allure\Setup\DefaultStatusDetector;
use Qameta\Allure\Setup\LinkTemplate;
use Qameta\Allure\Setup\LinkTemplateInterface;
use function class_exists;
use function is_a;
use function is_array;
use function is_callable;
use function is_string;
use function trim;
use const DIRECTORY_SEPARATOR;
final class AllureCodeception extends Extension
{
private const SETUP_HOOK_PARAMETER = 'setupHook';
private const OUTPUT_DIRECTORY_PARAMETER = 'outputDirectory';
private const LINK_TEMPLATES_PARAMETER = 'linkTemplates';
private const DEFAULT_RESULTS_DIRECTORY = 'allure-results';
protected static array $events = [
Events::MODULE_INIT => 'moduleInit',
Events::SUITE_BEFORE => 'suiteBefore',
Events::SUITE_AFTER => 'suiteAfter',
Events::TEST_START => 'testStart',
Events::TEST_FAIL => 'testFail',
Events::TEST_ERROR => 'testError',
Events::TEST_INCOMPLETE => 'testIncomplete',
Events::TEST_SKIPPED => 'testSkipped',
Events::TEST_SUCCESS => 'testSuccess',
Events::TEST_END => 'testEnd',
Events::STEP_BEFORE => 'stepBefore',
Events::STEP_AFTER => 'stepAfter'
];
private ?ThreadDetectorInterface $threadDetector = null;
private ?TestLifecycleInterface $testLifecycle = null;
/**
* {@inheritDoc}
*
* @throws ConfigurationException
* phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
*/
public function _initialize(): void
{
parent::_initialize();
$this->reconfigure();
}
/**
* @throws ConfigurationException
*/
public function moduleInit(): void
{
$this->reconfigure();
}
private function reconfigure(): void
{
QametaAllure::reset();
$this->testLifecycle = null;
$this->threadDetector = null;
QametaAllure::getLifecycleConfigurator()
->setOutputDirectory($this->getOutputDirectory());
foreach ($this->getLinkTemplates() as $linkType => $linkTemplate) {
QametaAllure::getLifecycleConfigurator()->addLinkTemplate($linkType, $linkTemplate);
}
$this->callSetupHook();
}
private function callSetupHook(): void
{
/**
* @var mixed $hookClass
* @psalm-var array $this->config
*/
$hookClass = $this->config[self::SETUP_HOOK_PARAMETER] ?? '';
/** @psalm-suppress MixedMethodCall */
$hook = is_string($hookClass) && class_exists($hookClass)
? new $hookClass()
: null;
if (is_callable($hook)) {
$hook();
}
}
/**
* @throws ConfigurationException
*/
private function getOutputDirectory(): string
{
/**
* @var mixed $outputCfg
* @psalm-var array $this->config
*/
$outputCfg = $this->config[self::OUTPUT_DIRECTORY_PARAMETER] ?? null;
$outputLocal = is_string($outputCfg)
? trim($outputCfg, '\\/')
: null;
return Configuration::outputDir() . ($outputLocal ?? self::DEFAULT_RESULTS_DIRECTORY) . DIRECTORY_SEPARATOR;
}
/**
* @psalm-suppress MoreSpecificReturnType
* @return iterable<LinkType, LinkTemplate>
*/
private function getLinkTemplates(): iterable
{
/**
* @var mixed $templatesConfig
* @psalm-var array $this->config
*/
$templatesConfig = $this->config[self::LINK_TEMPLATES_PARAMETER] ?? [];
if (!is_array($templatesConfig)) {
$templatesConfig = [];
}
foreach ($templatesConfig as $linkTypeName => $linkConfig) {
if (!is_string($linkConfig) || !is_string($linkTypeName)) {
continue;
}
yield LinkType::fromOptionalString($linkTypeName) =>
class_exists($linkConfig) && is_a($linkConfig, LinkTemplateInterface::class, true)
? new $linkConfig()
: new LinkTemplate($linkConfig);
}
}
/**
* @psalm-suppress MissingDependency
*/
public function suiteBefore(SuiteEvent $suiteEvent): void
{
/** @psalm-suppress InternalMethod */
$suiteName = $suiteEvent->getSuite()?->getName();
if (!isset($suiteName)) {
return;
}
$this
->getTestLifecycle()
->switchToSuite(new SuiteInfo($suiteName));
}
public function suiteAfter(): void
{
$this
->getTestLifecycle()
->resetSuite();
}
/**
* @psalm-suppress MissingDependency
*/
public function testStart(TestEvent $testEvent): void
{
$test = $testEvent->getTest();
$this
->getTestLifecycle()
->switchToTest($test)
->create()
->updateTest()
->startTest();
}
private function getThreadDetector(): ThreadDetectorInterface
{
return $this->threadDetector ??= new DefaultThreadDetector();
}
/**
* @psalm-suppress MissingDependency
*/
public function testError(FailEvent $failEvent): void
{
$this
->getTestLifecycle()
->switchToTest($failEvent->getTest())
->updateTestFailure(
$failEvent->getFail(),
Status::broken(),
);
}
/**
* @psalm-suppress MissingDependency
*/
public function testFail(FailEvent $failEvent): void
{
$error = $failEvent->getFail();
$this
->getTestLifecycle()
->switchToTest($failEvent->getTest())
->updateTestFailure(
$failEvent->getFail(),
Status::failed(),
new StatusDetails(message: $error->getMessage(), trace: $error->getTraceAsString()),
);
}
/**
* @psalm-suppress MissingDependency
*/
public function testIncomplete(FailEvent $failEvent): void
{
$error = $failEvent->getFail();
$this
->getTestLifecycle()
->switchToTest($failEvent->getTest())
->updateTestFailure(
$error,
Status::broken(),
new StatusDetails(message: $error->getMessage(), trace: $error->getTraceAsString()),
);
}
/**
* @psalm-suppress MissingDependency
*/
public function testSkipped(FailEvent $failEvent): void
{
$error = $failEvent->getFail();
$this
->getTestLifecycle()
->switchToTest($failEvent->getTest())
->updateTestFailure(
$error,
Status::skipped(),
new StatusDetails(message: $error->getMessage(), trace: $error->getTraceAsString()),
);
}
/**
* @psalm-suppress MissingDependency
*/
public function testSuccess(TestEvent $testEvent): void
{
$this
->getTestLifecycle()
->switchToTest($testEvent->getTest())
->updateTestSuccess();
}
/**
* @psalm-suppress MissingDependency
*/
public function testEnd(TestEvent $testEvent): void
{
$this
->getTestLifecycle()
->switchToTest($testEvent->getTest())
->updateTestResult()
->attachReports()
->stopTest();
}
/**
* @psalm-suppress MissingDependency
*/
public function stepBefore(StepEvent $stepEvent): void
{
$this
->getTestLifecycle()
->switchToTest($stepEvent->getTest())
->startStep($stepEvent->getStep())
->updateStep();
}
/**
* @psalm-suppress MissingDependency
*/
public function stepAfter(StepEvent $stepEvent): void
{
$this
->getTestLifecycle()
->switchToTest($stepEvent->getTest())
->switchToStep($stepEvent->getStep())
->updateStepResult()
->stopStep();
}
private function getTestLifecycle(): TestLifecycleInterface
{
return $this->testLifecycle ??= new TestLifecycle(
Allure::getLifecycle(),
Allure::getConfig()->getResultFactory(),
Allure::getConfig()->getStatusDetector(),
$this->getThreadDetector(),
Allure::getConfig()->getLinkTemplates(),
$_ENV,
);
}
}