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

assertJsonStructure breaks comparing structure with itself #28591

Closed
DerManoMann opened this issue May 23, 2019 · 8 comments
Closed

assertJsonStructure breaks comparing structure with itself #28591

DerManoMann opened this issue May 23, 2019 · 8 comments

Comments

@DerManoMann
Copy link

  • Laravel Version: 5.8.18
  • PHP Version: 7.3.5

Description:

Comparing a data structure with itself beaks with message Failed asserting that an array has the key 1.
Using assertExactJson does work, though.

Steps To Reproduce:

        $data = ['data' => ['user_id' => 1, 'role' => 'admin']];
        (new TestResponse(new Response()))->assertJsonStructure($data, $data);
@driesvints
Copy link
Member

Why would you want to do this? Can you provide a full test for this?

@DerManoMann
Copy link
Author

Well, this is just a test case to demonstrate the problem.
In my test class it would look like this:

$response->assertJsonStructure(['data' => ['user_id' => 1, 'role' => 'admin']]);

@DerManoMann
Copy link
Author

I would expect the assert to pass if the structure matches, even if the id and role values are not the same.

@driesvints
Copy link
Member

Hmm, I think you're maybe right. It seems to me that the $value on this line actually needs to be $key:

PHPUnit::assertArrayHasKey($value, $responseData);

But this has been around for two years already and no-one ever reported problems with this so I'm not really sure if it's broken or not: #17700

Maybe if I can ping the original author (@cretueusebiu) he can explain why this is implemented as $value and not $key.

@driesvints
Copy link
Member

There are also no tests for the second $responseData param so it's hard to check what the actual expected behavior is.

@cretueusebiu
Copy link
Contributor

At that time I just ported the method from the browser-kit-testing repo.

@driesvints
Copy link
Member

I finally get what's happening. The thing is that the $structure argument only needs the structure and not the any data. So it's not a one-on-one match with the actual response.

@DerManoMann what you need to do is this:

$response->assertJsonStructure(['data' => ['user_id', 'role']]);

The docs could use an example. I'll try to send in a PR for this but feel free to beat me to it.

@DerManoMann
Copy link
Author

I thought it might be something like that, although I am not convinced this is a good solution. At the minimum it is not intuitive at all.

The thing is, the method makes me compare an array (or object) with string keys (properties) with an array that has those as values with numeric keys.
To me that not really comparing two structures, but checking one structure against nested meta data describing the structure.

Personally, I'd be much happier wtith something that really compares two structures while ignoring all values except for array/object.

Anyway, thanks for looking into this, I do appreciate your time.

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

No branches or pull requests

3 participants