Skip to content

Commit

Permalink
Merge pull request #50 from localheinz/fix/repeat
Browse files Browse the repository at this point in the history
Fix: Do not repeat file validation
  • Loading branch information
localheinz authored Feb 18, 2018
2 parents 88f6969 + c46b4b6 commit f6b1528
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 77 deletions.
54 changes: 15 additions & 39 deletions src/Command/NormalizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,14 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
return 1;
}

$composerFile = Factory::getComposerFile();

try {
$composerFile = $this->composerFile();
} catch (\RuntimeException $exception) {
$composer = $this->factory->createComposer(
$io,
$composerFile
);
} catch (\Exception $exception) {
$io->writeError(\sprintf(
'<error>%s</error>',
$exception->getMessage()
Expand All @@ -132,10 +137,14 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
return 1;
}

$composer = $this->factory->createComposer(
$io,
$composerFile
);
if (!\is_writable($composerFile)) {
$io->writeError(\sprintf(
'<error>%s is not writable.</error>',
$composerFile
));

return 1;
}

$locker = $composer->getLocker();

Expand Down Expand Up @@ -275,39 +284,6 @@ private function indentFrom(Console\Input\InputInterface $input): ?string
);
}

/**
* @throws \RuntimeException
*
* @return string
*/
private function composerFile(): string
{
$composerFile = Factory::getComposerFile();

if (!\file_exists($composerFile)) {
throw new \RuntimeException(\sprintf(
'%s not found.',
$composerFile
));
}

if (!\is_readable($composerFile)) {
throw new \RuntimeException(\sprintf(
'%s is not readable.',
$composerFile
));
}

if (!\is_writable($composerFile)) {
throw new \RuntimeException(\sprintf(
'%s is not writable.',
$composerFile
));
}

return $composerFile;
}

/**
* @param string $before
* @param string $after
Expand Down
65 changes: 27 additions & 38 deletions test/Unit/Command/NormalizeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,52 +318,33 @@ public function testExecuteWithIndentFailsIfIndentStyleIsInvalid(): void
$this->assertStringEqualsFile($composerFile, $original);
}

public function testExecuteFailsIfComposerFileDoesNotExist(): void
public function testExecuteFailsIfCreatingComposerFails(): void
{
$exceptionMessage = $this->faker()->sentence;

$composerFile = $this->pathToNonExistentComposerFile();

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

$io
->writeError(Argument::is(\sprintf(
'<error>%s not found.</error>',
$composerFile
'<error>%s</error>',
$exceptionMessage
)))
->shouldBeCalled();

$command = new NormalizeCommand(
$this->prophesize(Factory::class)->reveal(),
$this->prophesize(Normalizer\NormalizerInterface::class)->reveal()
);

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

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

$tester->execute([]);

$this->assertSame(1, $tester->getStatusCode());
}

public function testExecuteFailsIfComposerFileIsNotReadable(): void
{
$original = $this->composerFileContent();

$composerFile = $this->pathToComposerFileWithContent($original);

\chmod($composerFile, 0222);

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

$io
->writeError(Argument::is(\sprintf(
'<error>%s is not readable.</error>',
$composerFile
)))
->shouldBeCalled();
$factory
->createComposer(
Argument::is($io->reveal()),
Argument::is($composerFile)
)
->shouldBeCalled()
->willThrow(new \Exception($exceptionMessage));

$command = new NormalizeCommand(
$this->prophesize(Factory::class)->reveal(),
$factory->reveal(),
$this->prophesize(Normalizer\NormalizerInterface::class)->reveal()
);

Expand All @@ -373,11 +354,7 @@ public function testExecuteFailsIfComposerFileIsNotReadable(): void

$tester->execute([]);

\chmod($composerFile, 0666);

$this->assertSame(1, $tester->getStatusCode());
$this->assertFileExists($composerFile);
$this->assertStringEqualsFile($composerFile, $original);
}

public function testExecuteFailsIfComposerFileIsNotWritable(): void
Expand All @@ -397,8 +374,20 @@ public function testExecuteFailsIfComposerFileIsNotWritable(): void
)))
->shouldBeCalled();

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

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

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

$command = new NormalizeCommand(
$this->prophesize(Factory::class)->reveal(),
$factory->reveal(),
$this->prophesize(Normalizer\NormalizerInterface::class)->reveal()
);

Expand Down

0 comments on commit f6b1528

Please sign in to comment.