[HttpClient] add MockHttpClient#30604
Merged
fabpot merged 1 commit intosymfony:masterfrom Mar 19, 2019
Merged
Conversation
This was referenced Mar 19, 2019
9a8f9a3 to
07bc27b
Compare
fabpot
approved these changes
Mar 19, 2019
8a7b0fc to
3a16803
Compare
Nyholm
reviewed
Mar 19, 2019
Member
Nyholm
left a comment
There was a problem hiding this comment.
I did a quick review. It looks alright, but I have not tested it yet.
weaverryan
reviewed
Mar 19, 2019
Contributor
weaverryan
left a comment
There was a problem hiding this comment.
Also haven't tested it, but the user experience looks wonderful!
3a16803 to
8fd7584
Compare
Member
|
Thank you @nicolas-grekas. |
fabpot
added a commit
that referenced
this pull request
Mar 19, 2019
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
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR introduces
MockHttpClientandMockResponse, to be used for testing classes that need an HTTP client without making actual HTTP requests.MockHttpClientis 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:
Or alternatively:
The responses provided to the client don't have to be instances of
MockResponse- anyResponseInterfaceworks (e.g.$this->getMockBuilder(ResponseInterface::class)->getMock()).Using
MockResponseallows simulating chunked responses and timeouts:Last but not least, the implementation simulates the full lifecycle of a properly behaving
HttpClientInterfacecontracts implementation: error handling, progress function, etc. This is "proved" byMockHttpClientTest, who implements and passes the reference test suite inHttpClientTestCase.