psr-mock/http-client-implementation
Tiny PSR-18 HTTP client implementation intended for mocking and testing. Provides a simple client you can plug into PSR-7/PSR-17 workflows to return predefined responses without real network calls.
Installation
composer require --dev psr-mock/http-client-implementation:^1.0
First Use Case: Mocking HTTP Calls in Tests
use Psr\Http\Message\RequestInterface;
use Psr\Http\Client\ClientInterface;
use PsrMock\Http\Client\MockClient;
// In your test class:
$mockClient = new MockClient();
$mockClient->mockResponse('https://api.example.com/users', '{"id": 1, "name": "John"}');
// Inject into your service under test:
$service = new MyService($mockClient);
Where to Look First
src/PsrMock/Http/Client/MockClient.php for core functionality (PHP 8.1+ syntax).tests/ directory for usage examples (verify compatibility with PHP 8.1).Basic Mocking (PHP 8.1+)
$mockClient = new MockClient();
$mockClient->mockResponse('https://api.example.com/data', '{"key": "value"}');
$response = $mockClient->sendRequest(new \GuzzleHttp\Psr7\Request('GET', 'https://api.example.com/data'));
$this->assertEquals('{"key": "value"}', $response->getBody()->getContents());
Dynamic Responses (PHP 8.1+)
$mockClient->mockResponse(
'https://api.example.com/users',
function (RequestInterface $request) {
$query = $request->getUri()->getQuery();
return new \GuzzleHttp\Psr7\Response(200, [], json_encode(['query' => $query]));
}
);
Exception Handling (PHP 8.1+)
$mockClient->mockException('https://api.example.com/error', new \RuntimeException('API down'));
Integration with Laravel (PHP 8.1+)
// In Laravel tests:
$mockClient = new MockClient();
$this->app->instance(ClientInterface::class, $mockClient);
Testing Middleware (PHP 8.1+)
$mockClient = new MockClient();
$stack = HandlerStack::create();
$stack->push(Middleware::class);
$mockClient->setHandler($stack);
$httpClient = new \Illuminate\Http\Client\PendingRequest($mockClient);
$response = $httpClient->get('https://api.example.com/users');
$mockClient = MockClient::withResponses([
'https://api.example.com/users' => '{"id": 1}',
'https://api.example.com/posts' => '{"title": "Test"}',
]);
PHP Version Requirement
^0.9).URL Matching Strictness
$mockClient->mockResponse('/api/users/*', '{"id": 1}'); // Partial match
$mockClient->mockResponse('/api/users/[0-9]+', function () { ... });
Case Sensitivity
GET, Post) are case-insensitive, but ensure consistency in tests.Response Order
Handler Overrides
setHandler()), it replaces all mocked responses. Use cautiously.$mockClient->getMockedResponses(); // Returns all mocked responses.
$lastRequest = $mockClient->getLastRequest();
$this->assertEquals('GET', $lastRequest->getMethod());
$mockClient->setResponseFactory(function ($url, $response) {
return new CustomResponse($response);
});
MockClient to support custom middleware in tests:
class TestableClient extends MockClient {
public function addTestMiddleware(callable $middleware) {
$this->getHandler()->push($middleware);
}
}
$mockClient->enableRequestLogging();
$logs = $mockClient->getRequestLogs();
How can I help you explore Laravel packages today?