Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #34871 [HttpClient] Allow pass array of callable to the mocki…
…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
- Loading branch information