Skip to content

Commit

Permalink
[HttpClient] Preserve float in JsonMockResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
Jibbarth committed Mar 4, 2024
1 parent a184a18 commit 4d120b8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class JsonMockResponse extends MockResponse
public function __construct(mixed $body = [], array $info = [])
{
try {
$json = json_encode($body, \JSON_THROW_ON_ERROR);
$json = json_encode($body, \JSON_THROW_ON_ERROR | \JSON_PRESERVE_ZERO_FRACTION);
} catch (\JsonException $e) {
throw new InvalidArgumentException('JSON encoding failed: '.$e->getMessage(), $e->getCode(), $e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ public function testJsonEncodeString()
$this->assertSame('application/json', $response->getHeaders()['content-type'][0]);
}

public function testJsonEncodeFloat()
{
$client = new MockHttpClient(new JsonMockResponse([
'foo' => 1.23,
'ccc' => 1.0,
'baz' => 10.,
]));
$response = $client->request('GET', 'https://symfony.com');

$this->assertSame([
'foo' => 1.23,
'ccc' => 1.,
'baz' => 10.,
], $response->toArray());
}

/**
* @dataProvider responseHeadersProvider
*/
Expand Down

0 comments on commit 4d120b8

Please sign in to comment.