44
55namespace Qameta \Allure \PHPUnit ;
66
7- use LogicException ;
87use PHPUnit \Runner \AfterIncompleteTestHook ;
98use PHPUnit \Runner \AfterRiskyTestHook ;
109use PHPUnit \Runner \AfterSkippedTestHook ;
1413use PHPUnit \Runner \AfterTestHook ;
1514use PHPUnit \Runner \AfterTestWarningHook ;
1615use PHPUnit \Runner \BeforeTestHook ;
16+ use Qameta \Allure \Allure ;
17+ use Qameta \Allure \Model \LinkType ;
1718use Qameta \Allure \Model \Status ;
18- use Qameta \Allure \PHPUnit \Internal \TestLifecycleFactory ;
19- use Qameta \Allure \PHPUnit \Internal \TestLifecycleFactoryInterface ;
19+ use Qameta \Allure \PHPUnit \Internal \Config ;
20+ use Qameta \Allure \PHPUnit \Internal \ConfigInterface ;
21+ use Qameta \Allure \PHPUnit \Internal \DefaultThreadDetector ;
22+ use Qameta \Allure \PHPUnit \Internal \TestLifecycle ;
2023use Qameta \Allure \PHPUnit \Internal \TestLifecycleInterface ;
21- use Qameta \Allure \PHPUnit \Setup \ ConfiguratorInterface ;
22- use Qameta \ Allure \ PHPUnit \ Setup \ DefaultConfigurator ;
24+ use Qameta \Allure \PHPUnit \Internal \ TestUpdater ;
25+ use RuntimeException ;
2326
24- use function class_exists ;
25- use function is_a ;
27+ use function file_exists ;
28+ use function is_array ;
2629
2730use const DIRECTORY_SEPARATOR ;
2831
@@ -39,41 +42,83 @@ final class AllureExtension implements
3942{
4043 private const DEFAULT_OUTPUT_DIRECTORY = 'build ' . DIRECTORY_SEPARATOR . 'allure-results ' ;
4144
45+ private const DEFAULT_CONFIG_FILE = 'config ' . DIRECTORY_SEPARATOR . 'allure.config.php ' ;
46+
4247 private TestLifecycleInterface $ testLifecycle ;
4348
4449 public function __construct (
45- ?string $ outputDirectory = null ,
46- string |ConfiguratorInterface |null $ configurator = null ,
47- mixed ...$ args ,
50+ string |array |ConfigInterface |TestLifecycleInterface |null $ configOrTestLifecycle = null ,
4851 ) {
49- if (!$ configurator instanceof ConfiguratorInterface) {
50- $ configurator = $ this ->createConfigurator (
51- $ configurator ?? DefaultConfigurator::class,
52- ...$ args ,
52+ $ this ->testLifecycle = $ configOrTestLifecycle instanceof TestLifecycleInterface
53+ ? $ configOrTestLifecycle
54+ : $ this ->createTestLifecycle ($ configOrTestLifecycle );
55+ }
56+
57+ private function createTestLifecycle (string |array |ConfigInterface |null $ configSource ): TestLifecycleInterface
58+ {
59+ $ config = $ configSource instanceof ConfigInterface
60+ ? $ configSource
61+ : $ this ->loadConfig ($ configSource );
62+
63+ $ this ->setupAllure ($ config );
64+
65+ return new TestLifecycle (
66+ Allure::getLifecycle (),
67+ Allure::getConfig ()->getResultFactory (),
68+ Allure::getConfig ()->getStatusDetector (),
69+ $ config ->getThreadDetector () ?? new DefaultThreadDetector (),
70+ AllureAdapter::getInstance (),
71+ new TestUpdater (Allure::getConfig ()->getLinkTemplates ()),
72+ );
73+ }
74+
75+ private function setupAllure (ConfigInterface $ config ): void
76+ {
77+ Allure::setOutputDirectory ($ config ->getOutputDirectory () ?? self ::DEFAULT_OUTPUT_DIRECTORY );
78+
79+ foreach ($ config ->getLinkTemplates () as $ linkType => $ linkTemplate ) {
80+ Allure::getLifecycleConfigurator ()->addLinkTemplate (
81+ LinkType::fromOptionalString ($ linkType ),
82+ $ linkTemplate ,
5383 );
5484 }
55- $ configurator ->setupAllure ($ outputDirectory ?? self ::DEFAULT_OUTPUT_DIRECTORY );
56- $ this ->testLifecycle = $ this ->createTestLifecycleInterface ($ configurator );
85+
86+ if (!empty ($ config ->getLifecycleHooks ())) {
87+ Allure::getLifecycleConfigurator ()->addHooks (...$ config ->getLifecycleHooks ());
88+ }
89+
90+ $ setupHook = $ config ->getSetupHook ();
91+ if (isset ($ setupHook )) {
92+ $ setupHook ();
93+ }
5794 }
5895
59- private function createConfigurator (string $ class , mixed ... $ args ): ConfiguratorInterface
96+ private function loadConfig (string | array | null $ configSource ): ConfigInterface
6097 {
61- return
62- class_exists ( $ class ) &&
63- is_a ( $ class , ConfiguratorInterface::class, true )
64- ? new $ class (... $ args )
65- : throw new LogicException ( " Invalid configurator class: { $ class }" );
98+ return new Config (
99+ is_array ( $ configSource )
100+ ? $ configSource
101+ : $ this -> loadConfigData ( $ configSource ),
102+ );
66103 }
67104
68- private function createTestLifecycleInterface ( ConfiguratorInterface $ configurator ): TestLifecycleInterface
105+ private function loadConfigData (? string $ configFile ): array
69106 {
70- $ testLifecycleFactory = $ configurator instanceof TestLifecycleFactoryInterface
71- ? $ configurator
72- : new TestLifecycleFactory ();
107+ $ fileShouldExist = isset ($ configFile );
108+ $ configFile ??= self ::DEFAULT_CONFIG_FILE ;
109+ if (file_exists ($ configFile )) {
110+ /** @psalm-var mixed $data */
111+ $ data = require $ configFile ;
112+
113+ return is_array ($ data )
114+ ? $ data
115+ : throw new RuntimeException ("Config file {$ configFile } must return array " );
116+ } elseif ($ fileShouldExist ) {
117+ throw new RuntimeException ("Config file {$ configFile } doesn't exist " );
118+ }
73119
74- return $ testLifecycleFactory -> createTestLifecycle ( $ configurator ) ;
120+ return [] ;
75121 }
76-
77122 public function executeBeforeTest (string $ test ): void
78123 {
79124 $ this
0 commit comments