Skip to content

Commit 80cd9a5

Browse files
minor #50062 [HttpClient] Improve MockHttpClient exception message (fancyweb)
This PR was merged into the 6.3 branch. Discussion ---------- [HttpClient] Improve MockHttpClient exception message | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | symfony/symfony#49874 (comment) | License | MIT | Doc PR | - When using `MockHttpClient`, if there are more requests done on the client than the passed available responses, the exception message is misleading. ~Targeting 5.4 since it only changes the exception message 😅~ Commits ------- bd5a4caef4 [HttpClient] Improve MockHttpClient exception message
2 parents 6fbb384 + deb4448 commit 80cd9a5

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

MockHttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function request(string $method, string $url, array $options = []): Respo
6969
} elseif (\is_callable($this->responseFactory)) {
7070
$response = ($this->responseFactory)($method, $url, $options);
7171
} elseif (!$this->responseFactory->valid()) {
72-
throw new TransportException('The response factory iterator passed to MockHttpClient is empty.');
72+
throw new TransportException($this->requestsCount ? 'No more response left in the response factory iterator passed to MockHttpClient: the number of requests exceeds the number of responses.' : 'The response factory iterator passed to MockHttpClient is empty.');
7373
} else {
7474
$responseFactory = $this->responseFactory->current();
7575
$response = \is_callable($responseFactory) ? $responseFactory($method, $url, $options) : $responseFactory;

Tests/MockHttpClientTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,4 +554,23 @@ public function testCancelingMockResponseExecutesOnProgressWithUpdatedInfo()
554554

555555
$this->assertTrue($canceled);
556556
}
557+
558+
public function testEmptyResponseFactory()
559+
{
560+
$this->expectException(TransportException::class);
561+
$this->expectExceptionMessage('The response factory iterator passed to MockHttpClient is empty.');
562+
563+
$client = new MockHttpClient([]);
564+
$client->request('GET', 'https://example.com');
565+
}
566+
567+
public function testMoreRequestsThanResponseFactoryResponses()
568+
{
569+
$this->expectException(TransportException::class);
570+
$this->expectExceptionMessage('No more response left in the response factory iterator passed to MockHttpClient: the number of requests exceeds the number of responses.');
571+
572+
$client = new MockHttpClient([new MockResponse()]);
573+
$client->request('GET', 'https://example.com');
574+
$client->request('GET', 'https://example.com');
575+
}
557576
}

0 commit comments

Comments
 (0)