Skip to content

Commit

Permalink
fix: MockResponse total_time should not be simulated when provided
Browse files Browse the repository at this point in the history
  • Loading branch information
pvgnd committed Feb 25, 2021
1 parent 0848d05 commit 67c5af7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Response/MockResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

Expand Down Expand Up @@ -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'];
}

Expand All @@ -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'];
}

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

0 comments on commit 67c5af7

Please sign in to comment.