free-elephants/static-http-client

PSR-18 implementation for work with file fixtures

0.0.3 2025-04-25 21:14 UTC

This package is auto-updated.

Last update: 2025-04-25 21:15:18 UTC


README

PSR-18 implementation for testing. Use static files for mock responses and write requests.

Installation

composer require free-elephants/static-http-client --dev

Usage

See tests for more examples.

public function test()
{
    $stubbedHttpClient = new \FreeElephants\StaticHttpClient\StaticHttpClient(
        $responseFactory, 
        new \FreeElephants\StaticHttpClient\PathResolver\PathBuilderBasedResolver(
            new \FreeElephants\StaticHttpClient\PathBuilder\Composite(
                new \FreeElephants\StaticHttpClient\PathBuilder\PrependBasePath(__DIR__),
                new \FreeElephants\StaticHttpClient\PathBuilder\PrependHostnameAsDirectory(),
                new \FreeElephants\StaticHttpClient\PathBuilder\AppendRequestPath(),
                new \FreeElephants\StaticHttpClient\PathBuilder\AppendDefaultFileExtension('.json'),
            ) 
        )
    );
    
    $response = $stubbedHttpClient->sendRequest(new \Nyholm\Psr7\Request('GET', 'https://example.com/foo')); // resolved as __DIR__ . '/example.com/foo.json'  
}