core icon indicating copy to clipboard operation
core copied to clipboard

feat(testing): Improve ApiTestAssertionsTrait::assertJsonEquals

Open DaedalusDev opened this issue 1 year ago • 5 comments

Q A
Branch main
Tickets Closes #5751, closes #5751
License MIT

Improve ApiTestAssertionsTrait::assertJsonEquals by replacing assertEqualsCanonicalizing with assertEquals.

DaedalusDev avatar Aug 16 '23 15:08 DaedalusDev

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 13 '23 20:11 stale[bot]

just came across this. It is madness that assertJsonEquals currently ignores keys. I've found a few ignored errors in my testsuite down to this.

We should look to get this merged.

bendavies avatar May 22 '24 10:05 bendavies

Should we target another branch then main though? @dunglas any opinion?

soyuka avatar May 22 '24 16:05 soyuka

it seems we should be using https://github.com/sebastianbergmann/phpunit/blob/main/src/Framework/Constraint/JsonMatches.php in some way which does the canonicalizing

bendavies avatar May 22 '24 16:05 bendavies

this seems to do the trick

    public static function assertJsonEquals(array|string $json, string $message = ''): void
    {
        if (\is_array($json)) {
            $json = json_encode(
                $json,
                JSON_UNESCAPED_UNICODE
                | JSON_UNESCAPED_SLASHES
                | JSON_PRESERVE_ZERO_FRACTION
                | JSON_THROW_ON_ERROR);
        }

        $constraint = new JsonMatches($json);

        static::assertThat(self::getHttpResponse()->getContent(false), $constraint, $message);
    }

bendavies avatar May 22 '24 17:05 bendavies

thanks @DaedalusDev @bendavies !

soyuka avatar May 24 '24 16:05 soyuka