From 67c5af7489b3c2eea771abd973243f5c58f5fb40 Mon Sep 17 00:00:00 2001 From: Pierrick VIGNAND Date: Thu, 18 Feb 2021 14:41:11 +0100 Subject: [PATCH] fix: MockResponse total_time should not be simulated when provided --- Response/MockResponse.php | 8 ++++---- Tests/Response/MockResponseTest.php | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Response/MockResponse.php b/Response/MockResponse.php index 0f359f3..8f58032 100644 --- a/Response/MockResponse.php +++ b/Response/MockResponse.php @@ -211,8 +211,8 @@ private static function writeRequest(self $response, array $options, ResponseInt $response->info['size_upload'] = 0.0; } - // simulate "total_time" if it is set - if (isset($response->info['total_time'])) { + // simulate "total_time" if it is not set + if (!isset($response->info['total_time'])) { $response->info['total_time'] = microtime(true) - $response->info['start_time']; } @@ -260,7 +260,7 @@ private static function readResponse(self $response, array $options, ResponseInt 'http_code' => $response->info['http_code'], ] + $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']; } @@ -287,7 +287,7 @@ private static function readResponse(self $response, array $options, ResponseInt $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']; } diff --git a/Tests/Response/MockResponseTest.php b/Tests/Response/MockResponseTest.php index b4a4a22..2f389bc 100644 --- a/Tests/Response/MockResponseTest.php +++ b/Tests/Response/MockResponseTest.php @@ -11,6 +11,24 @@ */ 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() { $data = ['color' => 'orange', 'size' => 42];