Skip to content

Commit fa07052

Browse files
authored
Needed updates for version 3.x supporting codecetion 5 (#54)
* Needed updates for version 3.x supporting codecetion 5 * Removed unneeded checks from scrutinizer * Removed wrongly commited file
1 parent 5b6fca3 commit fa07052

9 files changed

+102
-324
lines changed

.scrutinizer.yml

-12
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,6 @@ build:
1414
- php-scrutinizer-run
1515
- ./vendor/bin/codecept run
1616
nodes:
17-
php72:
18-
environment:
19-
php:
20-
version: 7.2
21-
php73:
22-
environment:
23-
php:
24-
version: 7.3
25-
php74:
26-
environment:
27-
php:
28-
version: 7.4
2917
php80:
3018
environment:
3119
php:

codeception.https.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
actor: Tester
22
paths:
33
tests: tests
4-
log: tests/_output
4+
output: tests/_output
55
data: tests/_data
66
support: tests/_support
77
envs: tests/_envs

codeception.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
actor: Tester
22
paths:
33
tests: tests
4-
log: tests/_output
4+
output: tests/_output
55
data: tests/_data
66
support: tests/_support
77
envs: tests/_envs

composer.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@
3333
"description" : "Codeception extension for Phiremock. Allows to stub remote services for HTTP requests.",
3434
"license" : "GPL-3.0-or-later",
3535
"require" : {
36-
"php" : "^7.2|^8.0",
37-
"codeception/codeception" : ">=2.2 <6.0",
38-
"symfony/process": ">=3.0.0 <7.0.0"
36+
"php" : "^8.0",
37+
"codeception/codeception" : "^5.0",
38+
"symfony/process": ">=5.0.0 <7.0.0"
3939
},
4040
"require-dev": {
4141
"mcustiel/phiremock-server": "^1.0",
42-
"codeception/module-rest": "^1.0",
43-
"codeception/module-phpbrowser": "^1.0",
42+
"codeception/module-rest": "^3.0",
43+
"codeception/module-phpbrowser": "^3.0",
4444
"guzzlehttp/guzzle" : "^7.0"
4545
},
4646
"suggest": {

src/Extension/Phiremock.php

+93-11
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
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\PhiremockPHP72;
29-
use Mcustiel\Phiremock\Codeception\Extension\PhiremockPHP74p;
3028

3129
class 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
}

src/PhiremockExtension/Config.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public function getWaitUntilReadyIntervalMicros(): int
207207
/** @throws ConfigurationException */
208208
public static function getDefaultLogsPath(): string
209209
{
210-
return Configuration::logDir();
210+
return Configuration::outputDir();
211211
}
212212

213213
private function initInterfaceAndPort(array $config): void

src/PhiremockExtension/PhiremockPHP72.php

-146
This file was deleted.

0 commit comments

Comments
 (0)