Skip to content

Http::fake does not preserve trailing zeros in JSON responses #54482

Closed
@maxwellmezadre

Description

@maxwellmezadre

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

  1. 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.
  2. 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),
      ]);
  3. 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();
  4. 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 as 10, causing a mismatch.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions