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

Fix: Do not repeat file validation #50

Merged
merged 1 commit into from
Feb 18, 2018
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
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