imbo / behat-api-extension

API extension for Behat, used to ease testing of JSON-based APIs
MIT License
109 stars 42 forks source link

JSON Arrays are not considered as valid JSON #34

Closed Alex-D closed 7 years ago

Alex-D commented 7 years ago

Supposed I have that in my Scenario:

And the response body contains:
      """
      [
        {
          "id": 1,
          "gameId": 1
        },
        {
          "id": 2,
          "gameId": 2
        }
      ]
      """

The Assertion that check it's a stdClass failed, because it's an array of stdClass (ApiContext line 547):

Assertion::isInstanceOf(
    $contains,
    'stdClass',
    'The supplied parameter is not a valid JSON object.'
);

If I remove this check, it works like a charm.

Maybe it would better to use the Guzzle's json_decode?

\GuzzleHttp\json_encode($contains);

which throw a catchable InvalidArgumentExcepotion.

Are you OK with that?

Thanks!

christeredvartsen commented 7 years ago

The Then the response body contains: <PyStringNode> step is a step specifically made for checking JSON objects, but by using a special syntax you can check arrays entries as well:

And the response body contains:
    """
    {
        "[0]": {
            "id": 1,
            "gameId": 1
        },
        "[1]": {
            "id": 2,
            "gameId": 2
        }
    }
    """

I just saw your PR now and will have a deep look at it and see if there are any other implications. I will get back to this later today.

Alex-D commented 7 years ago

Yep, but it's not user-friendly to write and to read :/

It works well with my little modification :)

christeredvartsen commented 7 years ago

Resolved by ba9cc350aee0aa4e0ebb29980d34e6eae90e796a.