-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpClient] Allow pass array of callable to the mocking http client #34871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
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
1ba760f
to
8accca2
Compare
8accca2
to
5be6d7f
Compare
friendly ping @Koc |
d5bb399
to
da57266
Compare
@nicolas-grekas I've updated the PR. |
nicolas-grekas
approved these changes
Jan 20, 2020
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(with minor comments)
da57266
to
a36797d
Compare
@nicolas-grekas done |
Try to restart build |
@nicolas-grekas all green |
Thank you @Koc. |
nicolas-grekas
added a commit
that referenced
this pull request
Feb 2, 2020
…ng http client (Koc) This PR was merged into the 5.1-dev branch. Discussion ---------- [HttpClient] Allow pass array of callable to the mocking http client | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | not yet For the now MockHttpClient allows pass closure as response factory. It useful for tests to perform assertions that expected request was sent. But If we are sending multiple sequental requests then it became a little bit tricky to perform assertions: ```php <?php $requestIndex = 0; $expectedRequest = function ($method, $url, $options) use (&$requestIndex) { switch (++$requestIndex) { case 1: $this->assertSame('GET', $method); $this->assertSame('https://example.com/api/v1/customer', $url); return new MockResponse(CustomerFixture::CUSTOMER_RESPONSE); case 2: $this->assertSame('POST', $method); $this->assertSame('https://example.com/api/v1/customer/1/products', $url); $this->assertJsonStringEqualsJsonFile(CustomerFixture::CUSTOMER_PRODUCT_PAYLOAD, $options['json']); return new MockResponse(); default: throw new \InvalidArgumentException('Too much requests'); } }; $client = new MockHttpClient($expectedRequest); static::$container->set('http_client.example', $client); $commandTester->execute(['--since' => '2019-01-01 00:05:00', '--until' => '2019-01-01 00:35:00']); $this->assertSame(2, $requestIndex, 'All expected requests was sent.'); ``` This PR introduces possibility to define multiple callable response factories and `getSentRequestsCount` method to make sure that each factory was called: ```php <?php $expectedRequests = [ function ($method, $url, $options) { $this->assertSame('GET', $method); $this->assertSame('https://example.com/api/v1/customer', $url); return new MockResponse(CustomerFixture::CUSTOMER_RESPONSE); }, function ($method, $url, $options) { $this->assertSame('POST', $method); $this->assertSame('https://example.com/api/v1/customer/1/products', $url); $this->assertJsonStringEqualsJsonFile(CustomerFixture::CUSTOMER_PRODUCT_PAYLOAD, $options['json']); return new MockResponse(); }, ]; $client = new MockHttpClient($expectedRequest); static::$container->set('http_client.example', $client); $commandTester->execute(['--since' => '2019-01-01 00:05:00', '--until' => '2019-01-01 00:35:00']); $this->assertSame(2, $client->getSentRequestsCount(), 'All expected requests was sent.'); ``` Also it adds a lot of tests. Commits ------- a36797d Allow pass array of callable to the mocking http client
Merged
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.
For the now MockHttpClient allows pass closure as response factory. It useful for tests to perform assertions that expected request was sent. But If we are sending multiple sequental requests then it became a little bit tricky to perform assertions:
This PR introduces possibility to define multiple callable response factories and
getSentRequestsCount
method to make sure that each factory was called:Also it adds a lot of tests.