Skip to content

Commit 16187fc

Browse files
committed
added some documentation. Renamed module methods
1 parent b7da58f commit 16187fc

File tree

5 files changed

+160
-9
lines changed

5 files changed

+160
-9
lines changed

README.md

Lines changed: 153 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,159 @@
11
# codeception-wiremock-extension
2-
An extension that allows to use WireMock when running codeception tests.
32

4-
## See
3+
This Codeception Extension allows developers and testers to use WireMock to mock external services when running codeception tests.
4+
5+
codeception-wiremock-extension connects to an already running instance of WireMock or can also run automatically a local standalone one. And, it is able to download the version of wiremock you preffer and run it too.
6+
After the tests are finished it will close the connection and turn wiremock service off (when it started it).
7+
8+
## See also
59

610
* WireMock PHP library: https://github.com/rowanhill/wiremock-php
711
* Stubbing using WireMock: http://wiremock.org/stubbing.html
812
* Verifying using WireMock: http://wiremock.org/verifying.html
13+
14+
## Installation
15+
16+
### Composer:
17+
18+
This project is published in packagist, so you just need to add it as a dependency in your composer.json:
19+
20+
```javascript
21+
"require": {
22+
// ...
23+
"mcustiel/php-simple-request": "*"
24+
}
25+
```
26+
27+
If you want to access directly to this repo, adding this to your composer.json should be enough:
28+
29+
```javascript
30+
{
31+
"repositories": [
32+
{
33+
"type": "vcs",
34+
"url": "https://github.com/mcustiel/php-simple-request"
35+
}
36+
],
37+
"require": {
38+
"mcustiel/php-simple-request": "dev-master"
39+
}
40+
}
41+
```
42+
43+
Or just download the release and include it in your path.
44+
45+
## Configuration Examples
46+
47+
### Module
48+
49+
```yaml
50+
# acceptance.suite.yml
51+
modules:
52+
enabled:
53+
- WireMock
54+
```
55+
56+
### Extension
57+
58+
#### Default configuration
59+
60+
This configuration will download WireMock version 1.57 and run it on port 8080, writing logs to `/tmp/codeceptionWireMock/logs`
61+
62+
```yaml
63+
# codeception.yml
64+
extensions:
65+
enabled:
66+
- Codeception\Extension\WireMock
67+
```
68+
69+
70+
#### Connect to a running WireMock instance
71+
72+
```yaml
73+
# codeception.yml
74+
extensions:
75+
enabled:
76+
- Codeception\Extension\WireMock
77+
config:
78+
Codeception\Extension\WireMock:
79+
host: my.wiremock.server
80+
port: 8080
81+
```
82+
83+
#### Start a local WireMock instance and run it with given command line arguments
84+
85+
```yaml
86+
# codeception.yml
87+
extensions:
88+
enabled:
89+
- Codeception\Extension\WireMock
90+
config:
91+
Codeception\Extension\WireMock:
92+
jar-path: /opt/wiremock/bin/wiremock-standalone.jar
93+
port: 18080
94+
https-port: 18443
95+
verbose: true
96+
root-dir: /opt/wiremock/root
97+
98+
```
99+
100+
#### Download a WireMock instance and run it with given command line arguments
101+
102+
```yaml
103+
# codeception.yml
104+
extensions:
105+
enabled:
106+
- Codeception\Extension\WireMock
107+
config:
108+
Codeception\Extension\WireMock:
109+
download-version: 1.57
110+
port: 18080
111+
https-port: 18443
112+
verbose: true
113+
root-dir: /opt/wiremock/root
114+
logs-path: /var/log/wiremock
115+
```
116+
117+
## How to use
118+
119+
### Prepare your application
120+
121+
First of all, configure your application so when it is being tested it will replace its external services with WireMock.
122+
For instance, if you make some requests to a REST service located under http://your.rest.interface, replace that url in configuration with the url where WireMock runs, for instance: http://localhost:8080/rest_interface.
123+
124+
### Write your tests
125+
126+
```php
127+
// YourCest.php
128+
class YourCest extends \Codeception\TestCase\Test
129+
{
130+
public function _after(\AcceptanceTester $I)
131+
{
132+
$I->cleanAllPreviousRequestsToWireMock();
133+
}
134+
135+
// tests
136+
public function tryToTest(\AcceptanceTester $I)
137+
{
138+
$I->expectRequestToWireMock(
139+
WireMock::get(WireMock::urlEqualTo('/some/url'))
140+
->willReturn(WireMock::aResponse()
141+
->withHeader('Content-Type', 'text/plain')
142+
->withBody('Hello world!'))
143+
);
144+
// Here you should execute your application in a way it requests wiremock. I do this directly to show it.
145+
$response = file_get_contents('http://localhost:18080/some/url');
146+
147+
$I->assertEquals('Hello world!', $response);
148+
$I->receivedRequestInWireMock(
149+
WireMock::getRequestedFor(WireMock::urlEqualTo('/some/url'))
150+
);
151+
}
152+
153+
// Also, you can access wiremock-php library directly
154+
public function moreComplexTest()
155+
{
156+
157+
}
158+
}
159+
```

src/Extension/WireMock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __construct(
4949

5050
$this->config = $this->argumentsManager->sanitize($this->config);
5151

52-
if (! empty($this->config['host'])) {
52+
if (!empty($this->config['host'])) {
5353
echo "Connecting to wiremock host {$this->config['host']}" . PHP_EOL;
5454
$host = $this->config['host'];
5555
} else {

src/Extension/WireMockProcess.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private function checkProcessIsRunning()
9595
public function stop()
9696
{
9797
if (is_resource($this->process)) {
98-
foreach ($this->pipes AS $pipe) {
98+
foreach ($this->pipes as $pipe) {
9999
if (is_resource($pipe)) {
100100
fflush($pipe);
101101
fclose($pipe);

src/Module/WireMock.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ public function _before(TestCase $testCase)
2424
$this->wireMock = WireMockConnection::get();
2525
}
2626

27-
public function cleanAllPreviousRequests()
27+
public function cleanAllPreviousRequestsToWireMock()
2828
{
2929
$this->wireMock->reset();
3030
}
3131

3232
/**
3333
* @param \WireMock\Client\MappingBuilder $builder
3434
*/
35-
public function expectRequest(MappingBuilder $builder)
35+
public function expectRequestToWireMock(MappingBuilder $builder)
3636
{
3737
$this->wireMock->stubFor($builder);
3838
}
@@ -41,7 +41,7 @@ public function expectRequest(MappingBuilder $builder)
4141
* @param \WireMock\Client\RequestPatternBuilder|integer $builderOrCount
4242
* @param \WireMock\Client\RequestPatternBuilder $builder
4343
*/
44-
public function receivedRequest($builderOrCount, RequestPatternBuilder $builder = null)
44+
public function receivedRequestToWireMock($builderOrCount, RequestPatternBuilder $builder = null)
4545
{
4646
$this->wireMock->verify($builderOrCount, $builder);
4747
}

tests/tests/acceptance/WelcomeCest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public function tryToTest(\AcceptanceTester $I)
3131
->withHeader('Content-Type', 'text/plain')
3232
->withBody('Hello world!'))
3333
);
34+
3435
$response = file_get_contents('http://localhost:18080/some/url');
35-
//Debug::debug(var_export($this->wiremock, true));
36-
Debug::debug(var_export($response, true));
36+
3737
$I->receivedRequest(
3838
WireMock::getRequestedFor(WireMock::urlEqualTo('/some/url'))
3939
);

0 commit comments

Comments
 (0)