Skip to content

Commit

Permalink
Merge pull request #51 from localheinz/feature/file-argument
Browse files Browse the repository at this point in the history
Enhancement: Add file argument
  • Loading branch information
localheinz authored Feb 18, 2018
2 parents f6b1528 + f7e9486 commit 13b6873
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 18 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ The `NormalizeCommand` provided by the `NormalizePlugin` within this package wil
* 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

### Arguments

* `file`: Path to composer.json file (optional, defaults to `composer.json` in working directory)

### Options

* `--dry-run`: Show the results of normalizing, but do not modify any files
Expand Down
11 changes: 10 additions & 1 deletion src/Command/NormalizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ protected function configure(): void
{
$this->setDescription('Normalizes composer.json according to its JSON schema (https://getcomposer.org/schema.json).');
$this->setDefinition([
new Console\Input\InputArgument(
'file',
Console\Input\InputArgument::OPTIONAL,
'Path to composer.json file'
),
new Console\Input\InputOption(
'dry-run',
null,
Expand Down Expand Up @@ -121,7 +126,11 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
return 1;
}

$composerFile = Factory::getComposerFile();
$composerFile = $input->getArgument('file');

if (null === $composerFile) {
$composerFile = Factory::getComposerFile();
}

try {
$composer = $this->factory->createComposer(
Expand Down
191 changes: 174 additions & 17 deletions test/Unit/Command/NormalizeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testHasNameAndDescription(): void
$this->assertSame('Normalizes composer.json according to its JSON schema (https://getcomposer.org/schema.json).', $command->getDescription());
}

public function testHasNoArguments(): void
public function testHasFileArgument(): void
{
$command = new NormalizeCommand(
$this->prophesize(Factory::class)->reveal(),
Expand All @@ -71,7 +71,13 @@ public function testHasNoArguments(): void

$definition = $command->getDefinition();

$this->assertCount(0, $definition->getArguments());
$this->assertTrue($definition->hasArgument('file'));

$argument = $definition->getArgument('file');

$this->assertFalse($argument->isRequired());
$this->assertSame('Path to composer.json file', $argument->getDescription());
$this->assertNull($argument->getDefault());
}

public function testHasDryRunOption(): void
Expand Down Expand Up @@ -181,6 +187,7 @@ public function testExecuteWithIndentFailsIfIndentStyleOptionIsNotUsed(): void
$tester = new Console\Tester\CommandTester($command);

$tester->execute([
'file' => $composerFile,
'--indent-size' => $this->faker()->numberBetween(1),
]);

Expand Down Expand Up @@ -211,6 +218,7 @@ public function testExecuteWithIndentFailsIfIndentSizeOptionIsNotUsed(): void
$tester = new Console\Tester\CommandTester($command);

$tester->execute([
'file' => $composerFile,
'--indent-style' => $this->faker()->randomElement([
'space',
'tab',
Expand Down Expand Up @@ -254,6 +262,7 @@ public function testExecuteWithIndentFailsIfIndentSizeIsInvalid($indentSize): vo
$tester = new Console\Tester\CommandTester($command);

$tester->execute([
'file' => $composerFile,
'--indent-size' => $indentSize,
'--indent-style' => $indentStyle,
]);
Expand Down Expand Up @@ -309,6 +318,7 @@ public function testExecuteWithIndentFailsIfIndentStyleIsInvalid(): void
$tester = new Console\Tester\CommandTester($command);

$tester->execute([
'file' => $composerFile,
'--indent-size' => $indentSize,
'--indent-style' => $indentStyle,
]);
Expand Down Expand Up @@ -352,7 +362,9 @@ public function testExecuteFailsIfCreatingComposerFails(): void

$tester = new Console\Tester\CommandTester($command);

$tester->execute([]);
$tester->execute([
'file' => $composerFile,
]);

$this->assertSame(1, $tester->getStatusCode());
}
Expand Down Expand Up @@ -395,7 +407,9 @@ public function testExecuteFailsIfComposerFileIsNotWritable(): void

$tester = new Console\Tester\CommandTester($command);

$tester->execute([]);
$tester->execute([
'file' => $composerFile,
]);

\chmod($composerFile, 0666);

Expand Down Expand Up @@ -454,7 +468,9 @@ public function testExecuteFailsIfLockerIsLockedButNotFresh(): void

$tester = new Console\Tester\CommandTester($command);

$tester->execute([]);
$tester->execute([
'file' => $composerFile,
]);

$this->assertSame(1, $tester->getStatusCode());
$this->assertFileExists($composerFile);
Expand Down Expand Up @@ -553,7 +569,9 @@ public function testExecuteFailsIfNormalizerThrowsInvalidArgumentException(): vo

$tester = new Console\Tester\CommandTester($command);

$tester->execute([]);
$tester->execute([
'file' => $composerFile,
]);

$this->assertSame($exitCode, $tester->getStatusCode());
$this->assertFileExists($composerFile);
Expand Down Expand Up @@ -622,7 +640,9 @@ public function testExecuteFailsIfNormalizerThrowsRuntimeException(): void

$tester = new Console\Tester\CommandTester($command);

$tester->execute([]);
$tester->execute([
'file' => $composerFile,
]);

$this->assertSame(1, $tester->getStatusCode());
$this->assertFileExists($composerFile);
Expand Down Expand Up @@ -707,7 +727,9 @@ public function testExecuteSucceedsIfLockerIsNotLockedAndComposerFileIsAlreadyNo

$tester = new Console\Tester\CommandTester($command);

$tester->execute([]);
$tester->execute([
'file' => $composerFile,
]);

$this->assertSame(0, $tester->getStatusCode());
$this->assertFileExists($composerFile);
Expand Down Expand Up @@ -797,7 +819,9 @@ public function testExecuteSucceedsIfLockerIsLockedAndFreshButComposerFileIsAlre

$tester = new Console\Tester\CommandTester($command);

$tester->execute([]);
$tester->execute([
'file' => $composerFile,
]);

$this->assertSame(0, $tester->getStatusCode());
$this->assertFileExists($composerFile);
Expand Down Expand Up @@ -888,6 +912,7 @@ public function testExecuteWithDryRunSucceedsIfLockerIsLockedAndFreshButComposer
$tester = new Console\Tester\CommandTester($command);

$tester->execute([
'file' => $composerFile,
'--dry-run' => null,
]);

Expand Down Expand Up @@ -982,7 +1007,9 @@ public function testExecuteSucceedsIfLockerIsNotLockedAndComposerFileWasNormaliz

$tester = new Console\Tester\CommandTester($command);

$tester->execute([]);
$tester->execute([
'file' => $composerFile,
]);

$this->assertSame(0, $tester->getStatusCode());
$this->assertFileExists($composerFile);
Expand Down Expand Up @@ -1094,6 +1121,7 @@ public function testExecuteWithDryRunFailsIfLockerIsNotLockedAndComposerFileWasN
$tester = new Console\Tester\CommandTester($command);

$tester->execute([
'file' => $composerFile,
'--dry-run' => null,
]);

Expand Down Expand Up @@ -1206,6 +1234,7 @@ public function testExecuteWithIndentSucceedsIfLockerIsNotLockedAndComposerFileW
$tester = new Console\Tester\CommandTester($command);

$tester->execute([
'file' => $composerFile,
'--indent-size' => $indentSize,
'--indent-style' => $indentStyle,
]);
Expand Down Expand Up @@ -1319,6 +1348,7 @@ public function testExecuteFailsIfLockerIsNotLockedAndComposerFileWasNormalizedS
$tester = new Console\Tester\CommandTester($command);

$tester->execute([
'file' => $composerFile,
'--indent-size' => $indentSize,
'--indent-style' => $indentStyle,
]);
Expand Down Expand Up @@ -1449,6 +1479,138 @@ public function testExecuteSucceedsIfLockerIsLockedAndLockerCouldBeUpdatedAfterN

$tester = new Console\Tester\CommandTester($command);

$tester->execute([
'file' => $composerFile,
]);

$this->assertSame(0, $tester->getStatusCode());
$this->assertFileExists($composerFile);
$this->assertStringEqualsFile($composerFile, $formatted);
}

public function testExecuteDefaultsToUsingComposerFileFromCurrentDirectory(): void
{
$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);

$this->useComposerFile($composerFile);

$io = $this->prophesize(IO\ConsoleIO::class);

$io
->write(Argument::is(\sprintf(
'<info>Successfully normalized %s.</info>',
$composerFile
)))
->shouldBeCalled();

$io
->write(Argument::is('<info>Updating lock file.</info>'))
->shouldBeCalled();

$locker = $this->prophesize(Package\Locker::class);

$locker
->isLocked()
->shouldBeCalled()
->willReturn(true);

$locker
->isFresh()
->shouldBeCalled()
->willReturn(true);

$composer = $this->prophesize(Composer::class);

$composer
->getLocker()
->shouldBeCalled()
->willReturn($locker);

$factory = $this->prophesize(Factory::class);

$factory
->createComposer(
Argument::is($io->reveal()),
Argument::is($composerFile)
)
->shouldBeCalled()
->willReturn($composer->reveal());

$application = $this->prophesize(Application::class);

$application
->getHelperSet()
->shouldBeCalled()
->willReturn(new Console\Helper\HelperSet());

$application
->getDefinition()
->shouldBeCalled()
->willReturn($this->createDefinitionMock());

$application
->run(
Argument::allOf(
Argument::type(Console\Input\StringInput::class),
Argument::that(function (Console\Input\StringInput $input) {
return 'update --lock --no-autoloader --no-plugins --no-scripts --no-suggest' === (string) $input;
})
),
Argument::type(Console\Output\OutputInterface::class)
)
->shouldBeCalled()
->willReturn(0);

$normalizer = $this->prophesize(Normalizer\NormalizerInterface::class);

$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);

$formatter = $this->prophesize(Normalizer\Format\FormatterInterface::class);

$formatter
->format(
Argument::is($normalized),
Argument::is($format->reveal())
)
->shouldBeCalled()
->willReturn($formatted);

$command = new NormalizeCommand(
$factory->reveal(),
$normalizer->reveal(),
$sniffer->reveal(),
$formatter->reveal()
);

$command->setIO($io->reveal());
$command->setApplication($application->reveal());

$tester = new Console\Tester\CommandTester($command);

$tester->execute([]);

$this->assertSame(0, $tester->getStatusCode());
Expand Down Expand Up @@ -1565,6 +1727,7 @@ public function testExecuteSucceedsIfLockerIsLockedButSkipsUpdatingLockerIfNoUpd
$tester = new Console\Tester\CommandTester($command);

$tester->execute([
'file' => $composerFile,
'--no-update-lock' => null,
]);

Expand Down Expand Up @@ -1597,8 +1760,6 @@ private function pathToComposerFileWithContent(string $content): string

\file_put_contents($composerFile, $content);

$this->useComposerFile($composerFile);

return $composerFile;
}

Expand All @@ -1609,11 +1770,7 @@ private function pathToComposerFileWithContent(string $content): string
*/
private function pathToNonExistentComposerFile(): string
{
$composerFile = $this->pathToComposerFile();

$this->useComposerFile($composerFile);

return $composerFile;
return $this->pathToComposerFile();
}

/**
Expand Down

0 comments on commit 13b6873

Please sign in to comment.