-
-
Notifications
You must be signed in to change notification settings - Fork 839
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
feat(testing): Improve ApiTestAssertionsTrait::assertJsonEquals #5752
Conversation
7f2e3a3
to
54d1eeb
Compare
54d1eeb
to
d7abd0b
Compare
@@ -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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :-(',
);
}
}
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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',
);
}
}
Here, we facing a false positive test because assert with assertEqualsCanonicalizing
won't throw an error...
Best regards.
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. |
just came across this. We should look to get this merged. |
Should we target another branch then main though? @dunglas any opinion? |
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 |
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);
} |
thanks @DaedalusDev @bendavies ! |
Improve ApiTestAssertionsTrait::assertJsonEquals by replacing
assertEqualsCanonicalizing
withassertEquals
.