diff --git a/src/Command/NormalizeCommand.php b/src/Command/NormalizeCommand.php
index 5496bffc..d4b17738 100644
--- a/src/Command/NormalizeCommand.php
+++ b/src/Command/NormalizeCommand.php
@@ -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(
'%s',
$exception->getMessage()
@@ -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(
+ '%s is not writable.',
+ $composerFile
+ ));
+
+ return 1;
+ }
$locker = $composer->getLocker();
@@ -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
diff --git a/test/Unit/Command/NormalizeCommandTest.php b/test/Unit/Command/NormalizeCommandTest.php
index dfb08bb0..b4dfccf5 100644
--- a/test/Unit/Command/NormalizeCommandTest.php
+++ b/test/Unit/Command/NormalizeCommandTest.php
@@ -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(
- '%s not found.',
- $composerFile
+ '%s',
+ $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(
- '%s is not readable.',
- $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()
);
@@ -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
@@ -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()
);