Skip to content

Testing that an attribute does not exist #42

Open
@denisvmedia

Description

@denisvmedia

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions