phpunit-snapshot-assertions
phpunit-snapshot-assertions copied to clipboard
feat: Driver with JSONPath support
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
JsonDriverwith an optional argument instead of a separate driver
Examples
Example 1: Simple Document
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
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+@',
])
Great proposal. Thanks.
The tests seem to fail, could you take a look at that?
oh snap, yes, i will
seg fault 👀 😆
that's pretty weird ... running on ubuntu php8.3 with the same command:
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.
@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
Found another issue while testing - need to extend this further
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.