Skip to content

Commit 68466f1

Browse files
Merge branch '4.4' into 5.3
* 4.4: [gha] swap the php versions we use in jobs [DoctrineBridge] fix calling get_class on non-object Update PR template [HttpClient] Fix handling error info in MockResponse
2 parents 3f55da6 + f13c1e2 commit 68466f1

File tree

3 files changed

+19
-27
lines changed

3 files changed

+19
-27
lines changed

Response/MockResponse.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ private static function readResponse(self $response, array $options, ResponseInt
285285
'http_code' => $response->info['http_code'],
286286
] + $info + $response->info;
287287

288+
if (null !== $response->info['error']) {
289+
throw new TransportException($response->info['error']);
290+
}
291+
288292
if (!isset($response->info['total_time'])) {
289293
$response->info['total_time'] = microtime(true) - $response->info['start_time'];
290294
}

Tests/MockHttpClientTest.php

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Symfony\Component\HttpClient\Response\ResponseStream;
1919
use Symfony\Contracts\HttpClient\ChunkInterface;
2020
use Symfony\Contracts\HttpClient\HttpClientInterface;
21-
use Symfony\Contracts\HttpClient\ResponseInterface;
2221

2322
class MockHttpClientTest extends HttpClientTestCase
2423
{
@@ -272,16 +271,8 @@ protected function getHttpClient(string $testCase): HttpClientInterface
272271
break;
273272

274273
case 'testDnsError':
275-
$mock = $this->createMock(ResponseInterface::class);
276-
$mock->expects($this->any())
277-
->method('getStatusCode')
278-
->willThrowException(new TransportException('DSN error'));
279-
$mock->expects($this->any())
280-
->method('getInfo')
281-
->willReturn([]);
282-
283-
$responses[] = $mock;
284-
$responses[] = $mock;
274+
$responses[] = $mockResponse = new MockResponse('', ['error' => 'DNS error']);
275+
$responses[] = $mockResponse;
285276
break;
286277

287278
case 'testToStream':
@@ -295,12 +286,7 @@ protected function getHttpClient(string $testCase): HttpClientInterface
295286
break;
296287

297288
case 'testTimeoutOnAccess':
298-
$mock = $this->createMock(ResponseInterface::class);
299-
$mock->expects($this->any())
300-
->method('getHeaders')
301-
->willThrowException(new TransportException('Timeout'));
302-
303-
$responses[] = $mock;
289+
$responses[] = new MockResponse('', ['error' => 'Timeout']);
304290
break;
305291

306292
case 'testAcceptHeader':
@@ -362,16 +348,7 @@ protected function getHttpClient(string $testCase): HttpClientInterface
362348
break;
363349

364350
case 'testMaxDuration':
365-
$mock = $this->createMock(ResponseInterface::class);
366-
$mock->expects($this->any())
367-
->method('getContent')
368-
->willReturnCallback(static function (): void {
369-
usleep(100000);
370-
371-
throw new TransportException('Max duration was reached.');
372-
});
373-
374-
$responses[] = $mock;
351+
$responses[] = new MockResponse('', ['error' => 'Max duration was reached.']);
375352
break;
376353
}
377354

Tests/Response/MockResponseTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPUnit\Framework\TestCase;
66
use Symfony\Component\HttpClient\Exception\JsonException;
7+
use Symfony\Component\HttpClient\Exception\TransportException;
78
use Symfony\Component\HttpClient\Response\MockResponse;
89

910
/**
@@ -96,4 +97,14 @@ public function toArrayErrors()
9697
'message' => 'JSON content was expected to decode to an array, "int" returned for "https://example.com/file.json".',
9798
];
9899
}
100+
101+
public function testErrorIsTakenIntoAccountInInitialization()
102+
{
103+
$this->expectException(TransportException::class);
104+
$this->expectExceptionMessage('ccc error');
105+
106+
MockResponse::fromRequest('GET', 'https://symfony.com', [], new MockResponse('', [
107+
'error' => 'ccc error',
108+
]))->getStatusCode();
109+
}
99110
}

0 commit comments

Comments
 (0)