diff --git a/Response/MockResponse.php b/Response/MockResponse.php index 800ac45..385487d 100644 --- a/Response/MockResponse.php +++ b/Response/MockResponse.php @@ -285,6 +285,10 @@ private static function readResponse(self $response, array $options, ResponseInt 'http_code' => $response->info['http_code'], ] + $info + $response->info; + if (null !== $response->info['error']) { + throw new TransportException($response->info['error']); + } + if (!isset($response->info['total_time'])) { $response->info['total_time'] = microtime(true) - $response->info['start_time']; } diff --git a/Tests/MockHttpClientTest.php b/Tests/MockHttpClientTest.php index 12bc2d8..9f47d10 100644 --- a/Tests/MockHttpClientTest.php +++ b/Tests/MockHttpClientTest.php @@ -18,7 +18,6 @@ use Symfony\Component\HttpClient\Response\ResponseStream; use Symfony\Contracts\HttpClient\ChunkInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; -use Symfony\Contracts\HttpClient\ResponseInterface; class MockHttpClientTest extends HttpClientTestCase { @@ -272,16 +271,8 @@ protected function getHttpClient(string $testCase): HttpClientInterface break; case 'testDnsError': - $mock = $this->createMock(ResponseInterface::class); - $mock->expects($this->any()) - ->method('getStatusCode') - ->willThrowException(new TransportException('DSN error')); - $mock->expects($this->any()) - ->method('getInfo') - ->willReturn([]); - - $responses[] = $mock; - $responses[] = $mock; + $responses[] = $mockResponse = new MockResponse('', ['error' => 'DNS error']); + $responses[] = $mockResponse; break; case 'testToStream': @@ -296,12 +287,7 @@ protected function getHttpClient(string $testCase): HttpClientInterface break; case 'testTimeoutOnAccess': - $mock = $this->createMock(ResponseInterface::class); - $mock->expects($this->any()) - ->method('getHeaders') - ->willThrowException(new TransportException('Timeout')); - - $responses[] = $mock; + $responses[] = new MockResponse('', ['error' => 'Timeout']); break; case 'testAcceptHeader': @@ -363,16 +349,7 @@ protected function getHttpClient(string $testCase): HttpClientInterface break; case 'testMaxDuration': - $mock = $this->createMock(ResponseInterface::class); - $mock->expects($this->any()) - ->method('getContent') - ->willReturnCallback(static function (): void { - usleep(100000); - - throw new TransportException('Max duration was reached.'); - }); - - $responses[] = $mock; + $responses[] = new MockResponse('', ['error' => 'Max duration was reached.']); break; } diff --git a/Tests/Response/MockResponseTest.php b/Tests/Response/MockResponseTest.php index def2c6f..f8c8d4c 100644 --- a/Tests/Response/MockResponseTest.php +++ b/Tests/Response/MockResponseTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\HttpClient\Exception\JsonException; +use Symfony\Component\HttpClient\Exception\TransportException; use Symfony\Component\HttpClient\Response\MockResponse; /** @@ -96,4 +97,14 @@ public function toArrayErrors() 'message' => 'JSON content was expected to decode to an array, "int" returned for "https://example.com/file.json".', ]; } + + public function testErrorIsTakenIntoAccountInInitialization() + { + $this->expectException(TransportException::class); + $this->expectExceptionMessage('ccc error'); + + MockResponse::fromRequest('GET', 'https://symfony.com', [], new MockResponse('', [ + 'error' => 'ccc error', + ]))->getStatusCode(); + } }