This is a fixture client for PHP-HTTP and is meant for testing purposes. It maps requests to static fixtures.
$ composer require --dev swisnl/php-http-fixture-client
// Create client
$responseBuilder = new \Pbxapi\Http\Fixture\ResponseBuilder('/path/to/fixtures');
$client = new \Pbxapi\Http\Fixture\Client($responseBuilder);
// Send request
$response = $client->sendRequest(new Request(...));
All requests send using this client are mapped to static fixtures located in the provided path. URLs are transformed to file paths by using the domain and path fragments and (optionally) the method and/or the query params (sorted alphabetically). A list of possible fixture paths is made and handled in order of specificity:
- {path}.{query}.{method}.mock
- {path}.{query}.mock
- {path}.{method}.mock
- {path}.mock
Please see the following table for some examples.
URL | Method | Possible fixtures (in order of specificity) |
---|---|---|
http://example.com/api/articles/1 | GET | /path/to/fixtures/example.com/api/articles/1.get.mock |
/path/to/fixtures/example.com/api/articles/1.mock | ||
http://example.com/api/articles | POST | /path/to/fixtures/example.com/api/articles.post.mock |
/path/to/fixtures/example.com/api/articles.mock | ||
http://example.com/api/comments?query=json | GET | /path/to/fixtures/example.com/api/comments.query=json.get.mock |
/path/to/fixtures/example.com/api/comments.query=json.mock | ||
/path/to/fixtures/example.com/api/comments.get.mock | ||
/path/to/fixtures/example.com/api/comments.mock | ||
http://example.com/api/comments?query=json&order=id | GET | /path/to/fixtures/example.com/api/comments.order=id&query=json.get.mock |
/path/to/fixtures/example.com/api/comments.order=id&query=json.mock | ||
/path/to/fixtures/example.com/api/comments.get.mock | ||
/path/to/fixtures/example.com/api/comments.mock |
The ReponseBuilder
can be set to strict mode using setStrictMode(true)
.
When in strict mode, only the first possible fixture path will be used.
This means that both the method and query params must be present in the fixture file name and it does not fall back to other fixture files.
Please see https://swisnl.github.io/php-http-fixture-client/#helper for the URL helper.
The body of a request is loaded directly from a fixture with the file extension .mock.
The contents of this file can be anything that is a valid HTTP response, e.g. HTML, JSON or even images.
If a fixture can not be found, a MockNotFoundException
will be thrown.
This exception has a convenience method getPossiblePaths()
which lists all file paths that were checked, in order of specificity.
The headers of a request are loaded from a fixture with the file extension .headers. This is a simple JSON object with headers, e.g.
{
"X-Made-With": "PHPUnit"
}
The status code of a request is loaded from a fixture with the file extension .status. This is a plain file containing only the HTTP status code. If no .status file is found, 200 OK will be used.
This client extends php-http/mock-client, which allows you to add custom responses and exceptions that ignore fixture files. Please see the mock-client documentation for more information.
N.B. A default response can not be set because this client uses that under the hood.
Please see CHANGELOG for more information on what has changed recently.
$ composer test
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
If you discover any security related issues, please email security@swis.nl instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
SWIS is a web agency from Leiden, the Netherlands. We love working with open source software.