Skip to content

Commit 6d13a4c

Browse files
authored
test different versions
1 parent 1b8cd61 commit 6d13a4c

File tree

3 files changed

+304
-94
lines changed

3 files changed

+304
-94
lines changed

src/Extension/Phiremock.php

Lines changed: 12 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -25,122 +25,40 @@
2525
use Mcustiel\Phiremock\Codeception\Extension\Config;
2626
use Mcustiel\Phiremock\Codeception\Extension\PhiremockProcessManager;
2727
use Mcustiel\Phiremock\Codeception\Extension\ReadinessCheckerFactory;
28+
use Mcustiel\Phiremock\Codeception\Extension\Phiremock72;
29+
use Mcustiel\Phiremock\Codeception\Extension\Phiremock74p;
2830

2931
class Phiremock extends CodeceptionExtension
3032
{
3133
/** @var array */
32-
public static array $events = [
34+
public static $events = [
3335
'suite.before' => 'startProcess',
3436
'suite.after' => 'stopProcess',
3537
];
3638

37-
/** @var array */
38-
protected array $config = Config::DEFAULT_CONFIG;
39-
40-
/** @var PhiremockProcessManager */
41-
private $process;
42-
43-
/** @var Config */
44-
private $extensionConfig;
39+
/** Phiremock72|Phiremock74p */
40+
private $instance;
4541

4642
/** @throws ConfigurationException */
4743
public function __construct(
4844
array $config,
4945
array $options,
5046
PhiremockProcessManager $process = null
5147
) {
52-
$this->setDefaultLogsPath();
53-
parent::__construct($config, $options);
54-
$this->extensionConfig = new Config($this->config, $this->getOutputCallable());
55-
$this->initProcess($process);
48+
if (version_compare(PHP_VERSION, '7.4.0') >= 0) {
49+
$this->instance = new Phiremock74p($config, $options, $process);
50+
} else {
51+
$this->instance = new Phiremock72($config, $options, $process);
52+
}
5653
}
5754

5855
public function startProcess(SuiteEvent $event): void
5956
{
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();
57+
$this->instance->startProcess($event);
7358
}
7459

7560
public function stopProcess(): void
7661
{
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!');
62+
$this->instance->stopProcess();
14563
}
14664
}

src/Extension/PhiremockPHP72.php

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
/**
3+
* This file is part of phiremock-codeception-extension.
4+
*
5+
* phiremock-codeception-extension is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* phiremock-codeception-extension is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with phiremock-codeception-extension. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
namespace Codeception\Extension;
20+
21+
use Codeception\Event\SuiteEvent;
22+
use Codeception\Exception\ConfigurationException;
23+
use Codeception\Extension as CodeceptionExtension;
24+
use Codeception\Suite;
25+
use Mcustiel\Phiremock\Codeception\Extension\Config;
26+
use Mcustiel\Phiremock\Codeception\Extension\PhiremockProcessManager;
27+
use Mcustiel\Phiremock\Codeception\Extension\ReadinessCheckerFactory;
28+
29+
class Phiremock72 extends CodeceptionExtension
30+
{
31+
/** @var array */
32+
public static $events = [
33+
'suite.before' => 'startProcess',
34+
'suite.after' => 'stopProcess',
35+
];
36+
37+
/** @var array */
38+
protected $config = Config::DEFAULT_CONFIG;
39+
40+
/** @var PhiremockProcessManager */
41+
private $process;
42+
43+
/** @var Config */
44+
private $extensionConfig;
45+
46+
/** @throws ConfigurationException */
47+
public function __construct(
48+
array $config,
49+
array $options,
50+
PhiremockProcessManager $process = null
51+
) {
52+
$this->setDefaultLogsPath();
53+
parent::__construct($config, $options);
54+
$this->extensionConfig = new Config($this->config, $this->getOutputCallable());
55+
$this->initProcess($process);
56+
}
57+
58+
public function startProcess(SuiteEvent $event): void
59+
{
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();
73+
}
74+
75+
public function stopProcess(): void
76+
{
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!');
145+
}
146+
}

0 commit comments

Comments
 (0)