8000 fix: MockResponse total_time should not be simulated when provided · symfony/symfony@8dada95 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8dada95

Browse files
committed
fix: MockResponse total_time should not be simulated when provided
1 parent 87aeb8d commit 8dada95

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/Symfony/Component/HttpClient/Response/MockResponse.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ private static function writeRequest(self $response, array $options, ResponseInt
211211
$response->info['size_upload'] = 0.0;
212212
}
213213

214-
// simulate "total_time" if it is set
215-
if (isset($response->info['total_time'])) {
214+
// simulate "total_time" if it is not set
215+
if (!isset($response->info['total_time'])) {
216216
$response->info['total_time'] = microtime(true) - $response->info['start_time'];
217217
}
218218

@@ -260,7 +260,7 @@ private static function readResponse(self $response, array $options, ResponseInt
260260
'http_code' => $response->info['http_code'],
261261
] + $info + $response->info;
262262

263-
if (isset($response->info['total_time'])) {
263+
if (!isset($response->info['total_time'])) {
264264
$response->info['total_time'] = microtime(true) - $response->info['start_time'];
265265
}
266266

@@ -287,7 +287,7 @@ private static function readResponse(self $response, array $options, ResponseInt
287287
$offset = \strlen($body);
288288
}
289289

290-
if (isset($response->info['total_time'])) {
290+
if (!isset($response->info['total_time'])) {
291291
$response->info['total_time'] = microtime(true) - $response->info['start_time'];
292292
}
293293

src/Symfony/Component/HttpClient/Tests/Response/MockResponseTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@
1111
*/
1212
class MockResponseTest extends TestCase
1313
{
14+
public function testTotalTimeShouldBeSimulatedWhenNotProvided()
15+
{
16+
$response = new MockResponse('body');
17+
$response = MockResponse::fromRequest('GET', 'https://example.com/file.txt', [], $response);
18+
19+
$this->assertNotNull($response->getInfo('total_time'));
20+
$this->assertGreaterThan(0.0, $response->getInfo('total_time'));
21+
}
22+
23+
public function testTotalTimeShouldNotBeSimulatedWhenProvided()
24+
{
25+
$totalTime = 4.2;
26+
$response = new MockResponse('body', ['total_time' => $totalTime]);
27+
$response = MockResponse::fromRequest('GET', 'https://example.com/file.txt', [], $response);
28+
29+
$this->assertEquals($totalTime, $response->getInfo('total_time'));
30+
}
31+
1432
public function testToArray()
1533
{
1634
$data = ['color' => 'orange', 'size' => 42];

0 commit comments

Comments
 (0)
0