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

JsonContains mismatches nested sequential array #61

Open
jumale opened this issue Feb 25, 2019 · 1 comment
Open

JsonContains mismatches nested sequential array #61

jumale opened this issue Feb 25, 2019 · 1 comment

Comments

@jumale
Copy link

jumale commented Feb 25, 2019

Hi,
I'm not sure if it's a bug or expected behaviour.

This example matches correctly:

<?php
$actualJson = [
    'foo' => [
        'bar' => ['cat', 'dog']
    ]
];

$expectedJson = ['cat', 'dog'];

(new JsonContains($expectedJson))->evaluate(json_encode($actualJson));
// success

While this example fails - it can not match a sequential array when it's inside of another sequential array:

<?php
$actualJson = [
    'foo' => [
        ['cat', 'dog']
    ]
];

$expectedJson = ['cat', 'dog'];

(new JsonContains($expectedJson))->evaluate(json_encode($actualJson));
// failed

There is a workaround to wrap the expected value in an array one more time, but still looks weird that it does not work in the example above.

<?php
$actualJson = [
    'foo' => [
        ['some noise'],
        ['cat', 'dog'],
        ['some noise'],
    ]
];

$expectedJson = [['cat', 'dog']];

(new JsonContains($expectedJson))->evaluate(json_encode($actualJson));
// success
@jumale
Copy link
Author

jumale commented Feb 25, 2019

It seems you need to wrap it as many times as amount of sequential arrays on the way to the matched value.
In the next example content is a collection of objects and rows is a collection of arrays. And you need to wrap the expected result twice, only then it matches:

<?php
$actualJson = [
    'content' => [
        [
            'rows' => [
                ['cat', 'dog']
            ]
        ]
    ]
];

$expectedJson = [[['cat', 'dog']]];

(new JsonContains($expectedJson))->evaluate(json_encode($actualJson));

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

No branches or pull requests

1 participant