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

Commit 2cf999b

Browse files
committed
Improved WireMock integration through separation of module and extension configurations. Now the module can be used to connect to an existent WireMock without using the extension
1 parent 3412072 commit 2cf999b

File tree

7 files changed

+50
-44
lines changed

7 files changed

+50
-44
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ After the tests are finished it will close the connection and turn wiremock serv
1010
* [Stubbing using WireMock](http://wiremock.org/stubbing.html)
1111
* [Verifying using WireMock](http://wiremock.org/verifying.html)
1212

13+
## Note
14+
15+
If you need an application with a functionality that is similar to the one offered by WireMock and is 100% PHP, please give Phiremock a try: [Phiremock](https://github.com/mcustiel/phiremock), it also has a nice [codeception extension](https://github.com/mcustiel/phiremock-codeception-extension).
16+
1317
## Installation
1418

1519
### Composer:
@@ -44,19 +48,22 @@ Or just download the release and include it in your path.
4448
## Configuration Examples
4549

4650
### Module
51+
The module allow you to connect to a WireMock instance, it can be the one ran by the extension or an already running one.
4752

4853
```yaml
4954
# acceptance.suite.yml
5055
modules:
5156
enabled:
52-
- WireMock
57+
- WireMock:
58+
host: my.wiremock.host # defaults to 127.0.0.1
59+
port: 80 # defaults to 8080
5360
```
5461
5562
### Extension
5663
5764
#### Default configuration
5865
59-
This configuration will download WireMock version 1.57 and run it on port 8080, writing logs to `/tmp/codeceptionWireMock/logs`
66+
This configuration will download WireMock version 1.57 and run it on port 8080, writing logs to codeception tests _output dir.
6067
6168
```yaml
6269
# codeception.yml

composer.json

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,16 @@
2525
"license" : "GPL-3.0+",
2626
"require" : {
2727
"php" : ">=5.4",
28-
"codeception/codeception" : "~2.1.0",
29-
"wiremock-php/wiremock-php" : ">=1.43",
30-
"mcustiel/php-simple-di" : ">=1.2.0"
28+
"codeception/codeception" : "^2.1.0",
29+
"wiremock-php/wiremock-php" : "^1.43"
3130
},
3231
"require-dev" : {
33-
"phing/phing" : ">=2.12.0",
34-
"squizlabs/php_codesniffer" : ">=2.3.4",
35-
"phpmd/phpmd" : ">=2.3.2",
36-
"sebastian/phpcpd" : ">=2.0.2",
37-
"pdepend/pdepend" : ">=2.0.6",
38-
"phploc/phploc" : ">=2.1.1",
39-
"phpdocumentor/phpdocumentor" : ">=2.8.5"
32+
"phing/phing" : "^2.12.0",
33+
"squizlabs/php_codesniffer" : "^2.3.4",
34+
"phpmd/phpmd" : "^2.3.2",
35+
"sebastian/phpcpd" : "^2.0.2",
36+
"pdepend/pdepend" : "^2.0.6",
37+
"phploc/phploc" : "^2.1.1",
38+
"phpdocumentor/phpdocumentor" : "^2.8.5"
4039
}
4140
}

src/Extension/WireMock.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
use Codeception\Platform\Extension as CodeceptionExtension;
2121
use WireMock\Client\WireMock as WireMockClient;
22-
use Mcustiel\DependencyInjection\DependencyContainer;
2322

2423
/**
2524
* Codeception Extension for WireMock
@@ -66,25 +65,13 @@ public function __construct(
6665

6766
$this->config = $this->argumentsManager->sanitize($this->config);
6867

69-
if (!empty($this->config['host'])) {
70-
echo "Connecting to wiremock host {$this->config['host']}" . PHP_EOL;
71-
$host = $this->config['host'];
72-
} else {
73-
echo "Starting local wiremock" . PHP_EOL;
74-
$this->process->start(
75-
$this->getJarPath(),
76-
$this->config['logs-path'],
77-
$this->mapConfigToWireMockArguments($this->config)
78-
);
79-
$host = 'localhost';
80-
sleep($this->config['start-delay']);
81-
}
82-
DependencyContainer::getInstance()->add(
83-
'wiremockConnection',
84-
function() use ($host) {
85-
return WireMockClient::create($host, $this->config['port']);
86-
}
68+
echo "Starting local wiremock" . PHP_EOL;
69+
$this->process->start(
70+
$this->getJarPath(),
71+
$this->config['logs-path'],
72+
$this->mapConfigToWireMockArguments($this->config)
8773
);
74+
sleep($this->config['start-delay']);
8875
}
8976

9077
private function initWireMockProcess($process)
@@ -119,7 +106,7 @@ private function initWireMockArgumentsManager($argumentsManager)
119106
*/
120107
public function __destruct()
121108
{
122-
$connection = DependencyContainer::getInstance()->get('wiremockConnection');
109+
$connection = WireMockClient::create('localhost', $this->config['port']);
123110
if ($connection->isAlive()) {
124111
$connection->shutdownServer();
125112
}

src/Extension/WireMockArguments.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
namespace Codeception\Extension;
1919

20+
use Codeception\Configuration as Config;
21+
2022
/**
2123
* Utility class to work with config parameters and wiremock command line arguments.
2224
*/
@@ -66,9 +68,7 @@ class WireMockArguments
6668
*/
6769
public function __construct()
6870
{
69-
$this->defaults['logs-path'] = sys_get_temp_dir()
70-
. DIRECTORY_SEPARATOR . 'codeceptionWireMock'
71-
. DIRECTORY_SEPARATOR . 'logs';
71+
$this->defaults['logs-path'] = Config::logDir();
7272
}
7373

7474
/**

src/Extension/WireMockProcess.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public function start($jarPath, $logsPath, $arguments)
5151
{
5252
$this->checkIfProcessIsRunning();
5353

54+
echo "Running " . $this->getCommandPrefix() . "java -jar {$jarPath}{$arguments}";
55+
5456
$this->process = proc_open(
5557
$this->getCommandPrefix() . "java -jar {$jarPath}{$arguments}",
5658
$this->createProcessDescriptors($logsPath),
@@ -71,6 +73,7 @@ private function createProcessDescriptors($logsPath)
7173
{
7274
$logFile = $logsPath . DIRECTORY_SEPARATOR . self::LOG_FILE_NAME;
7375
$descriptors = [
76+
['pipe', 'r'],
7477
['file', $logFile, 'w'],
7578
['file', $logFile, 'a'],
7679
];
@@ -128,8 +131,8 @@ public function stop()
128131
private function getCommandPrefix()
129132
{
130133
if (PHP_OS == 'WIN32' || PHP_OS == 'WINNT' || PHP_OS == 'Windows') {
131-
return 'exec ';
134+
return '';
132135
}
133-
return '';
136+
return 'exec ';
134137
}
135138
}

src/Module/WireMock.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919

2020
use Codeception\Module as CodeceptionModule;
2121
use Codeception\TestCase;
22-
use Codeception\Extension\WireMockConnection;
22+
use Codeception\Util\Debug;
23+
use WireMock\Client\WireMock as WireMockClient;
2324
use WireMock\Client\MappingBuilder;
2425
use WireMock\Client\RequestPatternBuilder;
25-
use Mcustiel\DependencyInjection\DependencyContainer;
2626

2727
class WireMock extends CodeceptionModule
2828
{
@@ -31,14 +31,22 @@ class WireMock extends CodeceptionModule
3131
*/
3232
private $wireMock;
3333

34+
protected $config = [
35+
'host' => 'localhost',
36+
'port' => '8080'
37+
];
38+
3439
/**
3540
* {@inheritDoc}
36-
* @see \Codeception\Module::_before()
41+
* @see \Codeception\Module::_beforeSuite()
3742
*/
38-
public function _before(TestCase $testCase)
43+
public function _beforeSuite($settings = [])
3944
{
40-
parent::_before($testCase);
41-
$this->wireMock = DependencyContainer::getInstance()->get('wiremockConnection');
45+
$this->config = array_merge($this->config, $settings);
46+
Debug::debug(
47+
"Connecting to WireMock in: host {$this->config['host']} and port {$this->config['port']}"
48+
);
49+
$this->wireMock = WireMockClient::create($this->config['host'], $this->config['port']);
4250
}
4351

4452
public function cleanAllPreviousRequestsToWireMock()

tests/tests/acceptance.suite.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ modules:
1010
- PhpBrowser:
1111
url: http://localhost/myapp
1212
- \Helper\Acceptance
13-
- WireMock
13+
- WireMock:
14+
host: localhost
15+
port: 18080
1416
- Asserts

0 commit comments

Comments
 (0)