PHP client for mountebank
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Latest Version on Packagist Software License Build Status Code Coverage Scrutinizer Code Quality

Juggler is a PHP client for mountebank - open source tool that provides test doubles over the wire. Juggler allows to:

  • interact with mountebank API;
  • verify mocks;
  • alter and build imposters;

Only HTTP imposters are supported at the moment.

There is also Codeception module available.


Via Composer

$ composer require meare/juggler:~1.0


API interactions

Juggler makes interactions with mountebank API easy:

use Meare\Juggler\Juggler;

$juggler = new Juggler('localhost');

// Delete active imposters before posting to avoid resource conflicts
$port = $juggler->postImposterFromFile(__DIR__ . '/contract.json');

// Retrieve imposter contract and save it to file
$juggler->retrieveAndSaveContract($port, __DIR__ . '/retrieved_contract.json');


Mock verification

mountebank remembers every request imposters get if --mock command line flag is set.

Here is how to verify mock with Juggler:

use Meare\Juggler\Juggler;

$juggler = new Juggler('localhost');

// Post imposter
$port = $juggler->postImposterFromFile(__DIR__ . '/contract.json');

// Do some requests

// Retrieve imposter and verify it received requests
$imposter = $juggler->getHttpImposter($port);
    'method' => 'GET',
    'path'   => '/foo',
    'query'  => ['bar' => 1],
]); // Will return true

Read more on mock verification

Imposter altering

Imagine you have stub for GET /account/3 which returns account balance:

  "port": 4545,
  "protocol": "http",
  "stubs": [
      "responses": [
          "is": {
            "statusCode": 200,
            "body": {
              "clientId": 3,
              "name": "Dmitar Ekundayo",
              "balance": 24.5
            "_mode": "text"
      "predicates": [
          "equals": {
            "method": "GET",
            "path": "/client/3"

At some point you might not want to create separate stub to imitate negative balance. Altering imposter will do the trick:

use Meare\Juggler\Juggler;

$juggler = new Juggler('localhost');
$port = $juggler->postImposterFromFile(__DIR__ . '/contract.json');

// Find stub by predicates and alter response
$imposter = $juggler->getHttpImposter($port);
$imposter->findStubByPredicates([['equals' => ['method' => 'GET', 'path' => '/account/3']]])
    ->modifyBody(function (array $body) {
        $body['balance'] = -5.75;

        return $body;

// Delete imposter and post again

Building imposter

use Meare\Juggler\Imposter\HttpImposter;
use Meare\Juggler\Imposter\Stub\Predicate\Predicate;
use Meare\Juggler\Imposter\Stub\Response\IsResponse;
use Meare\Juggler\Juggler;

$juggler = new Juggler('localhost');

// Create imposter with a stub for GET /test-endpoint
$imposter = new HttpImposter;
    [new IsResponse(200, ['Content-type' => 'application/json'], '{"status":200}')],
    [new Predicate(Predicate::OPERATOR_EQUALS, ['method' => 'GET', 'path' => '/test-endpoint'])]

// Post it!


$ composer test



The MIT License (MIT). Please see License File for more information.