phpunit-snapshot-assertions icon indicating copy to clipboard operation
phpunit-snapshot-assertions copied to clipboard

feat: Driver with JSONPath support

Open chr-hertel opened this issue 1 year ago • 6 comments

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 avatar Apr 02 '24 21:04 chr-hertel

Great proposal. Thanks.

4lxndr avatar Apr 07 '24 17:04 4lxndr

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

freekmurze avatar Apr 08 '24 07:04 freekmurze

oh snap, yes, i will

seg fault 👀 😆

chr-hertel avatar Apr 08 '24 07:04 chr-hertel

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 avatar Apr 08 '24 20:04 chr-hertel

@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: https://github.com/chr-hertel/json-assert-test/pull/2

chr-hertel avatar Apr 10 '24 19:04 chr-hertel

Found another issue while testing - need to extend this further

chr-hertel avatar Apr 12 '24 12:04 chr-hertel

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 avatar Aug 13 '24 10:08 spatie-bot