Skip to content

Commit

Permalink
bug #40239 MockResponse total_time should not be simulated when provi…
Browse files Browse the repository at this point in the history
…ded (Pierrick VIGNAND)

This PR was merged into the 4.4 branch.

Discussion
----------

MockResponse total_time should not be simulated when provided

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

When you provide a `total_time` to a MockResponse, it is overriden. It should be simulated only when it is not provided I guess.
Ex: `new MockResponse('{"foo":"bar"}', ['total_time' => 0.4])`

Commits
-------

8dada95 fix: MockResponse total_time should not be simulated when provided
  • Loading branch information
nicolas-grekas committed Feb 26, 2021
2 parents 3fe1564 + 8dada95 commit a5683c5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Symfony/Component/HttpClient/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
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 a5683c5

Please sign in to comment.