25
25
use Mcustiel \Phiremock \Codeception \Extension \Config ;
26
26
use Mcustiel \Phiremock \Codeception \Extension \PhiremockProcessManager ;
27
27
use Mcustiel \Phiremock \Codeception \Extension \ReadinessCheckerFactory ;
28
- use Mcustiel \Phiremock \Codeception \Extension \PhiremockPHP72 ;
29
- use Mcustiel \Phiremock \Codeception \Extension \PhiremockPHP74p ;
30
28
31
29
class Phiremock extends CodeceptionExtension
32
30
{
@@ -36,29 +34,113 @@ class Phiremock extends CodeceptionExtension
36
34
'suite.after ' => 'stopProcess ' ,
37
35
];
38
36
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 ;
41
45
42
46
/** @throws ConfigurationException */
43
47
public function __construct (
44
48
array $ config ,
45
49
array $ options ,
46
50
PhiremockProcessManager $ process = null
47
51
) {
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 );
53
56
}
54
57
55
58
public function startProcess (SuiteEvent $ event ): void
56
59
{
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 ();
58
73
}
59
74
60
75
public function stopProcess (): void
61
76
{
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! ' );
63
145
}
64
146
}
0 commit comments