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

Closed
wants to merge 2 commits into 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 force-pushed the json-path-regex-snapshot branch from 39ac954 to 7057c51 Compare April 5, 2024 18:18
@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 chr-hertel force-pushed the json-path-regex-snapshot branch from 0894566 to 9827f7c Compare April 8, 2024 20:42
@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 chr-hertel force-pushed the json-path-regex-snapshot branch from 9827f7c to bd9b273 Compare April 8, 2024 20:44
@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

@spatie-bot
Copy link

Dear contributor,

because this pull request seems to be inactive for quite some time now, I've automatically closed it. If you feel this pull request deserves some attention from my human colleagues feel free to reopen it.

@spatie-bot spatie-bot closed this Aug 13, 2024
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
4 participants