browser: Json::assertHas doesn't work as expected

Hi,

I just updated the library in my project to latest version (1.2) and replaced some places to use KernelBrowser::browser()->json()->assertHas('selector'). But it seems that it doesn’t work as expected.

I’m testing a json api endpoint which returns this json:

{
  "limit": 50,
  "total": 1,
  "page": 1,
  "pages", 1,
  "_links": {
    "first": {
      "href": "/api/backend/chains?_page=1&_per_page=50"
    },
    "last": {
      "href": "/api/backend/chains?_page=1&_per_page=50"
    }
  }
  "_embedded": {
    "items": [
         {"id": 1, "label": "Grand compte par défaut"}
    ]
  }
}

Here is my test which fails:

$this->browser()->get('/uri')->assertStatus(Response::RESPONSE_OK)
    ->json()->assertHas('total');

Here is my test which succeeds:

$response = $this->browser()->get('/uri')->assertStatus(Response::RESPONSE_OK)
    ->json()->decoded();
self::assertSame(1, $response['total']);

I tried to replace the Assert::try in Json::assertHas by Assert::that($selector)->isNotEmpty() and it works. I think the problem comes from length used which in this case returns something empty as 1 is not countable… I don’t know if my solution is the best to use. If so, let me know I can provide the PR. 😃

Thank you

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 23 (23 by maintainers)

Most upvoted comments

I tried to do some things base on some explode which didn’t work and produce an ugly code. ^^

I am not sure how it could be done differently or how to combine the different Assert possibilities.

And my other bug which may require me to update all my tests is painful.

Leave my pr open, I’ll try to search a bit more on monday. If you find a solution during the weekend you can close my pr and create your own or provide the diff and I will be able to apply in my branch. I am already happy to be able to help a bit here anyway. 🙂

Thank you.

Hi, I just pushed the changes.

yes it works!

you got a playground for jmespath here: https://jmespath.org/