Merge branch '4.4' into 5.2

* 4.4:
  improve exception message if symfony/security-csrf is missing
  fix: MockResponse total_time should not be simulated when provided
This commit is contained in:
Alexander M. Turek 2021-03-01 01:40:14 +01:00
commit 82e3b17cf4
3 changed files with 23 additions and 5 deletions

View File

@ -59,7 +59,7 @@ class FormRenderer implements FormRendererInterface
public function renderCsrfToken(string $tokenId) public function renderCsrfToken(string $tokenId)
{ {
if (null === $this->csrfTokenManager) { if (null === $this->csrfTokenManager) {
throw new BadMethodCallException('CSRF tokens can only be generated if a CsrfTokenManagerInterface is injected in FormRenderer::__construct().'); throw new BadMethodCallException('CSRF tokens can only be generated if a CsrfTokenManagerInterface is injected in FormRenderer::__construct(). Try running "composer require symfony/security-csrf".');
} }
return $this->csrfTokenManager->getToken($tokenId)->getValue(); return $this->csrfTokenManager->getToken($tokenId)->getValue();

View File

@ -232,8 +232,8 @@ class MockResponse implements ResponseInterface, StreamableInterface
$response->info['size_upload'] = 0.0; $response->info['size_upload'] = 0.0;
} }
// simulate "total_time" if it is set // simulate "total_time" if it is not set
if (isset($response->info['total_time'])) { if (!isset($response->info['total_time'])) {
$response->info['total_time'] = microtime(true) - $response->info['start_time']; $response->info['total_time'] = microtime(true) - $response->info['start_time'];
} }
@ -281,7 +281,7 @@ class MockResponse implements ResponseInterface, StreamableInterface
'http_code' => $response->info['http_code'], 'http_code' => $response->info['http_code'],
] + $info + $response->info; ] + $info + $response->info;
if (isset($response->info['total_time'])) { if (!isset($response->info['total_time'])) {
$response->info['total_time'] = microtime(true) - $response->info['start_time']; $response->info['total_time'] = microtime(true) - $response->info['start_time'];
} }
@ -308,7 +308,7 @@ class MockResponse implements ResponseInterface, StreamableInterface
$offset = \strlen($body); $offset = \strlen($body);
} }
if (isset($response->info['total_time'])) { if (!isset($response->info['total_time'])) {
$response->info['total_time'] = microtime(true) - $response->info['start_time']; $response->info['total_time'] = microtime(true) - $response->info['start_time'];
} }

View File

@ -11,6 +11,24 @@ use Symfony\Component\HttpClient\Response\MockResponse;
*/ */
class MockResponseTest extends TestCase class MockResponseTest extends TestCase
{ {
public function testTotalTimeShouldBeSimulatedWhenNotProvided()
{
$response = new MockResponse('body');
$response = MockResponse::fromRequest('GET', 'https://example.com/file.txt', [], $response);
$this->assertNotNull($response->getInfo('total_time'));
$this->assertGreaterThan(0.0, $response->getInfo('total_time'));
}
public function testTotalTimeShouldNotBeSimulatedWhenProvided()
{
$totalTime = 4.2;
$response = new MockResponse('body', ['total_time' => $totalTime]);
$response = MockResponse::fromRequest('GET', 'https://example.com/file.txt', [], $response);
$this->assertEquals($totalTime, $response->getInfo('total_time'));
}
public function testToArray() public function testToArray()
{ {
$data = ['color' => 'orange', 'size' => 42]; $data = ['color' => 'orange', 'size' => 42];