Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #30604 [HttpClient] add MockHttpClient (nicolas-grekas)
This PR was merged into the 4.3-dev branch. Discussion ---------- [HttpClient] add MockHttpClient | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This PR introduces `MockHttpClient` and `MockResponse`, to be used for testing classes that need an HTTP client without making actual HTTP requests. `MockHttpClient` is configured via its constructor: you provide it either with an iterable or a callable, and these will be used to provide responses as the consumer requests them. Example: ```php $responses = [ new MockResponse($body1, $info1), new MockResponse($body2, $info2), ]; $client = new MockHttpClient($responses); $response1 = $client->request(...); // created from $responses[0] $response2 = $client->request(...); // created from $responses[1] ``` Or alternatively: ```php $callback = function ($method, $url, $options) { return new MockResponse(...); }; $client = new MockHttpClient($callback); $response = $client->request(...); // calls $callback internal ``` The responses provided to the client don't have to be instances of `MockResponse` - any `ResponseInterface` works (e.g. `$this->getMockBuilder(ResponseInterface::class)->getMock()`). Using `MockResponse` allows simulating chunked responses and timeouts: ```php $body = function () { yield 'hello'; yield ''; // the empty string is turned into a timeout so that they are easy to test yield 'world'; }; $mockResponse = new Mockresponse($body); ``` Last but not least, the implementation simulates the full lifecycle of a properly behaving `HttpClientInterface` contracts implementation: error handling, progress function, etc. This is "proved" by `MockHttpClientTest`, who implements and passes the reference test suite in `HttpClientTestCase`. Commits ------- 8fd7584 [HttpClient] add MockHttpClient
- Loading branch information