From 93e964a66827665620cc3ab1b02f6b44feddd81b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Tue, 29 Dec 2020 08:26:57 +0100 Subject: [PATCH] Enhancement: Extract assertion that normalizes JSON before comparing --- test/Unit/AbstractNormalizerTestCase.php | 18 ++++++++++++++++++ .../Composer/AbstractComposerTestCase.php | 2 +- .../Unit/Vendor/Composer/BinNormalizerTest.php | 6 +++--- .../Composer/ComposerJsonNormalizerTest.php | 2 +- .../Composer/ConfigHashNormalizerTest.php | 6 +++--- .../Composer/PackageHashNormalizerTest.php | 6 +++--- .../VersionConstraintNormalizerTest.php | 4 ++-- 7 files changed, 31 insertions(+), 13 deletions(-) diff --git a/test/Unit/AbstractNormalizerTestCase.php b/test/Unit/AbstractNormalizerTestCase.php index f29fd5e4..451556bd 100644 --- a/test/Unit/AbstractNormalizerTestCase.php +++ b/test/Unit/AbstractNormalizerTestCase.php @@ -14,6 +14,7 @@ namespace Ergebnis\Json\Normalizer\Test\Unit; use Ergebnis\Json\Normalizer\NormalizerInterface; +use Ergebnis\Json\Printer; use Ergebnis\Test\Util\Helper; use PHPUnit\Framework; @@ -50,4 +51,21 @@ final protected function className(): string return $className; } + + final protected static function assertJsonStringEqualsJsonStringNormalized(string $expected, string $actual): void + { + $printer = new Printer\Printer(); + + $normalize = static function (string $json) use ($printer): string { + self::assertJson($json); + + $normalized = \json_encode(\json_decode($json)); + + self::assertIsString($normalized); + + return $printer->print($normalized); + }; + + self::assertSame($normalize($expected), $normalize($actual)); + } } diff --git a/test/Unit/Vendor/Composer/AbstractComposerTestCase.php b/test/Unit/Vendor/Composer/AbstractComposerTestCase.php index d3dad985..9d4d55a8 100644 --- a/test/Unit/Vendor/Composer/AbstractComposerTestCase.php +++ b/test/Unit/Vendor/Composer/AbstractComposerTestCase.php @@ -39,7 +39,7 @@ final public function testNormalizeDoesNotModifyWhenJsonDecodedIsNotAnObject(str $normalized = $normalizer->normalize($json); - self::assertSame($json->encoded(), $normalized->encoded()); + self::assertJsonStringEqualsJsonStringNormalized($json->encoded(), $normalized->encoded()); } /** diff --git a/test/Unit/Vendor/Composer/BinNormalizerTest.php b/test/Unit/Vendor/Composer/BinNormalizerTest.php index fb37237e..2fdccc5c 100644 --- a/test/Unit/Vendor/Composer/BinNormalizerTest.php +++ b/test/Unit/Vendor/Composer/BinNormalizerTest.php @@ -42,7 +42,7 @@ public function testNormalizeDoesNotModifyOtherProperty(): void $normalized = $normalizer->normalize($json); - self::assertSame($json->encoded(), $normalized->encoded()); + self::assertJsonStringEqualsJsonStringNormalized($json->encoded(), $normalized->encoded()); } public function testNormalizeDoesNotModifyBinIfPropertyExistsAsString(): void @@ -63,7 +63,7 @@ public function testNormalizeDoesNotModifyBinIfPropertyExistsAsString(): void $normalized = $normalizer->normalize($json); - self::assertSame($json->encoded(), $normalized->encoded()); + self::assertJsonStringEqualsJsonStringNormalized($json->encoded(), $normalized->encoded()); } public function testNormalizeSortsBinIfPropertyExistsAsArray(): void @@ -102,6 +102,6 @@ public function testNormalizeSortsBinIfPropertyExistsAsArray(): void $normalized = $normalizer->normalize($json); - self::assertSame(\json_encode(\json_decode($expected->encoded())), $normalized->encoded()); + self::assertJsonStringEqualsJsonStringNormalized($expected->encoded(), $normalized->encoded()); } } diff --git a/test/Unit/Vendor/Composer/ComposerJsonNormalizerTest.php b/test/Unit/Vendor/Composer/ComposerJsonNormalizerTest.php index 2f263b14..0aa5ab26 100644 --- a/test/Unit/Vendor/Composer/ComposerJsonNormalizerTest.php +++ b/test/Unit/Vendor/Composer/ComposerJsonNormalizerTest.php @@ -218,7 +218,7 @@ public function testNormalizeNormalizes(): void $normalized = $normalizer->normalize($json); - self::assertSame(\json_encode(\json_decode($expected->encoded())), $normalized->encoded()); + self::assertJsonStringEqualsJsonStringNormalized($expected->encoded(), $normalized->encoded()); } /** diff --git a/test/Unit/Vendor/Composer/ConfigHashNormalizerTest.php b/test/Unit/Vendor/Composer/ConfigHashNormalizerTest.php index 25d31b62..74c20f62 100644 --- a/test/Unit/Vendor/Composer/ConfigHashNormalizerTest.php +++ b/test/Unit/Vendor/Composer/ConfigHashNormalizerTest.php @@ -42,7 +42,7 @@ public function testNormalizeDoesNotModifyOtherProperty(): void $normalized = $normalizer->normalize($json); - self::assertSame($json->encoded(), $normalized->encoded()); + self::assertJsonStringEqualsJsonStringNormalized($json->encoded(), $normalized->encoded()); } /** @@ -62,7 +62,7 @@ public function testNormalizeIgnoresEmptyConfigHash(string $property): void $normalized = $normalizer->normalize($json); - self::assertSame($json->encoded(), $normalized->encoded()); + self::assertJsonStringEqualsJsonStringNormalized($json->encoded(), $normalized->encoded()); } /** @@ -104,7 +104,7 @@ public function testNormalizeSortsConfigHashIfPropertyExists(string $property): $normalized = $normalizer->normalize($json); - self::assertSame(\json_encode(\json_decode($expected->encoded())), $normalized->encoded()); + self::assertJsonStringEqualsJsonStringNormalized($expected->encoded(), $normalized->encoded()); } /** diff --git a/test/Unit/Vendor/Composer/PackageHashNormalizerTest.php b/test/Unit/Vendor/Composer/PackageHashNormalizerTest.php index ef8c4c92..82ffcb70 100644 --- a/test/Unit/Vendor/Composer/PackageHashNormalizerTest.php +++ b/test/Unit/Vendor/Composer/PackageHashNormalizerTest.php @@ -42,7 +42,7 @@ public function testNormalizeDoesNotModifyOtherProperty(): void $normalized = $normalizer->normalize($json); - self::assertSame($json->encoded(), $normalized->encoded()); + self::assertJsonStringEqualsJsonStringNormalized($json->encoded(), $normalized->encoded()); } /** @@ -62,7 +62,7 @@ public function testNormalizeIgnoresEmptyPackageHash(string $property): void $normalized = $normalizer->normalize($json); - self::assertSame(\json_encode(\json_decode($json->encoded())), $normalized->encoded()); + self::assertJsonStringEqualsJsonStringNormalized($json->encoded(), $normalized->encoded()); } /** @@ -112,7 +112,7 @@ public function testNormalizeSortsPackageHashIfPropertyExists(string $property): $normalized = $normalizer->normalize($json); - self::assertSame(\json_encode(\json_decode($expected->encoded())), $normalized->encoded()); + self::assertJsonStringEqualsJsonStringNormalized($expected->encoded(), $normalized->encoded()); } /** diff --git a/test/Unit/Vendor/Composer/VersionConstraintNormalizerTest.php b/test/Unit/Vendor/Composer/VersionConstraintNormalizerTest.php index 0803bb34..756fdc78 100644 --- a/test/Unit/Vendor/Composer/VersionConstraintNormalizerTest.php +++ b/test/Unit/Vendor/Composer/VersionConstraintNormalizerTest.php @@ -44,7 +44,7 @@ public function testNormalizeDoesNotModifyOtherProperty(string $constraint): voi $normalized = $normalizer->normalize($json); - self::assertSame($json->encoded(), $normalized->encoded()); + self::assertJsonStringEqualsJsonStringNormalized($json->encoded(), $normalized->encoded()); } /** @@ -76,7 +76,7 @@ public function testNormalizeIgnoresEmptyPackageHash(string $property): void $normalized = $normalizer->normalize($json); - self::assertSame(\json_encode(\json_decode($json->encoded())), $normalized->encoded()); + self::assertJsonStringEqualsJsonStringNormalized($json->encoded(), $normalized->encoded()); } /**