Skip to content

Commit a0f49bf

Browse files
committed
Updated README with documentation for version 2
1 parent 82d648c commit a0f49bf

File tree

1 file changed

+63
-115
lines changed

1 file changed

+63
-115
lines changed

README.md

Lines changed: 63 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# phiremock-codeception-extension
2-
Codeception extension and module to make working with [Phiremock](https://github.com/mcustiel/phiremock) even easier. It allows to start a Phiremock server specifically for the acceptance tests to run or to connect to an already running Phiremock server.
2+
Codeception extension to make working with [Phiremock Server](https://github.com/mcustiel/phiremock-server) even easier. It allows to start a Phiremock Server before each suite and stop it when the suite ends.
33

44
[![Latest Stable Version](https://poser.pugx.org/mcustiel/phiremock-codeception-extension/v/stable)](https://packagist.org/packages/mcustiel/phiremock-codeception-extension)
55
[![Build Status](https://scrutinizer-ci.com/g/mcustiel/phiremock-codeception-extension/badges/build.png?b=master)](https://scrutinizer-ci.com/g/mcustiel/phiremock-codeception-extension/build-status/master)
@@ -10,160 +10,108 @@ Codeception extension and module to make working with [Phiremock](https://github
1010

1111
### Composer:
1212

13-
This project is published in packagist, so you just need to add it as a dependency in your composer.json:
14-
1513
```json
1614
"require-dev": {
17-
"mcustiel/phiremock-codeception-extension": "*"
18-
},
19-
"minimum-stability": "dev"
15+
"mcustiel/phiremock-codeception-extension": "v2.0"
16+
}
2017
```
2118

22-
> *NOTE*
23-
> Phiremock uses a dev-master version of react/http to work. Because of this, until reactphp guys tag a new
24-
> version you will need to set your project's minimum stability to dev.
25-
26-
## How to use
19+
Optionally, you can install Phiremock Server in case you want to have it between your dependencies. If not, you need to specify the path to phiremock in the configuration.
2720

28-
### Extension
29-
The extension provides an easy way to start a Phiremock server with configured host, port, debug mode and logs path.
21+
```json
22+
"require-dev": {
23+
"mcustiel/phiremock-codeception-extension": "v2.0",
24+
"mcustiel/phiremock-server": "^1.0",
25+
"guzzlehttp/guzzle": "^6.0"
26+
```
3027

31-
#### Configuration
32-
In codeception.yml you will need to enable Phiremock extension and configure it in a proper way:
28+
## Configuration
3329

3430
```yaml
3531
extensions:
3632
enabled:
3733
- \Codeception\Extension\Phiremock
3834
config:
3935
\Codeception\Extension\Phiremock:
40-
listen: 127.0.0.1:18080 # defaults to 0.0.0.0:8086
36+
listen: 127.0.0.1:18080 # defaults to 0.0.0.0:8086
4137
bin_path: ../vendor/bin # defaults to codeception_dir/../vendor/bin
4238
logs_path: /var/log/my_app/tests/logs # defaults to codeception's tests output dir
4339
debug: true # defaults to false
44-
startDelay: 1 # default to 0
40+
start_delay: 1 # default to 0
4541
expectations_path: /my/expectations/path # defaults to tests/_expectations
46-
unique_expectations_path: /my/expectations/path # defaults to tests/_unique_expectations
42+
server_factory: \My\FactoryClass # defaults to 'default'
4743
```
4844
Note: Since Codeception version 2.2.7, extensions configuration can be added directly in the suite configuration file. That will avoid phiremock to be started for every suite.
4945

50-
Phiremock uses annotations internally. To be able to run the extension, the annotations autoloader must be activated. To do this, you must add the next lines in the bootstrap file where you include your composer autoloader:
51-
```php
52-
$loader = require APP_ROOT . '/vendor/autoload.php';
53-
\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader([$loader, 'loadClass']);
54-
```
55-
56-
## Parameters
46+
### Parameters
5747

58-
* **listen:** Specifies the interface and port where phiremock must listen for requests
59-
* **bin_path:** Path where Phiremock "binary" is located
60-
* **logs_path:** Path where to write the output
61-
* **debug:** Where to write debug data to log files
62-
* **startDelay:** Time to wait after Phiremock was started to start running the tests (used to give time to Phiremock to boot)
63-
* **expectations_path:** Specifies a directory to search for json files defining expectations to load by default.
64-
* **unique_expectations_path:** Specifies a directory to search for json files defining expectations to load via annotations.
48+
#### listen
49+
Specifies the interface and port where phiremock must listen for requests.
50+
**Default:** 0.0.0.0:8086
6551

66-
### Module
67-
The module allows you to connect to a Phiremock server and to interact with it in a semantic way through the codeception actor in your tests.
52+
#### bin_path
53+
Path where Phiremock Server's "binary" is located. You can, for instance, point to the location of the phar in your file system.
54+
**Default:** codeception_dir/../vendor/bin/phiremock
6855

69-
#### Configuration
70-
You need to enable Phiremock module in your suite's configuration file:
56+
#### logs_path
57+
Path where to write the output.
58+
**Default:** codeception's tests output dir
7159

72-
```yaml
73-
modules:
74-
enabled:
75-
- Phiremock:
76-
host: 127.0.0.1
77-
port: 18080
78-
resetBeforeEachTest: false # if set to true, executes `$I->haveACleanSetupInRemoteService` before each test.
79-
```
60+
#### debug
61+
Whether to write debug data to log file.
62+
**Default:** false
8063

81-
#### Use
82-
The module provides the following handy methods to communicate with Phiremock server:
64+
#### start_delay
65+
Time to wait after Phiremock Server is started before running the tests (used to give time to Phiremock Server to boot)
66+
**Default:** 0
8367

84-
#### expectARequestToRemoteServiceWithAResponse
85-
Allows you to setup an expectation in Phiremock, specifying the expected request and the response the server should give for it:
68+
#### expectations_path
69+
Specifies a directory to search for json files defining expectations to load by default.
70+
**Default:** codecption_dir/_expectations
8671

87-
```php
88-
$I->expectARequestToRemoteServiceWithAResponse(
89-
Phiremock::on(
90-
A::getRequest()->andUrl(Is::equalTo('/some/url'))
91-
)->then(
92-
Respond::withStatusCode(203)->andBody('I am a response')
93-
)
94-
);
95-
```
96-
97-
#### haveACleanSetupInRemoteService
98-
Cleans the server of all configured expectations, scenarios and requests history, and reloads expectation files.
99-
100-
```php
101-
$I->haveACleanSetupInRemoteService();
102-
```
103-
104-
#### dontExpectRequestsInRemoteService
105-
Cleans all previously configured expectations and requests history.
106-
107-
```php
108-
$I->dontExpectRequestsInRemoteService();
109-
```
72+
#### server_factory
73+
Specifies a Factory class extending `\Mcustiel\Phiremock\Server\Factory\Factory`. Useful if you want to provide your own PSR. This works only if you install phiremock as a local dependency required in your composer file.
74+
**Default:** default
11075

111-
#### haveCleanScenariosInRemoteService
112-
Cleans the state of all scenarios (sets all of them to inital state).
76+
**Example:**
77+
If this is in your composer.json:
11378

114-
```php
115-
$I->haveCleanScenariosInRemoteService();
116-
```
117-
118-
#### seeRemoteServiceReceived
119-
Allows you to verify that the server received a request a given amount of times. This request could or not be previously set up as an expectation.
120-
121-
```php
122-
$I->seeRemoteServiceReceived(1, A::getRequest()->andUrl(Is::equalTo('/some/url')));
123-
```
124-
125-
#### didNotReceiveRequestsInRemoteService
126-
Resets the requests counter for the verifier in Phiremock.
127-
128-
```php
129-
$I->didNotReceiveRequestsInRemoteService();
79+
```json
80+
"require-dev": {
81+
"mcustiel/phiremock-codeception-extension": "v2.0",
82+
"mcustiel/phiremock-server": "^1.0",
83+
"guzzlehttp/guzzle": "^7.0"
13084
```
13185

132-
#### grabRequestsMadeToRemoteService
133-
Retrieves all the requests received by Phiremock server matching the one specified.
86+
The you can create a factory as follows:
13487

13588
```php
136-
$I->grabRequestsMadeToRemoteService(A::getRequest()->andUrl(Is::equalTo('/some/url')));
137-
```
138-
139-
#### @expectation Annotations
89+
<?php
90+
namespace My\Namespace;
14091

141-
Allows you to to set up an expectation via a json file
92+
use GuzzleHttp;
93+
use Mcustiel\Phiremock\Server\Factory\Factory;
94+
use Psr\Http\Client\ClientInterface;
14295

143-
```php
144-
/**
145-
* @expectation("get_client_timeout")
146-
*/
147-
public function test(FunctionalTester $I)
96+
class FactoryWithGuzzle7 extends Factory
97+
{
98+
public function createHttpClient(): ClientInterface
14899
{
149-
...
100+
return new GuzzleHttp\Client();
150101
}
102+
}
151103
```
152104

153-
That will load by default the file at `tests/_unique_expectations/get_client_timeout.json`
105+
and in the extension config provide the fully qualified namespace to that class:
154106

155-
Multiple annotation formats are accepted
156-
157-
```
158-
* @expectation get_client_timeout
159-
* @expectation get_client_timeout.json
160-
* @expectation(get_client_timeout.json)
161-
* @expectation(get_client_timeout)
162-
* @expectation("get_client_timeout")
107+
```yaml
108+
enabled:
109+
- \Codeception\Extension\Phiremock
110+
config:
111+
\Codeception\Extension\Phiremock:
112+
server_factory: \My\Namespace\FactoryWithGuzzle7
163113
```
164114

165-
## Use case
166-
167-
### Yii2-Curl
115+
## See also:
168116

169-
[Yii2-Curl](https://github.com/linslin/Yii2-Curl) uses phiremock-codeception-extension for functional testing. You can see the configuration for the [extension](https://github.com/linslin/Yii2-Curl/blob/master/codeception.yml) and the [module](https://github.com/linslin/Yii2-Curl/blob/master/tests/functional.suite.yml), as well as how Phiremock is [configured in the tests](https://github.com/linslin/Yii2-Curl/blob/master/tests/functional/httpMockCest.php).
117+
Phiremock Server: https://github.com/mcustiel/phiremock-server

0 commit comments

Comments
 (0)