Skip to content

Commit 3e387cf

Browse files
feature #43280 [HttpClient] Add method to set response factory in mock client (greeflas)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [HttpClient] Add method to set response factory in mock client | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #40620 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#15883 <!-- required for new features --> <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Never break backward compatibility (see https://symfony.com/bc). - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too.) - Features and deprecations must be submitted against branch 5.x. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry --> Commits ------- 2e028cc [HttpClient] Add method to set response factory in mock client
2 parents 333d1d8 + 2e028cc commit 3e387cf

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/Symfony/Component/HttpClient/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.4
5+
---
6+
7+
* Add `MockHttpClient::setResponseFactory()` method to be able to set response factory after client creating
8+
49
5.3
510
---
611

src/Symfony/Component/HttpClient/MockHttpClient.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ class MockHttpClient implements HttpClientInterface
3535
* @param callable|callable[]|ResponseInterface|ResponseInterface[]|iterable|null $responseFactory
3636
*/
3737
public function __construct($responseFactory = null, ?string $baseUri = 'https://example.com')
38+
{
39+
$this->setResponseFactory($responseFactory);
40+
$this->defaultOptions['base_uri'] = $baseUri;
41+
}
42+
43+
/**
44+
* @param callable|callable[]|ResponseInterface|ResponseInterface[]|iterable|null $responseFactory
45+
*/
46+
public function setResponseFactory($responseFactory): void
3847
{
3948
if ($responseFactory instanceof ResponseInterface) {
4049
$responseFactory = [$responseFactory];
@@ -47,7 +56,6 @@ public function __construct($responseFactory = null, ?string $baseUri = 'https:/
4756
}
4857

4958
$this->responseFactory = $responseFactory;
50-
$this->defaultOptions['base_uri'] = $baseUri;
5159
}
5260

5361
/**

src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ protected function getHttpClient(string $testCase): HttpClientInterface
291291
case 'testReentrantBufferCallback':
292292
case 'testThrowingBufferCallback':
293293
case 'testInfoOnCanceledResponse':
294+
case 'testChangeResponseFactory':
294295
$responses[] = new MockResponse($body, ['response_headers' => $headers]);
295296
break;
296297

@@ -387,4 +388,16 @@ public function testHttp2PushVulcainWithUnusedResponse()
387388
{
388389
$this->markTestSkipped('MockHttpClient doesn\'t support HTTP/2 PUSH.');
389390
}
391+
392+
public function testChangeResponseFactory()
393+
{
394+
/* @var MockHttpClient $client */
395+
$client = $this->getHttpClient(__METHOD__);
396+
$expectedBody = '{"foo": "bar"}';
397+
$client->setResponseFactory(new MockResponse($expectedBody));
398+
399+
$response = $client->request('GET', 'http://localhost:8057');
400+
401+
$this->assertSame($expectedBody, $response->getContent());
402+
}
390403
}

0 commit comments

Comments
 (0)