From 2e028cccef4e917b6c4fbbdc5a5b9edf2db91df2 Mon Sep 17 00:00:00 2001 From: Volodymyr Kupriienko Date: Fri, 1 Oct 2021 20:55:23 +0300 Subject: [PATCH] [HttpClient] Add method to set response factory in mock client --- src/Symfony/Component/HttpClient/CHANGELOG.md | 5 +++++ src/Symfony/Component/HttpClient/MockHttpClient.php | 10 +++++++++- .../HttpClient/Tests/MockHttpClientTest.php | 13 +++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpClient/CHANGELOG.md b/src/Symfony/Component/HttpClient/CHANGELOG.md index c0e6fc88a451c..7c2fc2273b96a 100644 --- a/src/Symfony/Component/HttpClient/CHANGELOG.md +++ b/src/Symfony/Component/HttpClient/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +5.4 +--- + + * Add `MockHttpClient::setResponseFactory()` method to be able to set response factory after client creating + 5.3 --- diff --git a/src/Symfony/Component/HttpClient/MockHttpClient.php b/src/Symfony/Component/HttpClient/MockHttpClient.php index 538243f56c85d..361fe29f1519e 100644 --- a/src/Symfony/Component/HttpClient/MockHttpClient.php +++ b/src/Symfony/Component/HttpClient/MockHttpClient.php @@ -35,6 +35,15 @@ class MockHttpClient implements HttpClientInterface * @param callable|callable[]|ResponseInterface|ResponseInterface[]|iterable|null $responseFactory */ public function __construct($responseFactory = null, ?string $baseUri = 'https://example.com') + { + $this->setResponseFactory($responseFactory); + $this->defaultOptions['base_uri'] = $baseUri; + } + + /** + * @param callable|callable[]|ResponseInterface|ResponseInterface[]|iterable|null $responseFactory + */ + public function setResponseFactory($responseFactory): void { if ($responseFactory instanceof ResponseInterface) { $responseFactory = [$responseFactory]; @@ -47,7 +56,6 @@ public function __construct($responseFactory = null, ?string $baseUri = 'https:/ } $this->responseFactory = $responseFactory; - $this->defaultOptions['base_uri'] = $baseUri; } /** diff --git a/src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php b/src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php index 7e64e67f1706b..168be240c7aaa 100644 --- a/src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php +++ b/src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php @@ -291,6 +291,7 @@ protected function getHttpClient(string $testCase): HttpClientInterface case 'testReentrantBufferCallback': case 'testThrowingBufferCallback': case 'testInfoOnCanceledResponse': + case 'testChangeResponseFactory': $responses[] = new MockResponse($body, ['response_headers' => $headers]); break; @@ -387,4 +388,16 @@ public function testHttp2PushVulcainWithUnusedResponse() { $this->markTestSkipped('MockHttpClient doesn\'t support HTTP/2 PUSH.'); } + + public function testChangeResponseFactory() + { + /* @var MockHttpClient $client */ + $client = $this->getHttpClient(__METHOD__); + $expectedBody = '{"foo": "bar"}'; + $client->setResponseFactory(new MockResponse($expectedBody)); + + $response = $client->request('GET', 'http://localhost:8057'); + + $this->assertSame($expectedBody, $response->getContent()); + } }