This codeception 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.
"require-dev": {
"mcustiel/phiremock-codeception-module": "^1.0",
"guzzlehttp/guzzle": "^6.0"
}
You need to enable Phiremock module in your suite's configuration file:
modules:
enabled:
- Phiremock:
host: phiremock-myhost.dev # Defaults to localhost
port: 18080 # Defaults to 8086
reset_before_each_test: false # if set to true, executes `$I->haveACleanSetupInRemoteService` before each test. Defaults to false
expectations_path: /path/to/my/expectation/json/files # Defaults to codeception_dir/_data/phiremock-expectations
client_factory: \My\ClientFactory # Defaults to 'default'
secure: true # Default: false
extra_connections: [] # Default: empty array
Specifies the host where phiremock-server is listening for requests.
Default: localhost
Specifies the port where phiremock-server is listening for requests.
Default: 8086
Determines whether or not phiremock-server must be reset before each test.
Default: false
Specifies the path where expectation json files are located. The files can then be referenced using annotations and will be loaded automatically.
Default: codeception_dir/_data/phiremock-expectations
A boolean specifying if the connection is secure or not. If it's secure, the request is done through https, otherwise it's done through http.
Default: false. The requests to phiremock-client are done through http.
An list of objects specifying the parameters to request other phiremock-servers. This is useful if you want to have 2 phiremock instances, one listening for http connections, and the other listening for https connections.
Default: An empty list, meaning that no other phiremock servers are requested.
modules:
enabled:
- Phiremock:
host: phiremock-myhost.dev
port: 18080
secure: false
extra_connections:
secure-host:
host: phiremock-myhost.dev
port: 18081
secure: true
Then in the code you can use each connection by name as follows:
<?php
$I->takeConnection('secure-host')->reset();
The default connection is called 'default' and you can also take it:
$I->takeConnection('default')->reset();
Specifies the fully qualified class name of a class which overrides default phiremock-client factory. This is useful if you want to avoid using Guzzle HttpClient v6 (the one supported by default in phiremock-client).
Default: default
"require-dev": {
"mcustiel/phiremock-codeception-module": "v1.0",
"guzzlehttp/guzzle": "^7.0"
The you can create a factory as follows and provide the fully qualified class name in the module configuration:
<?php
namespace My\Namespace;
use Mcustiel\Phiremock\Client\Factory;
use GuzzleHttp;
use Psr\Http\Client\ClientInterface;
class FactoryWithGuzzle7 extends Factory
{
public function createRemoteConnection(): ClientInterface
{
return new GuzzleHttp\Client(['allow_redirects' => false]);
}
}
Any http client implementing psr18 can be provided.
The module provides the following handy methods to communicate with Phiremock server:
Allows you to setup an expectation in Phiremock, specifying the expected request and the response the server should give for it:
$I->expectARequestToRemoteServiceWithAResponse(
on(getRequest()->andUrl(isEqualTo('/some/url')))
->then(respond(203)->andBody('I am a response'))
);
Cleans the server of all configured expectations, scenarios and requests history, and reloads expectation files if they are present.
$I->haveACleanSetupInRemoteService();
Cleans all previously configured expectations and requests history.
$I->dontExpectRequestsInRemoteService();
Cleans the state of all scenarios (sets all of them to inital state).
$I->haveCleanScenariosInRemoteService();
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.
$I->seeRemoteServiceReceived(1, getRequest()->andUrl(isEqualTo('/some/url')));
Resets the requests counter for the verifier in Phiremock.
$I->didNotReceiveRequestsInRemoteService();
Retrieves all the requests received by Phiremock server matching the one specified.
$I->grabRequestsMadeToRemoteService(getRequest()->andUrl(isEqualTo('/some/url')));
Forces the state of a scenario.
$I->setScenarioState('scenarioName', 'newScenarioState');
Allows to use several connections to different phiremock servers.
$I->takeConnection('connectionName');
Allows you to set up an expectation via a json file
/**
* @expectation("get_client_timeout")
*/
public function test(FunctionalTester $I)
{
...
}
That will load by default the file at tests/_data/phiremock-expectations/get_client_timeout.json
. The path where to place the expectations is configurable.
You may use expectations placed in subdirectories
/**
* @expectation("edge_cases/get_client_timeout")
*/
public function test(FunctionalTester $I)
{
...
}
Multiple annotation formats are accepted
* @expectation get_client_timeout
* @expectation get_client_timeout.json
* @expectation(get_client_timeout.json)
* @expectation(get_client_timeout)
* @expectation("get_client_timeout")
- Phiremock Client: https://github.com/mcustiel/phiremock-client
- Phiremock Codeception Extension: https://github.com/mcustiel/phiremock-codeception-extension
- Examples in tests: https://github.com/mcustiel/phiremock-codeception-module/tree/master/tests/acceptance