Skip to content
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

The response body contains object is not working as expected #133

Open
josep11 opened this issue Jul 3, 2023 · 2 comments
Open

The response body contains object is not working as expected #133

josep11 opened this issue Jul 3, 2023 · 2 comments
Assignees
Labels

Comments

@josep11
Copy link

josep11 commented Jul 3, 2023

Hi all,

I found an issue when asserting the response body for a LIST endpoint. Discovered it because the endpoint is working as expected but the behat test were not passing.

Essentially the problem is that when using the step definition "And the response body contains JSON" in conjunction with an assertion for object "@variableType(object)" it's not working because the method ApiContext::assertResponseBodyContainsJson converts everything into an associative array.

Response returned by the API:

[
    {
        "id": "evt-05820f88852740a4852ad04a49a47997",
        "calendar_id": "16881371283040",
        "custom_data": {}
    }
]

Behat test:

Scenario Outline: Event LIST: Response Structure

        When I get the event list
        Then the response code is 200
        And the response body contains JSON:
"""
    [
      {
        "id": "@variableType(string)",
        "calendar_id": "@variableType(string)",
        "custom_data": "@variableType(object)"
      }
    ]
"""

More background

Essentially the issue is with this line:

$body = json_decode((string) json_encode($this->getResponseBody()), true);

As it parses both "[]" and "{}" values into an associative array, therefore, making the assertion for variable type object not working.

@christeredvartsen
Copy link
Member

Thanks for raising this issue. I have created a test locally that recreates the issue, and I will see if I can come up with a proper solution for it.

@josep11
Copy link
Author

josep11 commented Sep 15, 2023

@christeredvartsen I believe for the solution we will need to pass along an alternative haystack variable (decoded body as a non-associative array) into the method ArrayContainsComparator::compare and then in the foreach of that method check the $value to see if it equal to "@variableType(object)". If that happens, then pick the alternative haystack.
I think that will work.

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

No branches or pull requests

2 participants