Skip to content

[13.x] Add bulk JSON path assertions to TestResponse#59829

Merged
taylorotwell merged 2 commits intolaravel:13.xfrom
cyrodjohn:cyrodjohn/bulk-json-path-assertions
Apr 23, 2026
Merged

[13.x] Add bulk JSON path assertions to TestResponse#59829
taylorotwell merged 2 commits intolaravel:13.xfrom
cyrodjohn:cyrodjohn/bulk-json-path-assertions

Conversation

@cyrodjohn
Copy link
Copy Markdown
Contributor

@cyrodjohn cyrodjohn commented Apr 23, 2026

What & Why

When writing HTTP tests that assert multiple JSON values, developers currently have to chain individual assertJsonPath() calls. This becomes verbose and repetitive. This PR adds assertJsonPaths() and assertJsonMissingPaths() to Illuminate\Testing\TestResponse as bulk alternatives that accept arrays of paths and expected values.

Before:

$response->assertJsonPath(data.id, 1)
         ->assertJsonPath(data.name, Taylor)
         ->assertJsonPath(meta.count, 3);

After:

$response->assertJsonPaths([
    data.id => 1,
    data.name => Taylor,
    meta.count => 3,
]);

API

assertJsonPaths(array $paths)

Accepts an associative array mapping JSON paths to expected values. Supports closures for flexible assertions (inherited from assertJsonPath()). Returns $this for fluent chaining.

$response->assertJsonPaths([
    user.name => Taylor,
    user.admin => fn ($admin) => $admin === true,
]);

assertJsonMissingPaths($paths)

Accepts a single path string or an array of path strings (via Arr::wrap()). Asserts each path is absent from the JSON response. Returns $this for fluent chaining.

// Array of paths
$response->assertJsonMissingPaths([password, secret_token]);

// Single path (also works)
$response->assertJsonMissingPaths(internal_note);

Implementation Notes

Both methods are intentionally thin wrappers over the existing assertJsonPath() and assertJsonMissingPath() methods. They fail fast on the first bad path and explicitly guard against empty input:

  • assertJsonPaths([]) fails with: "No JSON paths were provided."
  • assertJsonMissingPaths([]) fails with: "No JSON missing paths were provided."

Test Coverage

Added to tests/Testing/TestResponseTest.php:

For assertJsonPaths:

  • testAssertJsonPaths — success case with mixed scalar and closure values
  • testAssertJsonPathsCanFail — failure case demonstrating fail-fast behavior
  • testAssertJsonPathsCanFailWhenPathsAreEmpty — empty input guard

For assertJsonMissingPaths:

  • testAssertJsonMissingPaths — success case with string and array input
  • testAssertJsonMissingPathsCanFail — failure case demonstrating fail-fast behavior
  • testAssertJsonMissingPathsCanFailWhenPathsAreEmpty — empty input guard

Test Plan

  • ./vendor/bin/phpunit tests/Testing/TestResponseTest.php
  • ./vendor/bin/pint --dirty

@taylorotwell taylorotwell merged commit 912d9ee into laravel:13.x Apr 23, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants