Client library to communicate with Phiremock server

Installs: 134 290

Dependents: 4

Suggesters: 0

Security: 0

Stars: 10

Watchers: 4

Forks: 5

Open Issues: 1


v1.2.1 2022-05-14 13:47 UTC


Phiremock client provides a nice API to interact with Phiremock Server, allowing developers to setup expectations, clear state, scenarios etc. Through a fluent interface.

Packagist Version Build Status Scrutinizer Code Quality Packagist Downloads


Default installation through composer

This project is published in packagist, so you just need to add it as a dependency in your composer.json:

    "require-dev": {
        "mcustiel/phiremock-client": "^1.0",
        "guzzlehttp/guzzle": "^6.0"

Phiremock Client requires guzzle client v6 to work. This dependency can be avoided and you can choose any psr18-compatible http client and overwrite Phiremock Client's factory to provide it.

Overwriting the factory class

If guzzle client v6 is provided as a dependency no extra configuration is needed. If you want to use a different http client you need to provide it to phiremock server as a psr18-compatible client. For instance, if you want to use guzzle client v7 you need to extend phiremock server's factory class:

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();

Then use this factory class to create the Phiremock Client Facade.


Creating the Client Facade

Create the Client Facade by requesting it from the factory object:

use Mcustiel\Phiremock\Client\Connection\Host;
use Mcustiel\Phiremock\Client\Connection\Port;

$phiremockClient = Factory::createDefault()->createPhiremockClient(new Host(''), new Port('8080'));

Now you can use $phiremockClient to access all the configuration options of Phiremock Server.

Note: Phiremock will by default listen for http (unsecured) connections.

Connection to a secure server

If phiremock-server is listening for https connections. You can pass the scheme to use as a third argument:

use Mcustiel\Phiremock\Client\Connection\Host;
use Mcustiel\Phiremock\Client\Connection\Port;
use Mcustiel\Phiremock\Client\Connection\Scheme;

$phiremockClient = Factory::createDefault()->createPhiremockClient(new Host(''), new Port('8443'), Scheme::createHttps());

Expectation creation

use Mcustiel\Phiremock\Client\Phiremock;
use Mcustiel\Phiremock\Client\Utils\A;
use Mcustiel\Phiremock\Client\Utils\Is;
use Mcustiel\Phiremock\Client\Utils\Respond;
use Mcustiel\Phiremock\Domain\Options\Priority;
// ...
            ->andHeader('Accept', Is::equalTo('application/banana'))
            ->andFormField('name', Is::equalTo('potato'))
            ->andBody('Is the answer to the Ultimate Question of Life, The Universe, and Everything')
            ->andHeader('Content-Type', 'application/banana')
    )->setPriority(new Priority(5))

Also a cleaner/shorter way to create expectations is provided by using helper functions:

use Mcustiel\Phiremock\Client\Phiremock;
use function Mcustiel\Phiremock\Client\contains;
use function Mcustiel\Phiremock\Client\getRequest;
use function Mcustiel\Phiremock\Client\isEqualTo;
use function Mcustiel\Phiremock\Client\request;
use function Mcustiel\Phiremock\Client\respond;
use function Mcustiel\Phiremock\Client\on;
use Mcustiel\Phiremock\Domain\Options\Priority;
// ...
            ->andHeader('Accept', isEqualTo('application/banana'))
            ->andFormField('name', isEqualTo('potato'))
            ->andBody('Is the answer to the Ultimate Question of Life, The Universe, and Everything')
            ->andHeader('Content-Type', 'application/banana')
    )->setPriority(new Priority(5))

This code is equivalent to the one in the previous example.

You can see the list of shortcuts here:

Listing created expectations

The listExpecatations method returns an array of instances of the Expectation class containing all the current expectations checked by Phiremock Server.

$expectations = $phiremockClient->listExpectations();

Clear all configured expectations

This will remove all expectations checked, causing Phiremock Server to return 404 for every non-phiremock-api request.


List requests received by Phiremock

Use this method to get a list of Psr-compatible Requests received by Phiremock Server.

Lists all requests:


Lists requests matching condition:


Note: Phiremock's API request are excluded from this list.

Count requests received by Phiremock

Provides an integer >= 0 representing the amount of requests received by Phiremock Server.

Count all requests:


Count requests matching condition:


Note: Phiremock's API request are excluded from this list.

Clear stored requests

This will clean the list of requests saved on Phiremock Server and resets the counter to 0.


Set Scenario State

Force a scenario to have certain state on Phiremock Server.

$phiremockClient->setScenarioState('myScenario', 'loginExecuted');

Reset Scenarios States

Reset all scenarios to the initial state (Scenario.START).


Reset all

Sets Phiremock Server to its initial state. This will cause Phiremock Server to:

  1. clear all expectations.
  2. clear the stored requests.
  3. reset all the scenarios.
  4. reload all expectations stored in files.


See also:


Just submit a pull request. Don't forget to run tests and php-cs-fixer first and write documentation.

Thanks to:

And everyone who submitted their Pull Requests.