From a5a835fcdad9d7ebf096ad48525df1793bf07169 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Wed, 11 Oct 2023 11:45:15 +0100 Subject: [PATCH] Improve test coverage --- psalm-baseline.xml | 6 ------ src/Format/JsonEncodeOptions.php | 2 +- src/Format/NewLine.php | 2 +- src/Vendor/Composer/PackageHashNormalizer.php | 2 +- src/Vendor/Composer/VersionConstraintNormalizer.php | 2 +- .../HasProperty/RequireAndRequireDev/normalized.json | 10 ++++++++++ .../Yes/HasProperty/RequireAndRequireDev/original.json | 10 ++++++++++ .../HasEntries/Yes/IsSortedByKey/Yes/normalized.json | 1 + .../HasEntries/Yes/IsSortedByKey/Yes/original.json | 1 + .../Yes/IsSortedByPackage/No/normalized.json | 1 + .../HasEntries/Yes/IsSortedByPackage/No/original.json | 1 + test/Unit/Format/IndentTest.php | 2 ++ test/Unit/Format/JsonEncodeOptionsTest.php | 2 +- test/Unit/Format/NewLineTest.php | 1 + .../Vendor/Composer/ComposerJsonNormalizerTest.php | 1 + 15 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/RequireAndRequireDev/normalized.json create mode 100644 test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/RequireAndRequireDev/original.json diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 300b4c0e..1386701f 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -36,17 +36,11 @@ $value - - $value - $value - - $value - diff --git a/src/Format/JsonEncodeOptions.php b/src/Format/JsonEncodeOptions.php index 051c1573..2b153be9 100644 --- a/src/Format/JsonEncodeOptions.php +++ b/src/Format/JsonEncodeOptions.php @@ -47,7 +47,7 @@ public static function fromJson(Json $json): self $jsonEncodeOptions = 0; if (!\str_contains($json->encoded(), '\/')) { - $jsonEncodeOptions |= \JSON_UNESCAPED_SLASHES; + $jsonEncodeOptions = \JSON_UNESCAPED_SLASHES; } if (1 !== \preg_match('/(\\\\+)u([0-9a-f]{4})/i', $json->encoded())) { diff --git a/src/Format/NewLine.php b/src/Format/NewLine.php index 2f479054..c962a480 100644 --- a/src/Format/NewLine.php +++ b/src/Format/NewLine.php @@ -30,7 +30,7 @@ private function __construct(private readonly string $value) */ public static function fromString(string $value): self { - if (1 !== \preg_match('/^(?>\r\n|\n|\r)$/', $value)) { + if ("\n" !== $value && "\r" !== $value && "\r\n" !== $value) { throw Exception\InvalidNewLineString::fromString($value); } diff --git a/src/Vendor/Composer/PackageHashNormalizer.php b/src/Vendor/Composer/PackageHashNormalizer.php index 019134a8..0f73d4dd 100644 --- a/src/Vendor/Composer/PackageHashNormalizer.php +++ b/src/Vendor/Composer/PackageHashNormalizer.php @@ -51,7 +51,7 @@ public function normalize(Json $json): Json foreach ($objectPropertiesThatShouldBeNormalized as $name => $value) { /** @var array $packages */ - $packages = (array) $decoded->{$name}; + $packages = (array) $value; if ([] === $packages) { continue; diff --git a/src/Vendor/Composer/VersionConstraintNormalizer.php b/src/Vendor/Composer/VersionConstraintNormalizer.php index 2ac9a6cf..91031a6a 100644 --- a/src/Vendor/Composer/VersionConstraintNormalizer.php +++ b/src/Vendor/Composer/VersionConstraintNormalizer.php @@ -50,7 +50,7 @@ public function normalize(Json $json): Json } foreach ($objectPropertiesThatShouldBeNormalized as $name => $value) { - $packages = (array) $decoded->{$name}; + $packages = (array) $value; if ([] === $packages) { continue; diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/RequireAndRequireDev/normalized.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/RequireAndRequireDev/normalized.json new file mode 100644 index 00000000..602cd33e --- /dev/null +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/RequireAndRequireDev/normalized.json @@ -0,0 +1,10 @@ +{ + "description": "This test exists to cover certain mutants from infection/infection", + "require": {}, + "require-dev": { + "ergebnis/overlapping-version-constraints-1": "^1.0 || 3.4.5", + "ergebnis/overlapping-version-constraints-2": "^1.0 || ^2.0", + "ergebnis/separator-mixing": "^1.0 || ^1.2 >=1.2.4 <1.2.7", + "ergebnis/whitespace-around-version": "*" + } +} diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/RequireAndRequireDev/original.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/RequireAndRequireDev/original.json new file mode 100644 index 00000000..12260a8f --- /dev/null +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/RequireAndRequireDev/original.json @@ -0,0 +1,10 @@ +{ + "description": "This test exists to cover certain mutants from infection/infection", + "require": {}, + "require-dev": { + "ergebnis/whitespace-around-version": " * ", + "ergebnis/overlapping-version-constraints-1": "3.4.5 || ^1.0 || ^1.1 || ^1.2", + "ergebnis/overlapping-version-constraints-2": "^1.0 || ^2.0 || ^1.1 || ^2.1", + "ergebnis/separator-mixing": "^1.0 | ^1.1 || ^1.2,<1.2.7 >=1.2.4 || ^1.4" + } +} diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Suggest/HasEntries/Yes/IsSortedByKey/Yes/normalized.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Suggest/HasEntries/Yes/IsSortedByKey/Yes/normalized.json index ced72310..40459a03 100644 --- a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Suggest/HasEntries/Yes/IsSortedByKey/Yes/normalized.json +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Suggest/HasEntries/Yes/IsSortedByKey/Yes/normalized.json @@ -1,5 +1,6 @@ { "homepage": "https://getcomposer.org/doc/04-schema.md#suggest", + "require": {}, "suggest": { "php": "Nothing works without it", "hhvm": "Okay", diff --git a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Suggest/HasEntries/Yes/IsSortedByKey/Yes/original.json b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Suggest/HasEntries/Yes/IsSortedByKey/Yes/original.json index db64e45e..fa2fa640 100644 --- a/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Suggest/HasEntries/Yes/IsSortedByKey/Yes/original.json +++ b/test/Fixture/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/Suggest/HasEntries/Yes/IsSortedByKey/Yes/original.json @@ -1,5 +1,6 @@ { "homepage": "https://getcomposer.org/doc/04-schema.md#suggest", + "require": {}, "suggest": { "php": "Nothing works without it", "hhvm": "Okay", diff --git a/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/Yes/IsSortedByPackage/No/normalized.json b/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/Yes/IsSortedByPackage/No/normalized.json index cae0ba2c..b60978fb 100644 --- a/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/Yes/IsSortedByPackage/No/normalized.json +++ b/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/Yes/IsSortedByPackage/No/normalized.json @@ -5,6 +5,7 @@ "ext-foo": "*", "lib-baz": "*", "composer-plugin-api": "*", + "0-test/sort-order": "*", "acquia/drupal-environment-detector": "*", "ergebnis/php-cs-fixer-config": "*", "ergebnis/test-util": "*" diff --git a/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/Yes/IsSortedByPackage/No/original.json b/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/Yes/IsSortedByPackage/No/original.json index 4f7ecc1e..36aba36a 100644 --- a/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/Yes/IsSortedByPackage/No/original.json +++ b/test/Template/Vendor/Composer/ComposerJsonNormalizer/NormalizeNormalizesJson/Json/IsObject/HasEntries/Yes/HasProperty/ValueContainsPackagesAndVersionConstraints/HasEntries/Yes/IsSortedByPackage/No/original.json @@ -7,6 +7,7 @@ "acquia/drupal-environment-detector": "*", "ext-foo": "*", "composer-plugin-api": "*", + "0-test/sort-order": "*", "php": "*" } } diff --git a/test/Unit/Format/IndentTest.php b/test/Unit/Format/IndentTest.php index f25c8611..7ad6c197 100644 --- a/test/Unit/Format/IndentTest.php +++ b/test/Unit/Format/IndentTest.php @@ -43,6 +43,7 @@ public function testFromSizeAndStyleRejectsInvalidSize(int $size): void $style = self::faker()->randomElement(\array_keys(Format\Indent::CHARACTERS)); $this->expectException(Exception\InvalidIndentSize::class); + $this->expectExceptionMessage(\sprintf('Size needs to be greater than %d, but %d is not.', 0, $size)); Format\Indent::fromSizeAndStyle( $size, @@ -76,6 +77,7 @@ public function testFromSizeAndStyleRejectsInvalidStyle(): void $style = $faker->sentence(); $this->expectException(Exception\InvalidIndentStyle::class); + $this->expectExceptionMessage(\sprintf('Style needs to be one of "space", "tab", but "%s" is not.', $style)); Format\Indent::fromSizeAndStyle( $size, diff --git a/test/Unit/Format/JsonEncodeOptionsTest.php b/test/Unit/Format/JsonEncodeOptionsTest.php index 7d0efaf0..cb878469 100644 --- a/test/Unit/Format/JsonEncodeOptionsTest.php +++ b/test/Unit/Format/JsonEncodeOptionsTest.php @@ -112,7 +112,7 @@ public static function provideJsonEncodeOptionsAndEncoded(): array [ \JSON_UNESCAPED_SLASHES, '{ - "name": "Andreas M\u00f6ller", + "name": "Andreas M\u00F6ller", "url": "https://github.com/ergebnis/json-normalizer" }', ], diff --git a/test/Unit/Format/NewLineTest.php b/test/Unit/Format/NewLineTest.php index 94056b4f..acf6e1f9 100644 --- a/test/Unit/Format/NewLineTest.php +++ b/test/Unit/Format/NewLineTest.php @@ -26,6 +26,7 @@ final class NewLineTest extends Framework\TestCase public function testFromStringRejectsInvalidNewLineString(string $string): void { $this->expectException(Exception\InvalidNewLineString::class); + $this->expectExceptionMessage(\sprintf('"%s" is not a valid new-line character sequence.', $string)); Format\NewLine::fromString($string); } diff --git a/test/Unit/Vendor/Composer/ComposerJsonNormalizerTest.php b/test/Unit/Vendor/Composer/ComposerJsonNormalizerTest.php index d33c472e..73faf43f 100644 --- a/test/Unit/Vendor/Composer/ComposerJsonNormalizerTest.php +++ b/test/Unit/Vendor/Composer/ComposerJsonNormalizerTest.php @@ -23,6 +23,7 @@ use Ergebnis\Json\Normalizer\WithFinalNewLineNormalizer; use PHPUnit\Framework; +#[Framework\Attributes\CoversClass(SchemaNormalizer::class)] #[Framework\Attributes\CoversClass(Vendor\Composer\BinNormalizer::class)] #[Framework\Attributes\CoversClass(Vendor\Composer\ComposerJsonNormalizer::class)] #[Framework\Attributes\CoversClass(Vendor\Composer\ConfigHashNormalizer::class)]