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

feat: Driver with JSONPath support #191

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

chr-hertel
Copy link

@chr-hertel chr-hertel commented Apr 2, 2024

Implementing JsonPathDriver - resolves #190

With this feature it is possible to have changing values in a JSON snapshot and add regex assertions on those values.

Implementation

  • the implementation needs galbar/jsonpath, which has Apache-2.0 license, but is only suggested and therefore optional.
  • could be part of the JsonDriver with an optional argument instead of a separate driver

Examples

Example 1: Simple Document

image

The corresponding test code would look like this:

self::assertMatchesJsonPathSnapshot($json, [
    '$.id' => '@\d+@',
    '$.cover' => '@https://bucket.foo/bar/\d+.[webp|jpg]@',
    '$.createdAt' => '@\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[\+\-]\d{2}:\d{2}@',
])

Example 2: json:api

image

The corresponding test code would look like this:

self::assertMatchesJsonPathSnapshot($json, [
    '$.data..id' => '@\d+@',
    '$.data..links.*' => '@http://example.com/articles/\d+(/[a-z/]+)?@',
    '$.included..id' => '@\d+@',
    '$.included..links.self' => '@http://example.com/(people|comments)/\d+@',
])

@chr-hertel chr-hertel marked this pull request as ready for review April 5, 2024 18:27
@4lxndr
Copy link

4lxndr commented Apr 7, 2024

Great proposal. Thanks.

@freekmurze
Copy link
Member

The tests seem to fail, could you take a look at that?

@chr-hertel
Copy link
Author

chr-hertel commented Apr 8, 2024

oh snap, yes, i will

seg fault 👀 😆

@chr-hertel
Copy link
Author

chr-hertel commented Apr 8, 2024

that's pretty weird ... running on ubuntu php8.3 with the same command:

image

doesn't look like this thing is flaky, so it's most likely my change ... can't wrap my head around it - need to give it another thought later.
added some docs though & did a rebase. was easier, ha.

@chr-hertel
Copy link
Author

@freekmurze would you mind to just approve another run for the pipeline?

i set up a seperate repo with the same pipeline and all phpunit jobs passed:
Actions: https://github.com/chr-hertel/json-assert-test/actions/runs/8636697242
PR: chr-hertel/json-assert-test#2

@chr-hertel
Copy link
Author

Found another issue while testing - need to extend this further

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

Successfully merging this pull request may close these issues.

Allow passing variables to Snapshot to enable dynamic responses
3 participants