Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[JsonPath] AddJsonPathAssertionsTrait 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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:7.3fromalexandre-daubois:jsonpath-assertions
May 19, 2025

Conversation

alexandre-daubois
Copy link
Member

QA
Branch?7.3
Bug fix?no
New feature?yes
Deprecations?no
Issues-
LicenseMIT

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

usePHPUnit\Framework\TestCase;useSymfony\Component\JsonPath\Test\JsonPathAssertionsTrait;class MyApiTestextends TestCase{use JsonPathAssertionsTrait;publicfunctiontestItFetchesAllUsers():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');    }publicfunctiontestItFetchesOneUser():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!     */privatestaticfunctionfetchUserCollection():string    {// Simulate fetching JSON data from an APIreturn<<<JSON        {            "users": [                {                    "username": "Melchior",                    "age": 30                },                {                    "username": "Gaspard",                    "age": 50                },                {                    "username": "Balthazar",                    "age": 55                }            ]        }        JSON;    }privatestaticfunctionfetchOneUser():string    {// Simulate fetching JSON data from an APIreturn<<<JSON        {            "user": {                "username": "Melchior",                "age": 30            }        }        JSON;    }}

matyo91 reacted with rocket emoji
@carsonbotcarsonbot added this to the7.3 milestoneApr 1, 2025
@alexandre-dauboisalexandre-dauboisforce-pushed thejsonpath-assertions branch 3 times, most recently from4325a17 to59965aaCompareApril 1, 2025 09:21
@alexandre-daubois
Copy link
MemberAuthor

alexandre-daubois commentedApr 1, 2025
edited
Loading

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

smnandre reacted with thumbs up emoji

@alexandre-dauboisalexandre-daubois changed the title[JsonPath] AddJsonPathAssertionsTrait[JsonPath] AddJsonPathAssertionsTrait and related constraintsApr 1, 2025
@alexandre-dauboisalexandre-daubois changed the title[JsonPath] AddJsonPathAssertionsTrait and related constraints[JsonPath][DX] AddJsonPathAssertionsTrait and related constraintsApr 9, 2025
@alexandre-dauboisalexandre-daubois added the DXDX = Developer eXperience (anything that improves the experience of using Symfony) labelApr 9, 2025
@carsonbotcarsonbot changed the title[JsonPath][DX] AddJsonPathAssertionsTrait and related constraintsAddJsonPathAssertionsTrait and related constraintsApr 9, 2025
GromNaN added a commit that referenced this pull requestApr 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       | MITSmall 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
@carsonbotcarsonbot changed the titleAddJsonPathAssertionsTrait and related constraints[JsonPath] AddJsonPathAssertionsTrait and related constraintsApr 16, 2025
Copy link
Member

@GromNaNGromNaN 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 usefulassertJsonContains method that matches an array subset.

You can addassertJsonPathSubset

@alexandre-daubois
Copy link
MemberAuthor

@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!

@nicolas-grekasnicolas-grekas added the ❄️ Feature FreezeImportant Pull Requests to finish before the next Symfony "feature freeze" labelMay 19, 2025
@fabpot
Copy link
Member

Thank you@alexandre-daubois.

@fabpotfabpot merged commit422893b intosymfony:7.3May 19, 2025
9 of 11 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@GromNaNGromNaNGromNaN approved these changes

@stofstofstof left review comments

@ging-devging-devging-dev left review comments

@fabpotfabpotfabpot approved these changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@OskarStarkOskarStarkOskarStark approved these changes

@chalasrchalasrchalasr approved these changes

Assignees
No one assigned
Labels
DXDX = Developer eXperience (anything that improves the experience of using Symfony)FeatureJsonPath❄️ Feature FreezeImportant Pull Requests to finish before the next Symfony "feature freeze"Status: Reviewed
Projects
None yet
Milestone
7.3
Development

Successfully merging this pull request may close these issues.

9 participants
@alexandre-daubois@fabpot@nicolas-grekas@GromNaN@stof@OskarStark@chalasr@ging-dev@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp