Skip to content

Commit

Permalink
Merge pull request #12 from localheinz/fix/lock
Browse files Browse the repository at this point in the history
Fix: Update locker after success message
  • Loading branch information
localheinz committed Jan 14, 2018
2 parents b7a1641 + c020045 commit d45da47
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 121 deletions.
15 changes: 5 additions & 10 deletions src/Command/NormalizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<error>Successfully normalized %s, but could not update lock file.</error>',
$file
));

return 1;
}

$io->write(\sprintf(
'<info>Successfully normalized %s.</info>',
$file
));

return 0;
if (!$locker->isLocked()) {
return 0;
}

return $this->updateLocker();
}

private function updateLocker(): int
Expand Down
128 changes: 17 additions & 111 deletions test/Unit/Command/NormalizeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -446,7 +445,7 @@ public function testExecuteSucceedsIfLockerIsNotLockedAndComposerFileWasNormaliz
$this->assertStringEqualsFile($composerFile, $normalized);
}

public function testExecuteFailsIfLockerIsLockedAndFreshButLockerCouldNotBeUpdatedAfterNormalization()
public function testExecuteSucceedsIfLockerIsLockedAndLockerCouldBeUpdatedAfterNormalization()
{
$original = $this->composerFileContent();

Expand All @@ -460,8 +459,8 @@ public function testExecuteFailsIfLockerIsLockedAndFreshButLockerCouldNotBeUpdat
$io = $this->prophesize(IO\ConsoleIO::class);

$io
->writeError(Argument::is(\sprintf(
'<error>Successfully normalized %s, but could not update lock file.</error>',
->write(Argument::is(\sprintf(
'<info>Successfully normalized %s.</info>',
$composerFile
)))
->shouldBeCalled();
Expand All @@ -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(
'<info>Successfully normalized %s.</info>',
$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);

Expand All @@ -581,7 +514,7 @@ public function testExecuteSucceedsIfLockerIsLockedAndLockerCouldBeUpdatedAfterN
$application
->getDefinition()
->shouldBeCalled()
->willReturn($this->createDefinitionProphecy());
->willReturn($definition);

$application
->run(
Expand Down Expand Up @@ -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;
}
}

0 comments on commit d45da47

Please sign in to comment.