PHP code example of meare / juggler

1. Go to this page and download the library: Download meare/juggler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

meare / juggler example snippets


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

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

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