You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
> 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.
27
20
28
-
### Extension
29
-
The extension provides an easy way to start a Phiremock server with configured host, port, debug mode and logs path.
In codeception.yml you will need to enable Phiremock extension and configure it in a proper way:
28
+
## Configuration
33
29
34
30
```yaml
35
31
extensions:
36
32
enabled:
37
33
- \Codeception\Extension\Phiremock
38
34
config:
39
35
\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
41
37
bin_path: ../vendor/bin # defaults to codeception_dir/../vendor/bin
42
38
logs_path: /var/log/my_app/tests/logs # defaults to codeception's tests output dir
43
39
debug: true # defaults to false
44
-
startDelay: 1# default to 0
40
+
start_delay: 1 # default to 0
45
41
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'
47
43
```
48
44
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.
49
45
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:
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
71
59
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
80
63
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
83
67
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
86
71
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
110
75
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:
113
78
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.
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;
142
95
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
148
99
{
149
-
...
100
+
return new GuzzleHttp\Client();
150
101
}
102
+
}
151
103
```
152
104
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:
154
106
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
163
113
```
164
114
165
-
## Use case
166
-
167
-
### Yii2-Curl
115
+
## See also:
168
116
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).
0 commit comments