Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
1ba760f
to8accca2
CompareUh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
friendly ping@Koc |
d5bb399
toda57266
Compare@nicolas-grekas I've updated the PR. |
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)
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
@nicolas-grekas done |
Try to restart build |
@nicolas-grekas all green |
Thank you@Koc. |
…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 yetFor 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
Uh oh!
There was an error while loading.Please reload this page.
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.