From c020045d41129e432a0ebfc5b2812c8343e3a77b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sun, 14 Jan 2018 13:04:51 +0100 Subject: [PATCH] Fix: Update locker after success message --- src/Command/NormalizeCommand.php | 15 +-- test/Unit/Command/NormalizeCommandTest.php | 128 +++------------------ 2 files changed, 22 insertions(+), 121 deletions(-) diff --git a/src/Command/NormalizeCommand.php b/src/Command/NormalizeCommand.php index cc63fb17..42e2fb73 100644 --- a/src/Command/NormalizeCommand.php +++ b/src/Command/NormalizeCommand.php @@ -111,21 +111,16 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O \file_put_contents($file, $normalized); - if ($locker->isLocked() && 0 !== $this->updateLocker()) { - $io->writeError(\sprintf( - 'Successfully normalized %s, but could not update lock file.', - $file - )); - - return 1; - } - $io->write(\sprintf( 'Successfully normalized %s.', $file )); - return 0; + if (!$locker->isLocked()) { + return 0; + } + + return $this->updateLocker(); } private function updateLocker(): int diff --git a/test/Unit/Command/NormalizeCommandTest.php b/test/Unit/Command/NormalizeCommandTest.php index 99aa6db3..614f6171 100644 --- a/test/Unit/Command/NormalizeCommandTest.php +++ b/test/Unit/Command/NormalizeCommandTest.php @@ -25,7 +25,6 @@ use org\bovigo\vfs; use PHPUnit\Framework; use Prophecy\Argument; -use Prophecy\Prophecy; use Symfony\Component\Console; final class NormalizeCommandTest extends Framework\TestCase @@ -446,7 +445,7 @@ public function testExecuteSucceedsIfLockerIsNotLockedAndComposerFileWasNormaliz $this->assertStringEqualsFile($composerFile, $normalized); } - public function testExecuteFailsIfLockerIsLockedAndFreshButLockerCouldNotBeUpdatedAfterNormalization() + public function testExecuteSucceedsIfLockerIsLockedAndLockerCouldBeUpdatedAfterNormalization() { $original = $this->composerFileContent(); @@ -460,8 +459,8 @@ public function testExecuteFailsIfLockerIsLockedAndFreshButLockerCouldNotBeUpdat $io = $this->prophesize(IO\ConsoleIO::class); $io - ->writeError(Argument::is(\sprintf( - 'Successfully normalized %s, but could not update lock file.', + ->write(Argument::is(\sprintf( + 'Successfully normalized %s.', $composerFile ))) ->shouldBeCalled(); @@ -485,91 +484,25 @@ public function testExecuteFailsIfLockerIsLockedAndFreshButLockerCouldNotBeUpdat ->shouldBeCalled() ->willReturn($locker); - $application = $this->prophesize(Application::class); - - $application - ->getHelperSet() - ->shouldBeCalled() - ->willReturn(new Console\Helper\HelperSet()); - - $application - ->getDefinition() - ->shouldBeCalled() - ->willReturn($this->createDefinitionProphecy()->reveal()); - - $application - ->run( - Argument::allOf( - Argument::type(Console\Input\StringInput::class), - Argument::that(function (Console\Input\StringInput $input) { - return 'update --lock --no-plugins' === (string) $input; - }) - ), - Argument::type(Console\Output\NullOutput::class) - ) - ->shouldBeCalled() - ->willReturn(1); - - $normalizer = $this->prophesize(Normalizer\NormalizerInterface::class); - - $normalizer - ->normalize(Argument::is($original)) - ->shouldBeCalled() - ->willReturn($normalized); - - $command = new NormalizeCommand($normalizer->reveal()); - - $command->setIO($io->reveal()); - $command->setComposer($composer->reveal()); - $command->setApplication($application->reveal()); - - $tester = new Console\Tester\CommandTester($command); - - $tester->execute([]); - - $this->assertSame(1, $tester->getStatusCode()); - $this->assertFileExists($composerFile); - $this->assertStringEqualsFile($composerFile, $normalized); - } - - public function testExecuteSucceedsIfLockerIsLockedAndLockerCouldBeUpdatedAfterNormalization() - { - $original = $this->composerFileContent(); - - $normalized = \json_encode(\array_reverse(\json_decode( - $original, - true - ))); - - $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); + /** + * @see Console\Tester\CommandTester::execute() + */ + $definition = $this->prophesize(Console\Input\InputDefinition::class); - $locker - ->isLocked() + $definition + ->hasArgument('command') ->shouldBeCalled() - ->willReturn(true); + ->willReturn(false); - $locker - ->isFresh() + $definition + ->getArguments() ->shouldBeCalled() - ->willReturn(true); - - $composer = $this->prophesize(Composer::class); + ->willReturn([]); - $composer - ->getLocker() + $definition + ->getOptions() ->shouldBeCalled() - ->willReturn($locker); + ->willReturn([]); $application = $this->prophesize(Application::class); @@ -581,7 +514,7 @@ public function testExecuteSucceedsIfLockerIsLockedAndLockerCouldBeUpdatedAfterN $application ->getDefinition() ->shouldBeCalled() - ->willReturn($this->createDefinitionProphecy()); + ->willReturn($definition); $application ->run( @@ -691,31 +624,4 @@ private function clearComposerFile() { \putenv('COMPOSER'); } - - /** - * @see Console\Tester\CommandTester::execute() - * - * @return Prophecy\ObjectProphecy - */ - private function createDefinitionProphecy(): Prophecy\ObjectProphecy - { - $definition = $this->prophesize(Console\Input\InputDefinition::class); - - $definition - ->hasArgument('command') - ->shouldBeCalled() - ->willReturn(false); - - $definition - ->getArguments() - ->shouldBeCalled() - ->willReturn([]); - - $definition - ->getOptions() - ->shouldBeCalled() - ->willReturn([]); - - return $definition; - } }