|
1 | 1 | # codeception-wiremock-extension |
2 | | -An extension that allows to use WireMock when running codeception tests. |
3 | 2 |
|
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 |
5 | 9 |
|
6 | 10 | * WireMock PHP library: https://github.com/rowanhill/wiremock-php |
7 | 11 | * Stubbing using WireMock: http://wiremock.org/stubbing.html |
8 | 12 | * 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 | +``` |
0 commit comments