Skip to content
This repository was archived by the owner on Jul 4, 2023. It is now read-only.

Commit 89a1dab

Browse files
committed
Modified server to use process and to support windows. Renamed classes to have uppercase M in Wiremock word
1 parent 7cdc02e commit 89a1dab

File tree

7 files changed

+89
-64
lines changed

7 files changed

+89
-64
lines changed

src/Wiremock.php renamed to src/WireMock.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
<?php
22
namespace Codeception\Extension;
33

4-
class Wiremock extends \Codeception\Platform\Extension
4+
class WireMock extends \Codeception\Platform\Extension
55
{
6-
const DEFAULT_LOGS_PATH = '/tmp/codeceptionWiremock/logs/';
6+
const DEFAULT_LOGS_PATH = '/tmp/codeceptionWireMock/logs/';
77

88
/**
99
*
10-
* @var WiremockDownloader
10+
* @var WireMockDownloader
1111
*/
1212
private $downloader;
1313
/**
1414
*
15-
* @var WiremockServer
15+
* @var WireMockServer
1616
*/
1717
private $server;
1818
/**
1919
*
20-
* @var WiremockArguments
20+
* @var WireMockArguments
2121
*/
2222
private $argumentsManager;
2323

2424
public function __construct(
2525
$config,
2626
$options,
27-
WiremockDownloader $downloader = null,
28-
WiremockServer $server = null,
29-
WiremockArguments $argumentsManager = null
27+
WireMockDownloader $downloader = null,
28+
WireMockServer $server = null,
29+
WireMockArguments $argumentsManager = null
3030
) {
3131
parent::__construct($config, $options);
3232

33-
$this->initWiremockDownloader($downloader);
34-
$this->initWiremockServer($server);
35-
$this->initWiremockArgumentsManager($argumentsManager);
33+
$this->initWireMockDownloader($downloader);
34+
$this->initWireMockServer($server);
35+
$this->initWireMockArgumentsManager($argumentsManager);
3636

3737
$this->config = $this->argumentsManager->sanitize($this->config);
3838

@@ -42,44 +42,44 @@ public function __construct(
4242
$this->server->start(
4343
$this->getJarPath(),
4444
$this->getLogsPath(),
45-
$this->mapConfigToWiremockArguments($this->config)
45+
$this->mapConfigToWireMockArguments($this->config)
4646
);
4747
$host = 'localhost';
4848
sleep($this->config['start-delay']);
4949
}
50-
WiremockConnection::setConnection(\WireMock\Client\WireMock::create($host, $this->config['port']));
50+
WireMockConnection::setConnection(\WireMock\Client\WireMock::create($host, $this->config['port']));
5151
}
5252

53-
private function initWiremockServer($server)
53+
private function initWireMockServer($server)
5454
{
5555
if ($server === null) {
56-
$this->server = new WiremockServer();
56+
$this->server = new WireMockServer();
5757
} else {
5858
$this->server = $server;
5959
}
6060
}
6161

62-
private function initWiremockDownloader($downloader)
62+
private function initWireMockDownloader($downloader)
6363
{
6464
if ($downloader === null) {
65-
$this->downloader = new WiremockDownloader();
65+
$this->downloader = new WireMockDownloader();
6666
} else {
6767
$this->downloader = $downloader;
6868
}
6969
}
7070

71-
private function initWiremockArgumentsManager($argumentsManager)
71+
private function initWireMockArgumentsManager($argumentsManager)
7272
{
7373
if ($argumentsManager === null) {
74-
$this->argumentsManager = new WiremockArguments();
74+
$this->argumentsManager = new WireMockArguments();
7575
} else {
7676
$this->argumentsManager = $argumentsManager;
7777
}
7878
}
7979

8080
public function __destruct()
8181
{
82-
$this->server->stop($this->getJarPath(), $this->mapConfigToWiremockArguments($this->config));
82+
$this->server->stop();
8383
}
8484

8585
private function getJarPath()
@@ -123,7 +123,7 @@ private function checkJarExists($jar)
123123
}
124124
}
125125

126-
private function mapConfigToWiremockArguments($config)
126+
private function mapConfigToWireMockArguments($config)
127127
{
128128
return $this->argumentsManager->generateArgumentsString($config);
129129
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace Codeception\Extension;
33

4-
class WiremockArguments
4+
class WireMockArguments
55
{
66
private $map = [
77
'root-dir' => true,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace Codeception\Extension;
33

4-
class WiremockConnection
4+
class WireMockConnection
55
{
66
/**
77
* @var \WireMock\Client\WireMock
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
namespace Codeception\Extension;
33

4-
class WiremockDownloader
4+
class WireMockDownloader
55
{
6-
const DESTINATION_PATH = '/tmp/codeceptionWiremock/jars/';
6+
const DESTINATION_PATH = '/tmp/codeceptionWireMock/jars/';
77

88
public function downloadAndGetLocalJarPath($version)
99
{

src/WireMockServer.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
namespace Codeception\Extension;
3+
4+
class WireMockServer
5+
{
6+
const LOG_FILE_NAME = 'wiremock.out';
7+
8+
/**
9+
* @var resource
10+
*/
11+
private $process;
12+
/**
13+
* @var resource[]
14+
*/
15+
private $pipes;
16+
17+
public function start($jarPath, $logsPath, $arguments)
18+
{
19+
if ($this->process !== null) {
20+
throw new \Exception('The server is already running');
21+
}
22+
$logFile = rtrim($logsPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . self::LOG_FILE_NAME;
23+
$descriptors = [
24+
['pipe', 'r'],
25+
['file', $logFile, 'w'],
26+
['file', $logFile, 'a'],
27+
];
28+
$this->process = proc_open(
29+
$this->getCommandPrefix() . "java -jar {$jarPath}{$arguments}",
30+
$descriptors,
31+
$this->pipes
32+
);
33+
$this->checkProcessIsRunning();
34+
}
35+
36+
private function checkProcessIsRunning()
37+
{
38+
if (!is_resource($this->process)) {
39+
throw new \Exception('Could not start local wiremock server');
40+
}
41+
}
42+
43+
public function stop()
44+
{
45+
if ($this->process !== null) {
46+
foreach ($this->pipes AS $pipe) {
47+
if (is_resource($pipe)) {
48+
fclose($pipe);
49+
}
50+
}
51+
proc_terminate($this->process, SIGINT);
52+
}
53+
}
54+
55+
private function getCommandPrefix()
56+
{
57+
if (PHP_OS == 'WIN32' || PHP_OS == 'WINNT' || PHP_OS == 'Windows') {
58+
return 'exec ';
59+
}
60+
return '';
61+
}
62+
}

src/WiremockServer.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

tests/codeception.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ settings:
1212
extensions:
1313
enabled:
1414
- Codeception\Extension\RunFailed
15-
- Codeception\Extension\Wiremock
15+
- Codeception\Extension\WireMock
1616
config:
17-
Codeception\Extension\Wiremock:
17+
Codeception\Extension\WireMock:
1818
download-version: 1.57
1919
root-dir: /tmp
2020
port: 18080

0 commit comments

Comments
 (0)