2017 © Pedro Peláez
 

library juggler

PHP client for Mountebank

image

meare/juggler

PHP client for Mountebank

  • Thursday, March 2, 2017
  • by meare
  • Repository
  • 1 Watchers
  • 4 Stars
  • 1,775 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 3 Versions
  • 8 % Grown

The README.md

Juggler

Latest Version on Packagist ![Software License][ico-license] Build Status [![Code Coverage][ico-coverage]][link-coverage] Scrutinizer Code Quality, (*1)

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

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

Only HTTP imposters are supported at the moment., (*3)

There is also Codeception module available., (*4)

Install

Via Composer, (*5)

``` bash $ composer require meare/juggler:~1.0, (*6)


## Usage ### API interactions Juggler makes interactions with [mountebank API](http://www.mbtest.org/docs/api/overview) easy: ``` php use Meare\Juggler\Juggler; $juggler = new Juggler('localhost'); // Delete active imposters before posting to avoid resource conflicts $juggler->deleteImposters(); $port = $juggler->postImposterFromFile(__DIR__ . '/contract.json'); // Retrieve imposter contract and save it to file $juggler->retrieveAndSaveContract($port, __DIR__ . '/retrieved_contract.json'); $juggler->deleteImposter($port);

Mock verification

mountebank remembers every request imposters get if --mock command line flag is set., (*7)

Here is how to verify mock with Juggler:, (*8)

use Meare\Juggler\Juggler;

$juggler = new Juggler('localhost');

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

// Do some requests
file_get_contents('http://mountebank:4545/foo?bar=1');
file_get_contents('http://mountebank:4545/foo?bar=2&zar=3');

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

Read more on mock verification, (*9)

Imposter altering

Imagine you have stub for GET /account/3 which returns account balance:, (*10)

{
  "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:, (*11)

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']]])
    ->getIsResponse()
    ->modifyBody(function (array $body) {
        $body['balance'] = -5.75;

        return $body;
    });

// Delete imposter and post again
$juggler->replaceImposter($imposter);

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;
$imposter->createStub(
    [new IsResponse(200, ['Content-type' => 'application/json'], '{"status":200}')],
    [new Predicate(Predicate::OPERATOR_EQUALS, ['method' => 'GET', 'path' => '/test-endpoint'])]
);

// Post it!
$juggler->postImposter($imposter);

Testing

bash $ composer test, (*12)

Credits

License

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

The Versions

02/03 2017

dev-master

9999999-dev https://github.com/meare/juggler

PHP client for Mountebank

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrejs Mironovs

api testing client mountebank component testing

17/01 2017

v1.0

1.0.0.0 https://github.com/meare/juggler

PHP client for Mountebank

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrejs Mironovs

api testing client mountebank component testing

17/10 2016

v0.9-beta

0.9.0.0-beta https://github.com/TransactPRO/mountebank-php

PHP client for Mountebank

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andrejs Mironovs

api testing client mountebank component testing