Skip to content

Improve JSON assertion in Symfony Test #57280

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

Closed
hantsy opened this issue Jun 1, 2024 · 5 comments · Fixed by #59655
Closed

Improve JSON assertion in Symfony Test #57280

hantsy opened this issue Jun 1, 2024 · 5 comments · Fixed by #59655

Comments

@hantsy
Copy link

hantsy commented Jun 1, 2024

Description

Currently the Symfony web function tests focus on traditional web pages, today many backend application/services only provide Restful APIs, the Request/Response should improve the JSON encoding and parsing.

Check JSON path doc: https://goessner.net/articles/JsonPath/

Example

$client->request(request => 
          request->get("/posts/{id}", postId )
              ->accept(...)
              ->contentType(...)
              ->headers(headers => headers->put())
              ->jsonBody(...)
              
      )
$response->jsonPath("$.size()")
$response->jsonPath("$.foo.bar")
$response->jsonPath("$foo['bar']")
@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?
Every feature is developed by the community.
Perhaps someone would like to try?
You can read how to contribute to get started.

@carsonbot
Copy link

Could I get a reply or should I close this?

@hantsy
Copy link
Author

hantsy commented Dec 17, 2024

Any update for this issue?

@carsonbot carsonbot removed the Stalled label Dec 17, 2024
@hantsy
Copy link
Author

hantsy commented Dec 17, 2024

I do not think a bot will close it.

@stof
Copy link
Member

stof commented Dec 17, 2024

Regarding the suggested API, I don't think we should have such jsonPath method on the BrowserKit Response itself (and even less considering that BrowserKit browsers are not required to return this Response object as their public API), which would require introducing a hard dependency on some jsonPath library. Instead, it would make more sense to have a dedicated JsonCrawler (or something like that) that could be instantiated with the response body (similar to the DomCrawler we have for HTML responses).
However, the first comment of carsonbot still holds: adding such feature won't happen if nobody contributes it.

Also note that the code snippet showing the call to request is totally incompatible with the current API of the request method (and is not even valid PHP code, even though I suspect you meant to use a short closure). This part is unlikely to happen as such BC break is not worth the cost for the ecosystem. We already have a jsonRequest method to send a request with a JSON body easily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants