Skip to content

Commit deb4448

Browse files
committed
[HttpClient] Improve MockHttpClient exception message
1 parent 279f53a commit deb4448

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
@@ -72,7 +72,7 @@ public function request(string $method, string $url, array $options = []): Respo
7272
} elseif (\is_callable($this->responseFactory)) {
7373
$response = ($this->responseFactory)($method, $url, $options);
7474
} elseif (!$this->responseFactory->valid()) {
75-
throw new TransportException('The response factory iterator passed to MockHttpClient is empty.');
75+
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.');
7676
} else {
7777
$responseFactory = $this->responseFactory->current();
7878
$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
@@ -527,4 +527,23 @@ public function testCancelingMockResponseExecutesOnProgressWithUpdatedInfo()
527527

528528
$this->assertTrue($canceled);
529529
}
530+
531+
public function testEmptyResponseFactory()
532+
{
533+
$this->expectException(TransportException::class);
534+
$this->expectExceptionMessage('The response factory iterator passed to MockHttpClient is empty.');
535+
536+
$client = new MockHttpClient([]);
537+
$client->request('GET', 'https://example.com');
538+
}
539+
540+
public function testMoreRequestsThanResponseFactoryResponses()
541+
{
542+
$this->expectException(TransportException::class);
543+
$this->expectExceptionMessage('No more response left in the response factory iterator passed to MockHttpClient: the number of requests exceeds the number of responses.');
544+
545+
$client = new MockHttpClient([new MockResponse()]);
546+
$client->request('GET', 'https://example.com');
547+
$client->request('GET', 'https://example.com');
548+
}
530549
}

0 commit comments

Comments
 (0)