Repository navigation
Expand file tree
/
Copy pathAllureExtension.php
More file actions
115 lines (95 loc) · 3.97 KB
/
Copy pathAllureExtension.php
File metadata and controls
115 lines (95 loc) · 3.97 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
<?php
declare(strict_types=1);
namespace Qameta\Allure\PHPUnit;
use PHPUnit\Runner\Extension\Extension;
use PHPUnit\Runner\Extension\Facade;
use PHPUnit\Runner\Extension\ParameterCollection;
use PHPUnit\TextUI\Configuration\Configuration;
use Qameta\Allure\Allure;
use Qameta\Allure\Model\LinkType;
use Qameta\Allure\PHPUnit\Internal\Config;
use Qameta\Allure\PHPUnit\Internal\ConfigInterface;
use Qameta\Allure\PHPUnit\Internal\DefaultThreadDetector;
use Qameta\Allure\PHPUnit\Internal\TestLifecycle;
use Qameta\Allure\PHPUnit\Internal\TestLifecycleInterface;
use Qameta\Allure\PHPUnit\Internal\TestUpdater;
use RuntimeException;
use function file_exists;
use function is_array;
use const DIRECTORY_SEPARATOR;
final class AllureExtension implements Extension
{
private const DEFAULT_OUTPUT_DIRECTORY = 'build' . DIRECTORY_SEPARATOR . 'allure-results';
private const DEFAULT_CONFIG_FILE = 'config' . DIRECTORY_SEPARATOR . 'allure.config.php';
public function __construct(
private readonly ?TestLifecycleInterface $testLifecycle = null,
) {
}
private function createTestLifecycle(?string $configSource): TestLifecycleInterface
{
$config = new Config($this->loadConfigData($configSource));
$this->setupAllure($config);
return new TestLifecycle(
Allure::getLifecycle(),
Allure::getConfig()->getResultFactory(),
Allure::getConfig()->getStatusDetector(),
$config->getThreadDetector() ?? new DefaultThreadDetector(),
AllureAdapter::getInstance(),
new TestUpdater(Allure::getConfig()->getLinkTemplates()),
);
}
private function setupAllure(ConfigInterface $config): void
{
Allure::getLifecycleConfigurator()->setOutputDirectory(
$config->getOutputDirectory() ?? self::DEFAULT_OUTPUT_DIRECTORY,
);
foreach ($config->getLinkTemplates() as $linkType => $linkTemplate) {
Allure::getLifecycleConfigurator()->addLinkTemplate(
LinkType::fromOptionalString($linkType),
$linkTemplate,
);
}
if (!empty($config->getLifecycleHooks())) {
Allure::getLifecycleConfigurator()->addHooks(...$config->getLifecycleHooks());
}
$setupHook = $config->getSetupHook();
if (isset($setupHook)) {
$setupHook();
}
}
private function loadConfigData(?string $configFile): array
{
$fileShouldExist = isset($configFile);
$configFile ??= self::DEFAULT_CONFIG_FILE;
if (file_exists($configFile)) {
/** @psalm-var mixed $data */
$data = require $configFile;
return is_array($data)
? $data
: throw new RuntimeException("Config file {$configFile} must return array");
} elseif ($fileShouldExist) {
throw new RuntimeException("Config file {$configFile} doesn't exist");
}
return [];
}
#[\Override]
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
{
$configSource = $parameters->has('config')
? $parameters->get('config')
: null;
$testLifecycle = $this->testLifecycle ?? $this->createTestLifecycle($configSource);
$facade->registerSubscribers(
new Event\TestPreparationStartedSubscriber($testLifecycle),
new Event\TestPreparedSubscriber($testLifecycle),
new Event\TestFinishedSubscriber($testLifecycle),
new Event\TestFailedSubscriber($testLifecycle),
new Event\TestErroredSubscriber($testLifecycle),
new Event\TestMarkedIncompleteSubscriber($testLifecycle),
new Event\TestSkippedSubscriber($testLifecycle),
new Event\TestWarningTriggeredSubscriber($testLifecycle),
new Event\TestConsideredRiskySubscriber($testLifecycle),
new Event\TestPassedSubscriber($testLifecycle),
);
}
}