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

Testing that an attribute does not exist #42

Open
denisvmedia opened this issue Jan 6, 2021 · 1 comment
Open

Testing that an attribute does not exist #42

denisvmedia opened this issue Jan 6, 2021 · 1 comment

Comments

@denisvmedia
Copy link

denisvmedia commented Jan 6, 2021

First of all, let me thank you for your library. I find it really useful in testing JSON APIs. However, I think I faced a problem that I'm not sure how to work around.

So, we have this json:

{
  "@context": "/api/contexts/Book",
  "@id": "/api/books",
  "@type": "hydra:Collection",
  "hydra:member": [
    {
      "@id": "/api/books/1",
      "@type": "Book",
      "id": 1,
      "name": "Oxford English Dictionary"
    },
    {
      "@id": "/api/books/2",
      "@type": "Book",
      "id": 2,
      "name": "The Hitchhiker's Guide to the Galaxy",
      "bookshelf": "/api/bookshelves/1"
    }
  ],
  "hydra:totalItems": 2
}

Now I want to test that the first item does not have a bookshelf element. In fact, in json it simply means null. But testing on null (as well as on empty) fails. What I tried:

  1. self::assertJsonValueEquals($content, '$["hydra:member"][0]["bookshelf"]', null);
  2. self::assertJsonValueMatches($content, '$["hydra:member"][0]["bookshelf"]', PHPUnit\Framework\Assert::isEmpty());
  3. self::assertJsonValueMatches($content, '$["hydra:member"][0]["bookshelf"]', PHPUnit\Framework\Assert::isNull());

But any of those unfortunately fail to match. Is there a way to do that? Thanks!

@denisvmedia
Copy link
Author

denisvmedia commented Jan 11, 2021

This is, by the way, a workaround:

    $bookshelfIdData = (new JSONPath(json_decode($content, true)))
        ->find('$["hydra:member"][0]["bookshelf"]')->getData();
    self::assertArrayNotHasKey(0, $bookshelfIdData);

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