mcustiel/phiremock-server
PHP HTTP mock/stub server inspired by WireMock. Mock requests by method/headers/URL/body/forms, set responses via REST API, support scenarios, priorities, latency simulation, verification counts, proxying, and loading expectations from JSON files.
composer require-dev mcustiel/phiremock-server guzzlehttp/guzzle
8086):
./vendor/bin/phiremock
// config/services.php (local/acceptance env)
'external_api' => env('EXTERNAL_API_URL', 'http://localhost:8086'),
phiremock-client):
curl -X POST http://localhost:8086/__phiremock/expectations \
-H "Content-Type: application/json" \
-d '{
"version": "2",
"on": {
"method": {"isSameString": "GET"},
"url": {"isEqualTo": "/api/users/1"}
},
"then": {
"response": {
"statusCode": 200,
"body": "{\"id\":1,\"name\":\"John\"}",
"headers": {"Content-Type": "application/json"}
}
}
}'
http://localhost:8086/api/users/1—your app will receive the mocked response.Use Laravel's environment files to toggle between real and mocked services:
// .env.local
EXTERNAL_API_URL=http://localhost:8086
// .env.acceptance
EXTERNAL_API_URL=http://localhost:8086
Leverage phiremock-client in PHPUnit tests:
use Mcustiel\Phiremock\Client\Client;
public function testUserFetch()
{
$client = new Client('http://localhost:8086');
$client->addExpectation([
'on' => ['method' => 'GET', 'url' => '/api/users/1'],
'then' => ['response' => ['statusCode' => 200, 'body' => '{"id":1}']]
]);
$response = Http::get('/api/users/1');
$this->assertEquals(200, $response->status());
}
Simulate stateful APIs (e.g., OAuth flows):
// First request (sets state)
$client->addExpectation([
'scenarioName' => 'auth_flow',
'on' => ['scenarioStateIs' => 'START', 'method' => 'POST', 'url' => '/login'],
'then' => [
'newScenarioState' => 'AUTHORIZED',
'response' => ['statusCode' => 200, 'body' => '{"token":"abc123"}']
]
]);
// Subsequent request (uses state)
$client->addExpectation([
'scenarioName' => 'auth_flow',
'on' => ['scenarioStateIs' => 'AUTHORIZED', 'method' => 'GET', 'url' => '/protected'],
'then' => ['response' => ['statusCode' => 200, 'body' => '{"data":"secret"}']]
]);
Store expectations in ~/.phiremock/expectations/ (e.g., users.json):
{
"version": "2",
"on": {"method": "GET", "url": {"matches": "~^/api/users/~"}},
"then": {"response": {"statusCode": 200, "body": "phiremock.base64:..."}}
}
Start Phiremock with:
./vendor/bin/phiremock --expectations-dir ~/.phiremock/expectations
Use Laravel’s Http facade with Phiremock:
$response = Http::withOptions(['verify' => false])->get('http://localhost:8086/api/data');
Case Sensitivity in Headers/URLs:
Phiremock matches headers/URLs case-sensitively by default. Use isEqualToIgnoringCase for flexibility.
"headers": {"Accept": {"isEqualToIgnoringCase": "application/json"}}
Base64 Encoding for Binary Data:
Forgetting phiremock.base64: prefix will return raw base64 strings instead of decoded binary.
"body": "phiremock.base64:SGVsbG8gV29ybGQ=" // Correct (decodes to "Hello World")
Priority Conflicts: Unintended priority overlaps can cause flaky tests. Explicitly set priorities for critical paths:
"priority": 100 // Highest priority
Debugging Stuck Requests: Enable debug mode to log unmatched requests:
./vendor/bin/phiremock --debug
curl http://localhost:8086/__phiremock/expectations
curl -X POST http://localhost:8086/__phiremock/executions \
-d '{"request": {"method": "GET", "url": {"isEqualTo": "/api/users"}}}'
curl -X POST http://localhost:8086/__phiremock/reset
Custom HTTP Client:
Override the factory class to use Guzzle v7 or Symfony’s HttpClient:
// app/Providers/PhiremockServiceProvider.php
public function register()
{
$this->app->bind(
\Mcustiel\Phiremock\Client\Factory::class,
\App\Phiremock\Guzzle7Factory::class
);
}
Dynamic Expectations from Database: Load expectations from Laravel’s database in a service provider:
public function boot()
{
$client = new Client('http://localhost:8086');
foreach (DB::table('mock_expectations')->get() as $expectation) {
$client->addExpectation(json_decode($expectation->json, true));
}
}
Codeception Integration:
Use the phiremock-codeception-extension for test-specific mocks:
# codeception.yml
extensions:
enabled:
- Phiremock
"then": {"delayMillis": 1500, "response": {...}}
$client->reset();
--certificate and --certificate-key for secure testing:
./vendor/bin/phiremock --certificate cert.pem --certificate-key key.pem
~/.phiremock/certs/).How can I help you explore Laravel packages today?