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

Enhancement: Extract assertion that normalizes JSON before comparing #422

Merged
merged 1 commit into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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