diff --git a/README.md b/README.md index f512a9b1..9b927644 100644 --- a/README.md +++ b/README.md @@ -30,18 +30,20 @@ The `NormalizeCommand` provided by the `NormalizePlugin` within this package wil * determine whether a `composer.json` exists * determine whether a `composer.lock` exists, and if so, whether it is up to date * use the `ComposerJsonNormalizer` to normalize the content of `composer.json` -* write the normalized content of `composer.json` back to the file +* format the normalized content (either as sniffed, or as specified using the `--indent-size` and `--indent-style` options) +* write the normalized and formatted content of `composer.json` back to the file * update the hash in `composer.lock` if it exists and if an update is necessary ### Options +* `--indent-size`: Indent size (an integer greater than 0); should be used with the `--indent-style` option +* `--indent-style`: Indent style (one of "space", "tab"); should be used with the `--indent-size` option * `--no-update-lock`: Do not update lock file if it exists ## Normalizers The `ComposerJsonNormalizer` composes normalizers provided by [`localheinz/json-normalizer`](https://github.com/localheinz/json-normalizer): -* [`Localheinz\Json\Normalizer\AutoFormatNormalizer`](https://github.com/localheinz/json-normalizer#autoformatnormalizer) * [`Localheinz\Json\Normalizer\ChainNormalizer`](https://github.com/localheinz/json-normalizer#chainnormalizer) * [`Localheinz\Json\Normalizer\SchemaNormalizer`](https://github.com/localheinz/json-normalizer#schemanormalizer) diff --git a/src/Command/NormalizeCommand.php b/src/Command/NormalizeCommand.php index 06aca168..1d92c3a3 100644 --- a/src/Command/NormalizeCommand.php +++ b/src/Command/NormalizeCommand.php @@ -20,22 +20,60 @@ final class NormalizeCommand extends Command\BaseCommand { + /** + * @var array + */ + private $indentStyles = [ + 'space' => ' ', + 'tab' => "\t", + ]; + /** * @var Normalizer\NormalizerInterface */ private $normalizer; - public function __construct(Normalizer\NormalizerInterface $normalizer) - { + /** + * @var Normalizer\Format\SnifferInterface + */ + private $sniffer; + + /** + * @var Normalizer\Format\FormatterInterface + */ + private $formatter; + + public function __construct( + Normalizer\NormalizerInterface $normalizer, + Normalizer\Format\SnifferInterface $sniffer = null, + Normalizer\Format\FormatterInterface $formatter = null + ) { parent::__construct('normalize'); $this->normalizer = $normalizer; + $this->sniffer = $sniffer ?: new Normalizer\Format\Sniffer(); + $this->formatter = $formatter ?: new Normalizer\Format\Formatter(); } protected function configure(): void { $this->setDescription('Normalizes composer.json according to its JSON schema (https://getcomposer.org/schema.json).'); $this->setDefinition([ + new Console\Input\InputOption( + 'indent-size', + null, + Console\Input\InputOption::VALUE_REQUIRED, + 'Indent size (an integer greater than 0); should be used with the --indent-style option' + ), + new Console\Input\InputOption( + 'indent-style', + null, + Console\Input\InputOption::VALUE_REQUIRED, + \sprintf( + 'Indent style (one of "%s"); should be used with the --indent-size option', + \implode('", "', \array_keys($this->indentStyles)) + ) + ), new Console\Input\InputOption( 'no-update-lock', null, @@ -47,10 +85,56 @@ protected function configure(): void protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): int { - $file = Factory::getComposerFile(); - $io = $this->getIO(); + $indent = null; + + $indentSize = $input->getOption('indent-size'); + $indentStyle = $input->getOption('indent-style'); + + if (null !== $indentSize || null !== $indentStyle) { + if (null === $indentSize) { + $io->writeError('When using the indent-style option, an indent size needs to be specified using the indent-size option.'); + + return 1; + } + + if (null === $indentStyle) { + $io->writeError(\sprintf( + 'When using the indent-size option, an indent style (one of "%s") needs to be specified using the indent-style option.', + \implode('", "', \array_keys($this->indentStyles)) + )); + + return 1; + } + + if ((string) (int) $indentSize !== (string) $indentSize || 1 > $indentSize) { + $io->writeError(\sprintf( + 'Indent size needs to be an integer greater than 0, but "%s" is not.', + $indentSize + )); + + return 1; + } + + if (!\array_key_exists($indentStyle, $this->indentStyles)) { + $io->writeError(\sprintf( + 'Indent style needs to be one of "%s", but "%s" is not.', + \implode('", "', \array_keys($this->indentStyles)), + $indentStyle + )); + + return 1; + } + + $indent = \str_repeat( + $this->indentStyles[$indentStyle], + (int) $indentSize + ); + } + + $file = Factory::getComposerFile(); + if (!\file_exists($file)) { $io->writeError(\sprintf( '%s not found.', @@ -108,7 +192,18 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O return 1; } - if ($json === $normalized) { + $format = $this->sniffer->sniff($json); + + if (null !== $indent) { + $format = $format->withIndent($indent); + } + + $formatted = $this->formatter->format( + $normalized, + $format + ); + + if ($json === $formatted) { $io->write(\sprintf( '%s is already normalized.', $file @@ -117,7 +212,7 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O return 0; } - \file_put_contents($file, $normalized); + \file_put_contents($file, $formatted); $io->write(\sprintf( 'Successfully normalized %s.', diff --git a/src/Normalizer/ComposerJsonNormalizer.php b/src/Normalizer/ComposerJsonNormalizer.php index 42214fe4..f15a6415 100644 --- a/src/Normalizer/ComposerJsonNormalizer.php +++ b/src/Normalizer/ComposerJsonNormalizer.php @@ -13,7 +13,6 @@ namespace Localheinz\Composer\Normalize\Normalizer; -use Localheinz\Json\Normalizer\AutoFormatNormalizer; use Localheinz\Json\Normalizer\ChainNormalizer; use Localheinz\Json\Normalizer\NormalizerInterface; use Localheinz\Json\Normalizer\SchemaNormalizer; @@ -27,13 +26,13 @@ final class ComposerJsonNormalizer implements NormalizerInterface public function __construct(string $schemaUri = 'https://getcomposer.org/schema.json') { - $this->normalizer = new AutoFormatNormalizer(new ChainNormalizer( + $this->normalizer = new ChainNormalizer( new SchemaNormalizer($schemaUri), new BinNormalizer(), new ConfigHashNormalizer(), new PackageHashNormalizer(), new VersionConstraintNormalizer() - )); + ); } public function normalize(string $json): string diff --git a/test/Unit/Command/NormalizeCommandTest.php b/test/Unit/Command/NormalizeCommandTest.php index aafc0418..89261f98 100644 --- a/test/Unit/Command/NormalizeCommandTest.php +++ b/test/Unit/Command/NormalizeCommandTest.php @@ -68,6 +68,44 @@ public function testHasNoArguments(): void $this->assertCount(0, $definition->getArguments()); } + public function testHasIndentSizeOption(): void + { + $command = new NormalizeCommand($this->prophesize(Normalizer\NormalizerInterface::class)->reveal()); + + $definition = $command->getDefinition(); + + $this->assertTrue($definition->hasOption('indent-size')); + + $option = $definition->getOption('indent-size'); + + $this->assertNull($option->getShortcut()); + $this->assertTrue($option->isValueRequired()); + $this->assertNull($option->getDefault()); + $this->assertSame('Indent size (an integer greater than 0); should be used with the --indent-style option', $option->getDescription()); + } + + public function testHasIndentStyleOption(): void + { + $command = new NormalizeCommand($this->prophesize(Normalizer\NormalizerInterface::class)->reveal()); + + $definition = $command->getDefinition(); + + $this->assertTrue($definition->hasOption('indent-style')); + + $option = $definition->getOption('indent-style'); + + $this->assertNull($option->getShortcut()); + $this->assertTrue($option->isValueRequired()); + $this->assertNull($option->getDefault()); + + $description = \sprintf( + 'Indent style (one of "%s"); should be used with the --indent-size option', + \implode('", "', \array_keys($this->indentStyles())) + ); + + $this->assertSame($description, $option->getDescription()); + } + public function testHasNoUpdateLockOption(): void { $command = new NormalizeCommand($this->prophesize(Normalizer\NormalizerInterface::class)->reveal()); @@ -84,6 +122,156 @@ public function testHasNoUpdateLockOption(): void $this->assertSame('Do not update lock file if it exists', $option->getDescription()); } + public function testExecuteFailsIfIndentSizeOptionIsUsedWithoutIndentStyleOption(): void + { + $original = $this->composerFileContent(); + + $composerFile = $this->pathToComposerFileWithContent($original); + + $io = $this->prophesize(IO\ConsoleIO::class); + + $io + ->writeError(Argument::is(\sprintf( + 'When using the indent-size option, an indent style (one of "%s") needs to be specified using the indent-style option.', + \implode('", "', \array_keys($this->indentStyles())) + ))) + ->shouldBeCalled(); + + $command = new NormalizeCommand($this->prophesize(Normalizer\NormalizerInterface::class)->reveal()); + + $command->setIO($io->reveal()); + + $tester = new Console\Tester\CommandTester($command); + + $tester->execute([ + '--indent-size' => $this->faker()->numberBetween(1), + ]); + + $this->assertSame(1, $tester->getStatusCode()); + $this->assertFileExists($composerFile); + $this->assertStringEqualsFile($composerFile, $original); + } + + public function testExecuteFailsIfIndentStyleOptionIsUsedWithoutIndentSizeOption(): void + { + $original = $this->composerFileContent(); + + $composerFile = $this->pathToComposerFileWithContent($original); + + $io = $this->prophesize(IO\ConsoleIO::class); + + $io + ->writeError(Argument::is('When using the indent-style option, an indent size needs to be specified using the indent-size option.')) + ->shouldBeCalled(); + + $command = new NormalizeCommand($this->prophesize(Normalizer\NormalizerInterface::class)->reveal()); + + $command->setIO($io->reveal()); + + $tester = new Console\Tester\CommandTester($command); + + $tester->execute([ + '--indent-style' => $this->faker()->randomElement([ + 'space', + 'tab', + ]), + ]); + + $this->assertSame(1, $tester->getStatusCode()); + $this->assertFileExists($composerFile); + $this->assertStringEqualsFile($composerFile, $original); + } + + /** + * @dataProvider providerInvalidIndentSize + * + * @param $indentSize + */ + public function testExecuteFailsIfInvalidIndentSizeIsUsed($indentSize): void + { + $indentStyle = $this->faker()->randomElement(\array_keys($this->indentStyles())); + + $original = $this->composerFileContent(); + + $composerFile = $this->pathToComposerFileWithContent($original); + + $io = $this->prophesize(IO\ConsoleIO::class); + + $io + ->writeError(Argument::is(\sprintf( + 'Indent size needs to be an integer greater than 0, but "%s" is not.', + $indentSize + ))) + ->shouldBeCalled(); + + $command = new NormalizeCommand($this->prophesize(Normalizer\NormalizerInterface::class)->reveal()); + + $command->setIO($io->reveal()); + + $tester = new Console\Tester\CommandTester($command); + + $tester->execute([ + '--indent-size' => $indentSize, + '--indent-style' => $indentStyle, + ]); + + $this->assertSame(1, $tester->getStatusCode()); + $this->assertFileExists($composerFile); + $this->assertStringEqualsFile($composerFile, $original); + } + + public function providerInvalidIndentSize(): \Generator + { + $values = [ + 'string-word' => $this->faker()->word, + 'int-zero' => 0, + 'int-negative' => -1, + 'int-zero-casted-to-string' => '0', + 'int-negative-casted-to-string' => '-1', + ]; + + foreach ($values as $key => $value) { + yield $key => [ + $value, + ]; + } + } + + public function testExecuteFailsIfInvalidIndentStyleOptionIsUsed(): void + { + $indentSize = $this->faker()->numberBetween(1); + $indentStyle = $this->faker()->sentence; + + $original = $this->composerFileContent(); + + $composerFile = $this->pathToComposerFileWithContent($original); + + $io = $this->prophesize(IO\ConsoleIO::class); + + $io + ->writeError(Argument::is(\sprintf( + 'Indent style needs to be one of "%s", but "%s" is not.', + \implode('", "', \array_keys($this->indentStyles())), + $indentStyle + ))) + ->shouldBeCalled(); + + $command = new NormalizeCommand($this->prophesize(Normalizer\NormalizerInterface::class)->reveal()); + + $command->setIO($io->reveal()); + + $tester = new Console\Tester\CommandTester($command); + + $tester->execute([ + '--indent-size' => $indentSize, + '--indent-style' => $indentStyle, + ]); + + $this->assertSame(1, $tester->getStatusCode()); + $this->assertFileExists($composerFile); + $this->assertStringEqualsFile($composerFile, $original); + } + public function testExecuteFailsIfComposerFileDoesNotExist(): void { $composerFile = $this->pathToNonExistentComposerFile(); @@ -106,7 +294,6 @@ public function testExecuteFailsIfComposerFileDoesNotExist(): void $tester->execute([]); $this->assertSame(1, $tester->getStatusCode()); - $this->assertFileNotExists($composerFile); } public function testExecuteFailsIfComposerFileIsNotReadable(): void @@ -366,6 +553,8 @@ public function testExecuteSucceedsIfLockerIsNotLockedAndComposerFileIsAlreadyNo { $original = $this->composerFileContent(); + $normalized = \json_encode(\json_decode($original)); + $composerFile = $this->pathToComposerFileWithContent($original); $io = $this->prophesize(IO\ConsoleIO::class); @@ -396,9 +585,32 @@ public function testExecuteSucceedsIfLockerIsNotLockedAndComposerFileIsAlreadyNo $normalizer ->normalize(Argument::is($original)) ->shouldBeCalled() + ->willReturn($normalized); + + $format = $this->prophesize(Normalizer\Format\FormatInterface::class); + + $sniffer = $this->prophesize(Normalizer\Format\SnifferInterface::class); + + $sniffer + ->sniff(Argument::is($original)) + ->shouldBeCalled() + ->willReturn($format->reveal()); + + $formatter = $this->prophesize(Normalizer\Format\FormatterInterface::class); + + $formatter + ->format( + Argument::is($normalized), + Argument::is($format->reveal()) + ) + ->shouldBeCalled() ->willReturn($original); - $command = new NormalizeCommand($normalizer->reveal()); + $command = new NormalizeCommand( + $normalizer->reveal(), + $sniffer->reveal(), + $formatter->reveal() + ); $command->setIO($io->reveal()); $command->setComposer($composer->reveal()); @@ -416,6 +628,8 @@ public function testExecuteSucceedsIfLockerIsLockedAndFreshButComposerFileIsAlre { $original = $this->composerFileContent(); + $normalized = \json_encode(\json_decode($original)); + $composerFile = $this->pathToComposerFileWithContent($original); $io = $this->prophesize(IO\ConsoleIO::class); @@ -451,9 +665,32 @@ public function testExecuteSucceedsIfLockerIsLockedAndFreshButComposerFileIsAlre $normalizer ->normalize(Argument::is($original)) ->shouldBeCalled() + ->willReturn($normalized); + + $format = $this->prophesize(Normalizer\Format\FormatInterface::class); + + $sniffer = $this->prophesize(Normalizer\Format\SnifferInterface::class); + + $sniffer + ->sniff(Argument::is($original)) + ->shouldBeCalled() + ->willReturn($format->reveal()); + + $formatter = $this->prophesize(Normalizer\Format\FormatterInterface::class); + + $formatter + ->format( + Argument::is($normalized), + Argument::is($format->reveal()) + ) + ->shouldBeCalled() ->willReturn($original); - $command = new NormalizeCommand($normalizer->reveal()); + $command = new NormalizeCommand( + $normalizer->reveal(), + $sniffer->reveal(), + $formatter->reveal() + ); $command->setIO($io->reveal()); $command->setComposer($composer->reveal()); @@ -476,6 +713,11 @@ public function testExecuteSucceedsIfLockerIsNotLockedAndComposerFileWasNormaliz true ))); + $formatted = \json_encode( + \json_decode($normalized), + JSON_PRETTY_PRINT + ); + $composerFile = $this->pathToComposerFileWithContent($original); $io = $this->prophesize(IO\ConsoleIO::class); @@ -508,7 +750,30 @@ public function testExecuteSucceedsIfLockerIsNotLockedAndComposerFileWasNormaliz ->shouldBeCalled() ->willReturn($normalized); - $command = new NormalizeCommand($normalizer->reveal()); + $format = $this->prophesize(Normalizer\Format\FormatInterface::class); + + $sniffer = $this->prophesize(Normalizer\Format\SnifferInterface::class); + + $sniffer + ->sniff(Argument::is($original)) + ->shouldBeCalled() + ->willReturn($format->reveal()); + + $formatter = $this->prophesize(Normalizer\Format\FormatterInterface::class); + + $formatter + ->format( + Argument::is($normalized), + Argument::is($format->reveal()) + ) + ->shouldBeCalled() + ->willReturn($formatted); + + $command = new NormalizeCommand( + $normalizer->reveal(), + $sniffer->reveal(), + $formatter->reveal() + ); $command->setIO($io->reveal()); $command->setComposer($composer->reveal()); @@ -519,7 +784,110 @@ public function testExecuteSucceedsIfLockerIsNotLockedAndComposerFileWasNormaliz $this->assertSame(0, $tester->getStatusCode()); $this->assertFileExists($composerFile); - $this->assertStringEqualsFile($composerFile, $normalized); + $this->assertStringEqualsFile($composerFile, $formatted); + } + + public function testExecuteSucceedsIfLockerIsNotLockedAndComposerFileWasNormalizedSuccessfullyWithIndent(): void + { + $faker = $this->faker(); + + $indentSize = (string) $faker->numberBetween(1, 5); + $indentStyle = $faker->randomElement(\array_keys($this->indentStyles())); + + $indent = \str_repeat( + $this->indentStyles()[$indentStyle], + (int) $indentSize + ); + + $original = $this->composerFileContent(); + + $normalized = \json_encode(\array_reverse(\json_decode( + $original, + true + ))); + + $formatted = \json_encode( + \json_decode($normalized), + JSON_PRETTY_PRINT + ); + + $composerFile = $this->pathToComposerFileWithContent($original); + + $io = $this->prophesize(IO\ConsoleIO::class); + + $io + ->write(Argument::is(\sprintf( + 'Successfully normalized %s.', + $composerFile + ))) + ->shouldBeCalled(); + + $locker = $this->prophesize(Package\Locker::class); + + $locker + ->isLocked() + ->shouldBeCalled() + ->willReturn(false); + + $composer = $this->prophesize(Composer::class); + + $composer + ->getLocker() + ->shouldBeCalled() + ->willReturn($locker); + + $normalizer = $this->prophesize(Normalizer\NormalizerInterface::class); + + $normalizer + ->normalize(Argument::is($original)) + ->shouldBeCalled() + ->willReturn($normalized); + + $configuredFormat = $this->prophesize(Normalizer\Format\FormatInterface::class); + + $sniffedFormat = $this->prophesize(Normalizer\Format\FormatInterface::class); + + $sniffedFormat + ->withIndent(Argument::is($indent)) + ->shouldBeCalled() + ->willReturn($configuredFormat->reveal()); + + $sniffer = $this->prophesize(Normalizer\Format\SnifferInterface::class); + + $sniffer + ->sniff(Argument::is($original)) + ->shouldBeCalled() + ->willReturn($sniffedFormat->reveal()); + + $formatter = $this->prophesize(Normalizer\Format\FormatterInterface::class); + + $formatter + ->format( + Argument::is($normalized), + Argument::is($configuredFormat->reveal()) + ) + ->shouldBeCalled() + ->willReturn($formatted); + + $command = new NormalizeCommand( + $normalizer->reveal(), + $sniffer->reveal(), + $formatter->reveal() + ); + + $command->setIO($io->reveal()); + $command->setComposer($composer->reveal()); + + $tester = new Console\Tester\CommandTester($command); + + $tester->execute([ + '--indent-size' => $indentSize, + '--indent-style' => $indentStyle, + ]); + + $this->assertSame(0, $tester->getStatusCode()); + $this->assertFileExists($composerFile); + $this->assertStringEqualsFile($composerFile, $formatted); } public function testExecuteSucceedsIfLockerIsLockedAndLockerCouldBeUpdatedAfterNormalization(): void @@ -531,6 +899,11 @@ public function testExecuteSucceedsIfLockerIsLockedAndLockerCouldBeUpdatedAfterN true ))); + $formatted = \json_encode( + \json_decode($normalized), + JSON_PRETTY_PRINT + ); + $composerFile = $this->pathToComposerFileWithContent($original); $io = $this->prophesize(IO\ConsoleIO::class); @@ -597,7 +970,30 @@ public function testExecuteSucceedsIfLockerIsLockedAndLockerCouldBeUpdatedAfterN ->shouldBeCalled() ->willReturn($normalized); - $command = new NormalizeCommand($normalizer->reveal()); + $format = $this->prophesize(Normalizer\Format\FormatInterface::class); + + $sniffer = $this->prophesize(Normalizer\Format\SnifferInterface::class); + + $sniffer + ->sniff(Argument::is($original)) + ->shouldBeCalled() + ->willReturn($format); + + $formatter = $this->prophesize(Normalizer\Format\FormatterInterface::class); + + $formatter + ->format( + Argument::is($normalized), + Argument::is($format->reveal()) + ) + ->shouldBeCalled() + ->willReturn($formatted); + + $command = new NormalizeCommand( + $normalizer->reveal(), + $sniffer->reveal(), + $formatter->reveal() + ); $command->setIO($io->reveal()); $command->setComposer($composer->reveal()); @@ -609,7 +1005,7 @@ public function testExecuteSucceedsIfLockerIsLockedAndLockerCouldBeUpdatedAfterN $this->assertSame(0, $tester->getStatusCode()); $this->assertFileExists($composerFile); - $this->assertStringEqualsFile($composerFile, $normalized); + $this->assertStringEqualsFile($composerFile, $formatted); } public function testExecuteSucceedsIfLockerIsLockedButSkipsUpdatingLockerIfNoUpdateLockOptionIsUsed(): void @@ -621,6 +1017,11 @@ public function testExecuteSucceedsIfLockerIsLockedButSkipsUpdatingLockerIfNoUpd true ))); + $formatted = \json_encode( + \json_decode($normalized), + JSON_PRETTY_PRINT + ); + $composerFile = $this->pathToComposerFileWithContent($original); $io = $this->prophesize(IO\ConsoleIO::class); @@ -674,7 +1075,30 @@ public function testExecuteSucceedsIfLockerIsLockedButSkipsUpdatingLockerIfNoUpd ->shouldBeCalled() ->willReturn($normalized); - $command = new NormalizeCommand($normalizer->reveal()); + $format = $this->prophesize(Normalizer\Format\FormatInterface::class); + + $sniffer = $this->prophesize(Normalizer\Format\SnifferInterface::class); + + $sniffer + ->sniff(Argument::is($original)) + ->shouldBeCalled() + ->willReturn($format); + + $formatter = $this->prophesize(Normalizer\Format\FormatterInterface::class); + + $formatter + ->format( + Argument::is($normalized), + Argument::is($format->reveal()) + ) + ->shouldBeCalled() + ->willReturn($formatted); + + $command = new NormalizeCommand( + $normalizer->reveal(), + $sniffer->reveal(), + $formatter->reveal() + ); $command->setIO($io->reveal()); $command->setComposer($composer->reveal()); @@ -688,7 +1112,7 @@ public function testExecuteSucceedsIfLockerIsLockedButSkipsUpdatingLockerIfNoUpd $this->assertSame(0, $tester->getStatusCode()); $this->assertFileExists($composerFile); - $this->assertStringEqualsFile($composerFile, $normalized); + $this->assertStringEqualsFile($composerFile, $formatted); } private function composerFileContent(): string @@ -791,4 +1215,15 @@ private function createDefinitionMock(): Console\Input\InputDefinition return $definition->reveal(); } + + /** + * @return array + */ + private function indentStyles(): array + { + return [ + 'space' => ' ', + 'tab' => "\t", + ]; + } } diff --git a/test/Unit/Normalizer/ComposerJsonNormalizerTest.php b/test/Unit/Normalizer/ComposerJsonNormalizerTest.php index 8f89dcd0..57ee716c 100644 --- a/test/Unit/Normalizer/ComposerJsonNormalizerTest.php +++ b/test/Unit/Normalizer/ComposerJsonNormalizerTest.php @@ -18,7 +18,6 @@ use Localheinz\Composer\Normalize\Normalizer\ConfigHashNormalizer; use Localheinz\Composer\Normalize\Normalizer\PackageHashNormalizer; use Localheinz\Composer\Normalize\Normalizer\VersionConstraintNormalizer; -use Localheinz\Json\Normalizer\AutoFormatNormalizer; use Localheinz\Json\Normalizer\ChainNormalizer; use Localheinz\Json\Normalizer\NormalizerInterface; use Localheinz\Json\Normalizer\SchemaNormalizer; @@ -29,13 +28,9 @@ public function testComposesNormalizers(): void { $normalizer = new ComposerJsonNormalizer(); - $this->assertComposesNormalizer(AutoFormatNormalizer::class, $normalizer); + $this->assertComposesNormalizer(ChainNormalizer::class, $normalizer); - $autoFormatNormalizer = $this->composedNormalizer($normalizer); - - $this->assertComposesNormalizer(ChainNormalizer::class, $autoFormatNormalizer); - - $chainNormalizer = $this->composedNormalizer($autoFormatNormalizer); + $chainNormalizer = $this->composedNormalizer($normalizer); $normalizerClassNames = [ SchemaNormalizer::class, @@ -178,7 +173,9 @@ public function testNormalizeNormalizes(): void \realpath(__DIR__ . '/../../Fixture/composer-schema.json') )); - $this->assertSame($normalized, $normalizer->normalize($json)); + $normalizedNotPretty = \json_encode(\json_decode($normalized)); + + $this->assertSame($normalizedNotPretty, $normalizer->normalize($json)); } private function assertComposesNormalizer(string $className, NormalizerInterface $normalizer): void