Closed
Description
Laravel Version
11.39.1
PHP Version
8.4
Database Driver & Version
SQLite for macOS 15.3 on arm64 (Homebrew)
Description
When using Http::fake() in tests with fixture responses containing decimal numbers formatted with trailing zeros (e.g., 10.0), the numeric values are decoded without the trailing zero (resulting in 10). This causes tests that rely on exact formatting to fail. Although this behavior is a consequence of PHP’s json_decode() (which converts numbers to their numeric representation), it negatively impacts tests that expect the exact string representation as defined in the fixture.
Steps To Reproduce
-
Create a JSON Fixture:
- Create a file at
tests/fixtures/sample.json
with the following content:{ "value": 10.0, "other": "data" }
- Note: The fixture contains
10.0
with a trailing zero.
- Create a file at
-
Set Up the Test with
Http::fake()
:- In your test file, add the following code to load the fixture and set up the fake response:
use Illuminate\Support\Facades\Http; // Load the fixture file $fixture = json_decode(file_get_contents(base_path('tests/fixtures/sample.json')), true); // Fake the HTTP response Http::fake([ 'api.example.com/*' => Http::response($fixture), ]);
- In your test file, add the following code to load the fixture and set up the fake response:
-
Trigger the HTTP Request:
- Make the HTTP request that will be intercepted by
Http::fake()
:$response = Http::get('api.example.com/data'); $data = $response->json();
- Make the HTTP request that will be intercepted by
-
Compare the Returned Data:
- Write an assertion comparing the returned data with the fixture:
expect($data)->toBe($fixture);
- The test will fail because the numeric value
10.0
from the fixture is decoded as10
, causing a mismatch.
- Write an assertion comparing the returned data with the fixture: