1515use Codeception \Step ;
1616use Qameta \Allure \Allure ;
1717use Qameta \Allure \Allure as QametaAllure ;
18- use Qameta \Allure \Attribute \LinkTemplateInterface ;
1918use Qameta \Allure \Codeception \Internal \DefaultThreadDetector ;
2019use Qameta \Allure \Codeception \Internal \SuiteInfo ;
2120use Qameta \Allure \Codeception \Internal \TestLifecycle ;
2221use Qameta \Allure \Codeception \Internal \TestLifecycleInterface ;
2322use Qameta \Allure \Codeception \Setup \ThreadDetectorInterface ;
23+ use Qameta \Allure \Model \LinkType ;
2424use Qameta \Allure \Model \Status ;
2525use Qameta \Allure \Model \StatusDetails ;
2626use Qameta \Allure \Setup \DefaultStatusDetector ;
27+ use Qameta \Allure \Setup \LinkTemplate ;
28+ use Qameta \Allure \Setup \LinkTemplateInterface ;
2729use Throwable ;
2830
2931use function class_exists ;
32+ use function is_a ;
33+ use function is_array ;
3034use function is_callable ;
3135use function is_string ;
3236use function trim ;
3539
3640final class AllureCodeception extends Extension
3741{
38-
3942 private const SETUP_HOOK_PARAMETER = 'setupHook ' ;
4043 private const OUTPUT_DIRECTORY_PARAMETER = 'outputDirectory ' ;
44+ private const LINK_TEMPLATES_PARAMETER = 'linkTemplates ' ;
4145
4246 private const DEFAULT_RESULTS_DIRECTORY = 'allure-results ' ;
4347
@@ -55,11 +59,6 @@ final class AllureCodeception extends Extension
5559 Events::STEP_AFTER => 'stepAfter '
5660 ];
5761
58- /**
59- * @var array<string, LinkTemplateInterface>
60- */
61- private array $ linkTemplates = [];
62-
6362 private ?ThreadDetectorInterface $ threadDetector = null ;
6463
6564 private ?TestLifecycleInterface $ testLifecycle = null ;
@@ -76,9 +75,12 @@ public function _initialize(): void
7675 parent ::_initialize ();
7776 QametaAllure::reset ();
7877 QametaAllure::getLifecycleConfigurator ()
79- ->setStatusDetector (new StatusDetector (new DefaultStatusDetector ()));
78+ ->setStatusDetector (new StatusDetector (new DefaultStatusDetector ()))
79+ ->setOutputDirectory ($ this ->getOutputDirectory ());
80+ foreach ($ this ->getLinkTemplates () as $ linkType => $ linkTemplate ) {
81+ QametaAllure::getLifecycleConfigurator ()->addLinkTemplate ($ linkType , $ linkTemplate );
82+ }
8083 $ this ->callSetupHook ();
81- QametaAllure::setOutputDirectory ($ this ->getOutputDirectory ());
8284 }
8385
8486 private function callSetupHook (): void
@@ -115,6 +117,31 @@ private function getOutputDirectory(): string
115117 return Configuration::outputDir () . ($ outputLocal ?? self ::DEFAULT_RESULTS_DIRECTORY ) . DIRECTORY_SEPARATOR ;
116118 }
117119
120+ /**
121+ * @psalm-suppress MoreSpecificReturnType
122+ * @return iterable<LinkType, LinkTemplate>
123+ */
124+ private function getLinkTemplates (): iterable
125+ {
126+ /**
127+ * @var mixed $templatesConfig
128+ * @psalm-var array $this->config
129+ */
130+ $ templatesConfig = $ this ->config [self ::LINK_TEMPLATES_PARAMETER ] ?? [];
131+ if (!is_array ($ templatesConfig )) {
132+ $ templatesConfig = [];
133+ }
134+ foreach ($ templatesConfig as $ linkTypeName => $ linkConfig ) {
135+ if (!is_string ($ linkConfig ) || !is_string ($ linkTypeName )) {
136+ continue ;
137+ }
138+ yield LinkType::fromOptionalString ($ linkTypeName ) =>
139+ class_exists ($ linkConfig ) && is_a ($ linkConfig , LinkTemplateInterface::class, true )
140+ ? new $ linkConfig ()
141+ : new LinkTemplate ($ linkConfig );
142+ }
143+ }
144+
118145 /**
119146 * @psalm-suppress MissingDependency
120147 */
@@ -270,10 +297,10 @@ private function getTestLifecycle(): TestLifecycleInterface
270297 {
271298 return $ this ->testLifecycle ??= new TestLifecycle (
272299 Allure::getLifecycle (),
273- Allure::getResultFactory (),
274- Allure::getStatusDetector (),
300+ Allure::getConfig ()-> getResultFactory (),
301+ Allure::getConfig ()-> getStatusDetector (),
275302 $ this ->getThreadDetector (),
276- $ this -> linkTemplates ,
303+ Allure:: getConfig ()-> getLinkTemplates () ,
277304 );
278305 }
279306}
0 commit comments