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(testing): Improve ApiTestAssertionsTrait::assertJsonEquals #5752

Conversation

DaedalusDev
Copy link

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

Improve ApiTestAssertionsTrait::assertJsonEquals by replacing assertEqualsCanonicalizing with assertEquals.

@DaedalusDev DaedalusDev changed the title api-platform/core#5751 - Improve ApiTestAssertionsTrait::assertJsonEquals feat(testing): Improve ApiTestAssertionsTrait::assertJsonEquals Aug 16, 2023
@DaedalusDev DaedalusDev force-pushed the api-platform/core#5751-Improve-ApiTestAssertionsTraitassertJsonEquals branch 2 times, most recently from 7f2e3a3 to 54d1eeb Compare August 16, 2023 15:55
@DaedalusDev DaedalusDev force-pushed the api-platform/core#5751-Improve-ApiTestAssertionsTraitassertJsonEquals branch from 54d1eeb to d7abd0b Compare August 16, 2023 15:57
@@ -74,7 +74,7 @@ public static function assertJsonEquals(array|string $json, string $message = ''
throw new \InvalidArgumentException('$json must be array or string (JSON array or JSON object)');
}

static::assertEqualsCanonicalizing($json, self::getHttpResponse()->toArray(false), $message);
Copy link
Member

@soyuka soyuka Aug 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

order of arrays is not guaranteed I think that this is needed and don't get why it wouldn't work

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<?php

namespace App\Tests\Api;

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;

/**
 * @internal
 *
 * @coversNothing
 */
class CanolicalizingTest extends ApiTestCase
{
    public function testCanonicalizing(): void
    {
        static::assertEqualsCanonicalizing(
            [
                [
                    'id' => 1,
                ],
                [
                    'id' => 2,
                ],
            ],
            [
                [
                    'id' => 2,
                ],
                [
                    'id' => 1,
                ],
            ],
            'assertEqualsCanonicalizing :-(',
        );
        static::assertEqualsCanonicalizing(
            [
                'foo' => 5,
                'bar' => 10,
            ],
            [
                'bar' => 5,
                'foo' => 10,
            ],
            'assertEqualsCanonicalizing :-(',
        );
    }

    public function testWithoutCanonicalizing(): void
    {
        static::assertEquals(
            [
                [
                    'id' => 1,
                ],
                [
                    'id' => 2,
                ],
            ],
            [
                [
                    'id' => 2,
                ],
                [
                    'id' => 1,
                ],
            ],
            'assertEquals :-(',
        );
        static::assertEquals(
            [
                'foo' => 5,
                'bar' => 10,
            ],
            [
                'bar' => 5,
                'foo' => 10,
            ],
            'assertEquals :-(',
        );
    }
}

Result :
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mhh I see, would it be possible to sort the array so that the comparison works nonetheless? or do you think it's not needed (We had the problem in the past)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to check that comparing ['a', 'b', 'c'] with ['b', 'c', 'a'] fails because while JSON objects are unordered, JSON arrays are.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test will fail. I have mention an exemple in the related issued, with a result and comparison result : #5751 (comment)

<?php

namespace App\Tests\Api;

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\Tests\ApiTestCaseTrait;
use JsonException;

use const JSON_THROW_ON_ERROR;

/**
 * @internal
 *
 * @coversNothing
 */
class CanolicalizingTest extends ApiTestCase
{
    use ApiTestCaseTrait;

    /**
     * @throws JsonException
     */
    public function testJson(): void
    {
        $json = json_decode('["c", "b", "a"]', true, 512, JSON_THROW_ON_ERROR);
        static::assertEqualsCanonicalizing(
            ['a', 'b', 'c'],
            $json,
            'Canonicalizing :-(',
        );
        static::assertEquals(
            ['a', 'b', 'c'],
            $json,
            'Assert equals',
        );
    }
}

image

image

Here, we facing a false positive test because assert with assertEqualsCanonicalizing won't throw an error...
Best regards.

Copy link

stale bot commented Nov 13, 2023

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 stale bot added the stale label Nov 13, 2023
@soyuka soyuka added the bug label Nov 14, 2023
@stale stale bot removed the stale label Nov 14, 2023
@bendavies
Copy link
Contributor

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.

@soyuka
Copy link
Member

soyuka commented May 22, 2024

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

@bendavies
Copy link
Contributor

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
Copy link
Contributor

bendavies commented May 22, 2024

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);
    }

@soyuka
Copy link
Member

soyuka commented May 24, 2024

thanks @DaedalusDev @bendavies !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve ApiTestAssertionsTrait::assertJsonEquals with assertEquals insteads of assertEqualsCanonicalizing
4 participants