Skip to content

[JsonPath] Add JsonPathAssertionsTrait and related constraints #60105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
from

Conversation

alexandre-daubois
Copy link
Member

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues -
License MIT

Let's leverage JsonPath in test cases. I propose to add a new trait to ease testing JSON strings against JsonPath:

use PHPUnit\Framework\TestCase;
use Symfony\Component\JsonPath\Test\JsonPathAssertionsTrait;

class MyApiTest extends TestCase
{
    use JsonPathAssertionsTrait;

    public function testItFetchesAllUsers(): void
    {
        $json = self::fetchUserCollection();

        $this->assertJsonPathCount(3, '$.users[*]', $json);
        $this->assertJsonPathSame(['Melchior'], '$.users[0].username', $json);
        $this->assertJsonPathEquals(['30'], '$.users[0].age', $json, 'should return the age as string or int');
    }

    public function testItFetchesOneUser(): void
    {
        $json = self::fetchOneUser();

        $this->assertJsonPathSame(['Melchior'], '$.user.username', $json);
        $this->assertJsonPathEquals(['30'], '$.user.age', $json, 'should return the age as string or int');
    }

    /**
     * Hard-coded for the example, but in a real integration test, you would actually make the API call!
     */
    private static function fetchUserCollection(): string
    {
        // Simulate fetching JSON data from an API
        return <<<JSON
        {
            "users": [
                {
                    "username": "Melchior",
                    "age": 30
                },
                {
                    "username": "Gaspard",
                    "age": 50
                },
                {
                    "username": "Balthazar",
                    "age": 55
                }
            ]
        }
        JSON;
    }

    private static function fetchOneUser(): string
    {
        // Simulate fetching JSON data from an API
        return <<<JSON
        {
            "user": {
                "username": "Melchior",
                "age": 30
            }
        }
        JSON;
    }
}

@carsonbot carsonbot added this to the 7.3 milestone Apr 1, 2025
@alexandre-daubois alexandre-daubois force-pushed the jsonpath-assertions branch 3 times, most recently from 4325a17 to 59965aa Compare April 1, 2025 09:21
@alexandre-daubois
Copy link
Member Author

alexandre-daubois commented Apr 1, 2025

@stof All your comments are addressed, I pushed a new version with "standalone" constraints as done in BrowserKit.

@alexandre-daubois alexandre-daubois changed the title [JsonPath] Add JsonPathAssertionsTrait [JsonPath] Add JsonPathAssertionsTrait and related constraints Apr 1, 2025
@alexandre-daubois alexandre-daubois changed the title [JsonPath] Add JsonPathAssertionsTrait and related constraints [JsonPath][DX] Add JsonPathAssertionsTrait and related constraints Apr 9, 2025
@alexandre-daubois alexandre-daubois added the DX DX = Developer eXperience (anything that improves the experience of using Symfony) label Apr 9, 2025
@carsonbot carsonbot changed the title [JsonPath][DX] Add JsonPathAssertionsTrait and related constraints Add JsonPathAssertionsTrait and related constraints Apr 9, 2025
GromNaN added a commit that referenced this pull request Apr 10, 2025
… (alexandre-daubois)

This PR was merged into the 7.3 branch.

Discussion
----------

[JsonPath] Add two utils methods to `JsonPath` builder

| Q             | A
| ------------- | ---
| Branch?       | 7.3
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Issues        | -
| License       | MIT

Small DX improvements that goes with #60105 and #60083.

This PR adds two new methods, `first()` and `last()`, added to JsonPath builder. This voluntary reminds methods from the DomCrawler component. The goal is not to add every possible method, but I think `first()` and `last()` are common enough to be added.

I also propose to rename `anyIndex()` to `all()`.

```php
$path = new JsonPath();

// Get the first user of the collection
$path = $path->key('users')->first();
```

```php
$path = new JsonPath();

// Get the last user of the collection
$path = $path->key('users')->last();
```

```php
$path = new JsonPath();

// Get all users of the collection
$path = $path->key('users')->all();
```

Commits
-------

3bc3559 [JsonPath][DX] Add utils methods to `JsonPath` builder
@carsonbot carsonbot changed the title Add JsonPathAssertionsTrait and related constraints [JsonPath] Add JsonPathAssertionsTrait and related constraints Apr 16, 2025
Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API Platform has a useful assertJsonContains method that matches an array subset.

You can add assertJsonPathSubset

@alexandre-daubois
Copy link
Member Author

@GromNaN indeed, good idea of feature! I'll have a look at it a bit later when I have a some time to dig the subject on why Sebastian deprecated the feature in PHPUnit first 🙂 Thanks for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DX DX = Developer eXperience (anything that improves the experience of using Symfony) Feature JsonPath Status: Reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants