Skip to content

Commit fb7c465

Browse files
authored
fix(test): canonicalizing json arrays (#6386)
1 parent 316931d commit fb7c465

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

.commitlintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"serializer",
2020
"jsonschema",
2121
"validation",
22-
"state"
22+
"state",
23+
"test"
2324
]
2425
],
2526
"scope-empty": [

src/Symfony/Bundle/Test/ApiTestAssertionsTrait.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
2222
use ApiPlatform\Symfony\Bundle\Test\Constraint\ArraySubset;
2323
use ApiPlatform\Symfony\Bundle\Test\Constraint\MatchesJsonSchema;
24+
use PHPUnit\Framework\Constraint\JsonMatches;
2425
use PHPUnit\Framework\ExpectationFailedException;
2526
use Symfony\Bundle\FrameworkBundle\Test\BrowserKitAssertionsTrait;
2627
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
@@ -71,14 +72,18 @@ public static function assertJsonContains(array|string $subset, bool $checkForOb
7172
*/
7273
public static function assertJsonEquals(array|string $json, string $message = ''): void
7374
{
74-
if (\is_string($json)) {
75-
$json = json_decode($json, true, 512, \JSON_THROW_ON_ERROR);
76-
}
77-
if (!\is_array($json)) {
78-
throw new \InvalidArgumentException('$json must be array or string (JSON array or JSON object)');
75+
if (\is_array($json)) {
76+
$json = json_encode(
77+
$json,
78+
\JSON_UNESCAPED_UNICODE
79+
| \JSON_UNESCAPED_SLASHES
80+
| \JSON_PRESERVE_ZERO_FRACTION
81+
| \JSON_THROW_ON_ERROR);
7982
}
8083

81-
static::assertEqualsCanonicalizing($json, self::getHttpResponse()->toArray(false), $message);
84+
$constraint = new JsonMatches($json);
85+
86+
static::assertThat(self::getHttpResponse()->getContent(false), $constraint, $message);
8287
}
8388

8489
/**

tests/Symfony/Bundle/Test/ApiTestCaseTest.php

-12
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,6 @@ public function testAssertJsonEqualsWithJsonObjectString(): void
9696
);
9797
}
9898

99-
public function testAssertJsonEqualsWithJsonScalarString(): void
100-
{
101-
$this->expectException(\InvalidArgumentException::class);
102-
$this->expectExceptionMessage('$json must be array or string (JSON array or JSON object)');
103-
104-
self::createClient()->request('GET', '/contexts/Address');
105-
$this->assertJsonEquals(<<<JSON
106-
"Address/name"
107-
JSON
108-
);
109-
}
110-
11199
public function testAssertMatchesJsonSchema(): void
112100
{
113101
$jsonSchema = <<<JSON

0 commit comments

Comments
 (0)