|
5 | 5 | [](LICENSE)
|
6 | 6 | [](https://app.shippable.com/github/swisnl/php-http-fixture-client)
|
7 | 7 | [](https://app.shippable.com/github/swisnl/php-http-fixture-client)
|
| 8 | + |
| 9 | +This is a fixture client for PHP-HTTP and is meant for testing purposes. |
| 10 | +It maps requests to static fixtures. |
| 11 | + |
| 12 | +## Install |
| 13 | + |
| 14 | +``` bash |
| 15 | +composer require --dev swisnl/php-http-fixture-client |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +``` php |
| 21 | +// Create client |
| 22 | +$responseBuilder = new \Swis\Http\Fixture\ResponseBuilder('/path/to/fixtures'); |
| 23 | +$client = new \Swis\Http\Fixture\Client($responseBuilder); |
| 24 | + |
| 25 | +// Send request |
| 26 | +$response = $client->sendRequest(new Request(...)); |
| 27 | +``` |
| 28 | + |
| 29 | +## Fixture mapping |
| 30 | + |
| 31 | +All requests send using this client are mapped to static fixtures located in the provided path. |
| 32 | +URLs are transformed to file paths by using the domain and path fragments and (optionally) the method and/or the query params (sorted alphabetically). |
| 33 | +A list of possible fixture paths is made and handled in order of specificity: |
| 34 | + |
| 35 | + 1. {path}.{query}.{method}.mock |
| 36 | + 2. {path}.{query}.mock |
| 37 | + 3. {path}.{method}.mock |
| 38 | + 4. {path}.mock |
| 39 | + |
| 40 | +Please see the following table for some examples. |
| 41 | + |
| 42 | +| URL | Method | Possible fixtures (in order of specificity) | |
| 43 | +| --- | ------ | ------------------------------------------- | |
| 44 | +| http://example.com/api/articles/1 | GET | /path/to/fixtures/example.com/api/articles/1.get.mock | |
| 45 | +| | | /path/to/fixtures/example.com/api/articles/1.mock | |
| 46 | +| http://example.com/api/articles | POST | /path/to/fixtures/example.com/api/articles.post.mock | |
| 47 | +| | | /path/to/fixtures/example.com/api/articles.mock | |
| 48 | +| http://example.com/api/comments?query=json | GET | /path/to/fixtures/example.com/api/comments.query-json.get.mock | |
| 49 | +| | | /path/to/fixtures/example.com/api/comments.query-json.mock | |
| 50 | +| | | /path/to/fixtures/example.com/api/comments.get.mock | |
| 51 | +| | | /path/to/fixtures/example.com/api/comments.mock | |
| 52 | +| http://example.com/api/comments?query=json&order=id | GET | /path/to/fixtures/example.com/api/comments.order-id-query-json.get.mock | |
| 53 | +| | | /path/to/fixtures/example.com/api/comments.order-id-query-json.mock | |
| 54 | +| | | /path/to/fixtures/example.com/api/comments.get.mock | |
| 55 | +| | | /path/to/fixtures/example.com/api/comments.mock | |
| 56 | + |
| 57 | +### Body |
| 58 | + |
| 59 | +The body of a request is loaded directly from a fixture with the file extension _.mock_. |
| 60 | +The contents of this file can be anything that is a valid HTTP response, e.g. HTML, JSON or even images. |
| 61 | +If a fixture can not be found, a `MockNotFoundException` will be thrown. |
| 62 | +This exception has a convenience method `getPossiblePaths()` which lists all file paths that were checked, in order of specificity. |
| 63 | + |
| 64 | +### Headers (optional) |
| 65 | + |
| 66 | +The headers of a request are loaded from a fixture with the file extension _.headers_. |
| 67 | +This is a simple JSON object with headers, e.g. |
| 68 | +``` json |
| 69 | +{ |
| 70 | + "X-Made-With": "PHPUnit" |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +### Status (optional) |
| 75 | + |
| 76 | +The status code of a request is loaded from a fixture with the file extension _.status_. |
| 77 | +This is a plain file containing only the [HTTP status code](https://httpstatuses.com/). |
| 78 | +If no _.status_ file is found, _200 OK_ will be used. |
| 79 | + |
| 80 | +## Testing |
| 81 | + |
| 82 | +``` bash |
| 83 | +composer test |
| 84 | +``` |
| 85 | + |
| 86 | +## Security |
| 87 | + |
| 88 | +If you discover any security related issues, please email [email protected] instead of using the issue tracker. |
| 89 | + |
| 90 | +## License |
| 91 | + |
| 92 | +The MIT License (MIT). Please see [License File](LICENSE) for more information. |
0 commit comments