PHP client for MockServer - enables easy mocking of any system you integrate with via HTTP or HTTPS.
- PHP 8.1+
- Composer
composer require mock-server/mockserver-client<?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 all expectations
$client->reset();use MockServer\Times;
use MockServer\TimeToLive;
use MockServer\Delay;
use MockServer\HttpForward;
// Respond with a delay
$client->when(
HttpRequest::request()->method('POST')->path('/api/data')
->header('Content-Type', 'application/json')
->jsonBody(['key' => 'value'])
)->respond(
HttpResponse::response()
->statusCode(201)
->body('{"id": 1}')
->delay(Delay::milliseconds(500))
);
// Match only 3 times, with priority
$client->when(
HttpRequest::request()->path('/limited'),
Times::exactly(3),
TimeToLive::exactly('SECONDS', 60),
priority: 10
)->respond(
HttpResponse::response()->statusCode(200)
);
// Forward to another server
$client->when(
HttpRequest::request()->path('/proxy')
)->forward(
HttpForward::forward()->host('backend.local')->port(8080)->scheme('HTTP')
);use MockServer\VerificationTimes;
// Verify at least once
$client->verify(
HttpRequest::request()->path('/hello'),
VerificationTimes::atLeast(1)
);
// Verify exactly 3 times
$client->verify(
HttpRequest::request()->method('POST')->path('/api'),
VerificationTimes::exactly(3)
);
// Verify sequence
$client->verifySequence(
HttpRequest::request()->path('/first'),
HttpRequest::request()->path('/second')
);// Retrieve recorded requests
$requests = $client->retrieveRecordedRequests(
HttpRequest::request()->path('/api')
);
// Retrieve active expectations
$expectations = $client->retrieveActiveExpectations();
// Retrieve log messages
$logs = $client->retrieveLogMessages();// Clear specific expectations/logs
$client->clear(HttpRequest::request()->path('/old'));
$client->clear(null, 'EXPECTATIONS'); // type: EXPECTATIONS, LOG, or ALL
$client->clearById('my-expectation-id');
// Reset everything
$client->reset();
// Check server status
$status = $client->status(); // ['ports' => [1080]]
// Bind additional ports
$client->bind(1081, 1082);
// Check if server is running
if ($client->hasStarted()) {
echo "MockServer is ready";
}The PHP client does not include a binary launcher (PHP lacks the native WebSocket and subprocess management required for embedded launch). To start MockServer, use one of the following approaches:
- Docker:
docker run -d -p 1080:1080 mockserver/mockserver - Executable JAR:
java -jar mockserver-netty-no-dependencies-<version>.jar -serverPort 1080 - Homebrew:
brew install mockserver && mockserver -serverPort 1080 - Another client's launcher: The Node, Python, Ruby, Go, .NET, and Rust clients can each download and launch MockServer automatically without Java or Docker.
See the Running MockServer documentation for all available options.
composer installUnit tests (no server required):
vendor/bin/phpunit --testsuite UnitIntegration tests (requires a running MockServer):
MOCKSERVER_URL=http://localhost:1080 vendor/bin/phpunit --testsuite IntegrationApache 2.0 - see LICENSE