2525use Mcustiel \Phiremock \Codeception \Extension \Config ;
2626use Mcustiel \Phiremock \Codeception \Extension \PhiremockProcessManager ;
2727use Mcustiel \Phiremock \Codeception \Extension \ReadinessCheckerFactory ;
28- use Mcustiel \Phiremock \Codeception \Extension \PhiremockPHP72 ;
29- use Mcustiel \Phiremock \Codeception \Extension \PhiremockPHP74p ;
3028
3129class Phiremock extends CodeceptionExtension
3230{
@@ -36,29 +34,113 @@ class Phiremock extends CodeceptionExtension
3634 'suite.after ' => 'stopProcess ' ,
3735 ];
3836
39- /** Phiremock72|Phiremock74p */
40- private $ instance ;
37+ /** @var array */
38+ protected array $ config = Config::DEFAULT_CONFIG ;
39+
40+ /** @var PhiremockProcessManager */
41+ private $ process ;
42+
43+ /** @var Config */
44+ private $ extensionConfig ;
4145
4246 /** @throws ConfigurationException */
4347 public function __construct (
4448 array $ config ,
4549 array $ options ,
4650 PhiremockProcessManager $ process = null
4751 ) {
48- if (version_compare (PHP_VERSION , '7.4.0 ' ) >= 0 ) {
49- $ this ->instance = new PhiremockPHP74p ($ config , $ options , $ process );
50- } else {
51- $ this ->instance = new PhiremockPHP72 ($ config , $ options , $ process );
52- }
52+ $ this ->setDefaultLogsPath ();
53+ parent ::__construct ($ config , $ options );
54+ $ this ->extensionConfig = new Config ($ this ->config , $ this ->getOutputCallable ());
55+ $ this ->initProcess ($ process );
5356 }
5457
5558 public function startProcess (SuiteEvent $ event ): void
5659 {
57- $ this ->instance ->startProcess ($ event );
60+ $ this ->writeln ('Starting default phiremock instance... ' );
61+ $ suite = $ event ->getSuite ();
62+ if ($ this ->mustRunForSuite ($ suite , $ this ->extensionConfig ->getSuites ())) {
63+ $ this ->process ->start ($ this ->extensionConfig );
64+ }
65+ foreach ($ this ->extensionConfig ->getExtraInstances () as $ configInstance ) {
66+ if ($ this ->mustRunForSuite ($ suite , $ configInstance ->getSuites ())) {
67+ $ this ->writeln ('Starting extra phiremock instance... ' );
68+ $ this ->process ->start ($ configInstance );
69+ }
70+ }
71+ $ this ->executeDelay ();
72+ $ this ->waitUntilReady ();
5873 }
5974
6075 public function stopProcess (): void
6176 {
62- $ this ->instance ->stopProcess ();
77+ $ this ->writeln ('Stopping phiremock... ' );
78+ $ this ->process ->stop ();
79+ }
80+
81+ public function getOutputCallable (): callable
82+ {
83+ return function (string $ message ) {
84+ $ this ->writeln ($ message );
85+ };
86+ }
87+
88+ private function mustRunForSuite (Suite $ suite , array $ allowedSuites ): bool
89+ {
90+ return empty ($ allowedSuites ) || in_array ($ suite ->getBaseName (), $ allowedSuites , true );
91+ }
92+
93+ private function executeDelay (): void
94+ {
95+ $ delay = $ this ->extensionConfig ->getDelay ();
96+ if ($ delay > 0 ) {
97+ sleep ($ delay );
98+ }
99+ }
100+
101+ private function initProcess (?PhiremockProcessManager $ process ): void
102+ {
103+ $ this ->process = $ process ?? new PhiremockProcessManager ($ this ->getOutputCallable ());
104+ }
105+
106+ /** @throws ConfigurationException */
107+ private function setDefaultLogsPath (): void
108+ {
109+ if (!isset ($ this ->config ['logs_path ' ])) {
110+ $ this ->config ['logs_path ' ] = Config::getDefaultLogsPath ();
111+ }
112+ }
113+
114+ private function waitUntilReady (): void
115+ {
116+ if (!$ this ->extensionConfig ->waitUntilReady ()) {
117+ return ;
118+ }
119+
120+ $ this ->writeln ('Waiting until Phiremock is ready... ' );
121+
122+ $ readinessChecker = ReadinessCheckerFactory::create (
123+ $ this ->extensionConfig ->getInterface (),
124+ $ this ->extensionConfig ->getPort (),
125+ $ this ->extensionConfig ->isSecure ()
126+ );
127+
128+ $ start = \microtime (true );
129+ $ interval = $ this ->extensionConfig ->getWaitUntilReadyIntervalMicros ();
130+ $ timeout = $ this ->extensionConfig ->getWaitUntilReadyTimeout ();
131+ while (true ) {
132+ if ($ readinessChecker ->isReady ()) {
133+ break ;
134+ }
135+ \usleep ($ interval );
136+ $ elapsed = (int ) (\microtime (true ) - $ start );
137+
138+ if ($ elapsed > $ timeout ) {
139+ throw new \RuntimeException (
140+ \sprintf ('Phiremock failed to start within %d seconds ' , $ this ->extensionConfig ->getWaitUntilReadyTimeout ())
141+ );
142+ }
143+ }
144+ $ this ->writeln ('Phiremock is ready! ' );
63145 }
64146}
0 commit comments