Skip to content

Commit

Permalink
Enhancement: Extract assertion that normalizes JSON before comparing
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Dec 29, 2020
1 parent a2fe0ee commit 93e964a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 13 deletions.
18 changes: 18 additions & 0 deletions test/Unit/AbstractNormalizerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
}
}
2 changes: 1 addition & 1 deletion test/Unit/Vendor/Composer/AbstractComposerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test/Unit/Vendor/Composer/BinNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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());
}
}
2 changes: 1 addition & 1 deletion test/Unit/Vendor/Composer/ComposerJsonNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test/Unit/Vendor/Composer/ConfigHashNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testNormalizeDoesNotModifyOtherProperty(): void

$normalized = $normalizer->normalize($json);

self::assertSame($json->encoded(), $normalized->encoded());
self::assertJsonStringEqualsJsonStringNormalized($json->encoded(), $normalized->encoded());
}

/**
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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());
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test/Unit/Vendor/Composer/PackageHashNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testNormalizeDoesNotModifyOtherProperty(): void

$normalized = $normalizer->normalize($json);

self::assertSame($json->encoded(), $normalized->encoded());
self::assertJsonStringEqualsJsonStringNormalized($json->encoded(), $normalized->encoded());
}

/**
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/Unit/Vendor/Composer/VersionConstraintNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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());
}

/**
Expand Down

0 comments on commit 93e964a

Please sign in to comment.