Skip to content

Commit

Permalink
Merge branch '5.4' into 6.0
Browse files Browse the repository at this point in the history
* 5.4:
  Avoid duplicated session listener registration in tests
  [HttpFoundation] fix SessionHandlerFactory using connections
  [gha] swap the php versions we use in jobs
  [DoctrineBridge] fix calling get_class on non-object
  Update PR template
  ResponseListener needs only 2 parameters
  [Lock] create lock table if it does not exist
  [HttpClient] Fix handling error info in MockResponse
  [SecurityBundle] Fix invalid reference with `always_authenticate_before_granting`
  Bump Symfony version to 5.4.1
  Update VERSION for 5.4.0
  Update CHANGELOG for 5.4.0
  • Loading branch information
nicolas-grekas committed Dec 1, 2021
2 parents 39f34cd + 78b69fc commit 6309807
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 27 deletions.
4 changes: 4 additions & 0 deletions Response/MockResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
Expand Down
31 changes: 4 additions & 27 deletions Tests/MockHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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':
Expand All @@ -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':
Expand Down Expand Up @@ -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;
}

Expand Down
11 changes: 11 additions & 0 deletions Tests/Response/MockResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 6309807

Please sign in to comment.