Packagist   Packagist version

The mock-server/mockserver-client package provides a PHP client for MockServer's control-plane REST API, with a fluent builder API covering expectations, verification, retrieval, and control operations. PHP 8.1+ and Composer are required.

Install

composer require mock-server/mockserver-client

Quick start

<?php

use MockServer\MockServerClient;
use MockServer\HttpRequest;
use MockServer\HttpResponse;
use MockServer\VerificationTimes;

// Connect to MockServer
$client = new MockServerClient('localhost', 1080);

// Create an expectation
$client->when(
    HttpRequest::request()->method('GET')->path('/hello')
)->respond(
    HttpResponse::response()
        ->statusCode(200)
        ->header('Content-Type', 'application/json')
        ->body('{"message":"world"}')
);

// Verify the request was received
$client->verify(
    HttpRequest::request()->path('/hello'),
    VerificationTimes::atLeast(1)
);

// Reset
$client->reset();

Starting MockServer

The PHP client does not include a binary launcher. To start MockServer, use one of these approaches:

For the full PHP client API, see MockServer clients.