diff --git a/composer.json b/composer.json index 0f6fe8b..2a24815 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,8 @@ }, "autoload": { "psr-4": { - "AOE\\Tagging\\": "src/" + "AOE\\Tagging\\": "src/", + "AOE\\Tagging\\Tests\\": "test /" } }, "bin": [ diff --git a/src/Command/GitCommand.php b/src/Command/GitCommand.php index 4c19157..42a42dc 100644 --- a/src/Command/GitCommand.php +++ b/src/Command/GitCommand.php @@ -176,7 +176,7 @@ private function getLatestVersion(InputInterface $input, GitDriver $git) } } - /** + /** * @param string $url * @return GitDriver */ diff --git a/src/Vcs/Driver/GitDriver.php b/src/Vcs/Driver/GitDriver.php index 2b0445b..9a00fd1 100644 --- a/src/Vcs/Driver/GitDriver.php +++ b/src/Vcs/Driver/GitDriver.php @@ -60,8 +60,8 @@ public function tag($tag, $branch, $path) } /** - * @param $branch - * @param $path + * @param string $branch + * @param string $path * @param OutputInterface $output * @throws \Exception */ diff --git a/test/Command/GitCommandTest.php b/test/Command/GitCommandTest.php index f94d451..c0700fa 100644 --- a/test/Command/GitCommandTest.php +++ b/test/Command/GitCommandTest.php @@ -2,6 +2,8 @@ namespace AOE\Tagging\Tests\Command; use AOE\Tagging\Command\GitCommand; +use AOE\Tagging\Tests\TaggingPHPUnitTestCase; +use AOE\Tagging\Vcs\Driver\GitDriver; use Symfony\Component\Console\Application; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Tester\CommandTester; @@ -9,7 +11,7 @@ /** * @package AOE\Tagging\Tests\Vcs */ -class GitCommandTest extends \PHPUnit_Framework_TestCase +class GitCommandTest extends TaggingPHPUnitTestCase { /** * @test @@ -293,6 +295,43 @@ public function shouldCommitAdditionalFileWithMessage() ); } + /** + * @test + */ + public function shouldWriteCommitAndPushInfoWithVerbosityVerbose() + { + $gitDriver = $this->getMockBuilder('AOE\\Tagging\\Vcs\\Driver\\GitDriver') + ->disableOriginalConstructor() + ->setMethods(array('getLatestTag', 'tag', 'hasChangesSinceTag', 'commit')) + ->getMock(); + $gitDriver->expects($this->once())->method('hasChangesSinceTag')->will($this->returnValue(true)); + $gitDriver->expects($this->once())->method('getLatestTag')->will($this->returnValue('2.7.3')); + $gitDriver->expects($this->once())->method('commit') + ->with(array('myfile.ext'), '/home/foo/bar', 'my message for commit'); + $gitCommand = $this->getMockBuilder('AOE\\Tagging\\Command\\GitCommand') + ->setMethods(array('getDriver')) + ->getMock(); + $gitCommand->expects($this->once())->method('getDriver')->will($this->returnValue($gitDriver)); + + /** @var GitCommand $gitCommand */ + $application = new Application(); + $application->add($gitCommand); + + $command = $application->find('git'); + $commandTester = new CommandTester($command); + $test = $commandTester->getOutput(); + $commandTester->execute( + array( + 'command' => $command->getName(), + 'url' => 'git@git.test.test/foo/bar', + 'path' => '/home/foo/bar', + '--commit-and-push' => array('myfile.ext'), + '--message' => 'my message for commit' + ), + ['verbosity' => 2] + ); + } + /** * @test */ @@ -375,4 +414,47 @@ public function shouldEvaluateVersionNumberIfNoChangesDetected() $this->assertEmpty($commandTester->getDisplay()); } + + /** + * @test + */ + public function shouldCheckoutBranchWithSwitchBranchOptionWhenExecuting() + { + $gitDriver = $this->getMockBuilder('AOE\\Tagging\\Vcs\\Driver\\GitDriver') + ->disableOriginalConstructor() + ->setMethods(array('checkoutBranch')) + ->getMock(); + + $gitDriver->expects($this->once())->method('checkoutBranch'); + + $gitCommand = $this->getMockBuilder('AOE\\Tagging\\Command\\GitCommand') + ->setMethods(array('getDriver')) + ->getMock(); + $gitCommand->expects($this->once())->method('getDriver')->will($this->returnValue($gitDriver)); + + /** @var GitCommand $gitCommand */ + $application = new Application(); + $application->add($gitCommand); + + $command = $application->find('git'); + $commandTester = new CommandTester($command); + $commandTester->execute( + array( + 'command' => $command->getName(), + 'url' => 'git@git.test.test/foo/bar', + 'path' => '/home/foo/bar', + '--switch-branch' => null + ) + ); + } + + /** + * @test + */ + public function shouldGetDriver() + { + $gitCommand = new GitCommand(); + $driver = $this->invokeMethod($gitCommand, 'getDriver',['https://url']); + $this->assertInstanceOf(GitDriver::class, $driver); + } } diff --git a/test/TaggingPHPUnitTestCase.php b/test/TaggingPHPUnitTestCase.php new file mode 100644 index 0000000..29f35ba --- /dev/null +++ b/test/TaggingPHPUnitTestCase.php @@ -0,0 +1,23 @@ +getMethod($methodName); + $method->setAccessible(true); + + return $method->invokeArgs($object, $parameters); + } +} \ No newline at end of file diff --git a/test/Vcs/Driver/GitDriverTest.php b/test/Vcs/Driver/GitDriverTest.php index 82e3c95..d1b3fc3 100644 --- a/test/Vcs/Driver/GitDriverTest.php +++ b/test/Vcs/Driver/GitDriverTest.php @@ -1,6 +1,7 @@ givenAnAdapter(); - $output = $this->getMockBuilder('Symfony\\Component\\Console\\Output\\OutputInterface') - ->disableOriginalConstructor() - ->getMock(); $adapter->expects($this->at(0))->method('execute')->with( 'tag', @@ -41,18 +40,12 @@ public function shouldTagAndPush() ); $adapter->expects($this->at(2))->method('execute')->with( - 'branch', - array('myBranch', 'feature/myBranch', '-f'), - '/home/my/vcs/repo' - ); - - $adapter->expects($this->at(3))->method('execute')->with( 'push', array('origin', 'feature/myBranch'), '/home/my/vcs/repo' ); - $adapter->expects($this->at(4))->method('execute')->with( + $adapter->expects($this->at(3))->method('execute')->with( 'push', array('origin', 'tag', '0.2.5'), '/home/my/vcs/repo' @@ -66,23 +59,21 @@ public function shouldTagAndPush() $driver = $this->givenADriver(); - $driver->expects($this->any())->method('getGit')->will( + $driver->expects($this->exactly(4))->method('getGit')->will( $this->returnValue($git) ); /** @var GitDriver $driver */ - $driver->tag('0.2.5', 'feature/myBranch', '/home/my/vcs/repo', $output); + $driver->tag('0.2.5', 'feature/myBranch', '/home/my/vcs/repo'); } /** + * @test * @expectedException \Exception */ public function shouldCleanOnError() { $adapter = $this->givenAnAdapter(); - $output = $this->getMockBuilder('Symfony\\Component\\Console\\Output\\OutputInterface') - ->disableOriginalConstructor() - ->getMock(); $adapter->expects($this->at(0))->method('execute')->with( 'tag', @@ -97,24 +88,18 @@ public function shouldCleanOnError() ); $adapter->expects($this->at(2))->method('execute')->with( - 'branch', - array('myBranch', 'feature/myBranch', '-f'), - '/home/my/vcs/repo' - ); - - $adapter->expects($this->at(3))->method('execute')->with( 'push', array('origin', 'master'), '/home/my/vcs/repo' )->will($this->throwException(new \Exception('could not push to remote'))); - $adapter->expects($this->at(4))->method('execute')->with( + $adapter->expects($this->at(3))->method('execute')->with( 'reset', array('--hard'), '/home/my/vcs/repo' ); - $adapter->expects($this->at(5))->method('execute')->with( + $adapter->expects($this->at(4))->method('execute')->with( 'tag', array('-d', '0.2.5'), '/home/my/vcs/repo' @@ -124,16 +109,16 @@ public function shouldCleanOnError() ->disableOriginalConstructor() ->setMethods(array('getAdapter')) ->getMock(); - $git->expects($this->exactly(6))->method('getAdapter')->will($this->returnValue($adapter)); + $git->expects($this->exactly(5))->method('getAdapter')->will($this->returnValue($adapter)); $driver = $this->givenADriver(); - $driver->expects($this->exactly(6))->method('getGit')->will( + $driver->expects($this->exactly(5))->method('getGit')->will( $this->returnValue($git) ); /** @var GitDriver $driver */ - $driver->tag('0.2.5', 'master', '/home/my/vcs/repo', $output); + $driver->tag('0.2.5', 'master', '/home/my/vcs/repo'); } /** @@ -181,6 +166,7 @@ public function shouldAbortRebaseOnPullError() } /** + * @test */ public function shouldNotHaveChangesSinceTagWithNullValueFromGitAdapter() { @@ -196,14 +182,8 @@ public function shouldNotHaveChangesSinceTagWithNullValueFromGitAdapter() ); $adapter->expects($this->at(1))->method('execute')->with( - 'branch', - array('master', 'origin/master', '-f'), - '/home/my/vcs/repo' - ); - - $adapter->expects($this->at(2))->method('execute')->with( 'diff', - array('--ignore-all-space', '0.2.5'), + array('--ignore-all-space', '0.2.5', 'master'), '/home/my/vcs/repo' )->will($this->returnValue(null)); @@ -211,7 +191,7 @@ public function shouldNotHaveChangesSinceTagWithNullValueFromGitAdapter() ->disableOriginalConstructor() ->setMethods(array('getAdapter')) ->getMock(); - $git->expects($this->any())->method('getAdapter')->will($this->returnValue($adapter)); + $git->expects($this->exactly(2))->method('getAdapter')->will($this->returnValue($adapter)); $driver = $this->givenADriver(); @@ -224,6 +204,7 @@ public function shouldNotHaveChangesSinceTagWithNullValueFromGitAdapter() } /** + * @test */ public function shouldNotHaveChangesSinceTagWithEmptyStringValueFromGitAdapter() { @@ -239,14 +220,8 @@ public function shouldNotHaveChangesSinceTagWithEmptyStringValueFromGitAdapter() ); $adapter->expects($this->at(1))->method('execute')->with( - 'branch', - array('master', 'origin/master', '-f'), - '/home/my/vcs/repo' - ); - - $adapter->expects($this->at(2))->method('execute')->with( 'diff', - array('--ignore-all-space', '0.2.5'), + array('--ignore-all-space', '0.2.5', 'master'), '/home/my/vcs/repo' )->will($this->returnValue("")); @@ -258,7 +233,7 @@ public function shouldNotHaveChangesSinceTagWithEmptyStringValueFromGitAdapter() $driver = $this->givenADriver(); - $driver->expects($this->any())->method('getGit')->will( + $driver->expects($this->exactly(2))->method('getGit')->will( $this->returnValue($git) ); @@ -268,6 +243,7 @@ public function shouldNotHaveChangesSinceTagWithEmptyStringValueFromGitAdapter() /** + * @test */ public function shouldHaveChangesSinceTag() { @@ -283,14 +259,8 @@ public function shouldHaveChangesSinceTag() ); $adapter->expects($this->at(1))->method('execute')->with( - 'branch', - array('master', 'origin/master', '-f'), - '/home/my/vcs/repo' - ); - - $adapter->expects($this->at(2))->method('execute')->with( 'diff', - array('--ignore-all-space', '0.2.5'), + array('--ignore-all-space', '0.2.5', 'master'), '/home/my/vcs/repo' )->will($this->returnValue('diff --git a/TEST b/TEST index 56a6051..d2b3621 100644 @@ -306,7 +276,7 @@ public function shouldHaveChangesSinceTag() ->disableOriginalConstructor() ->setMethods(array('getAdapter')) ->getMock(); - $git->expects($this->any())->method('getAdapter')->will($this->returnValue($adapter)); + $git->expects($this->exactly(2))->method('getAdapter')->will($this->returnValue($adapter)); $driver = $this->givenADriver(); @@ -319,6 +289,7 @@ public function shouldHaveChangesSinceTag() } /** + * @test */ public function shouldHaveChangesSinceTagOnUnknownTag() { @@ -334,14 +305,8 @@ public function shouldHaveChangesSinceTagOnUnknownTag() ); $adapter->expects($this->at(1))->method('execute')->with( - 'branch', - array('master', 'origin/master', '-f'), - '/home/my/vcs/repo' - ); - - $adapter->expects($this->at(2))->method('execute')->with( 'diff', - array('--ignore-all-space', '0.2.5'), + array('--ignore-all-space', '0.2.5', 'master'), '/home/my/vcs/repo' )->will($this->throwException( new \RuntimeException('ambiguous argument \'0.0.0\': unknown revision or path not in the working tree.') @@ -355,7 +320,7 @@ public function shouldHaveChangesSinceTagOnUnknownTag() $driver = $this->givenADriver(); - $driver->expects($this->any())->method('getGit')->will( + $driver->expects($this->exactly(2))->method('getGit')->will( $this->returnValue($git) ); @@ -364,6 +329,48 @@ public function shouldHaveChangesSinceTagOnUnknownTag() } /** + * @test + * @expectedException \Exception + */ + public function shouldThrowRuntimeExceptionOnDiffFailureHavingChangesSinceTag() + { + $adapter = $this->givenAnAdapter(); + $output = $this->getMockBuilder('Symfony\\Component\\Console\\Output\\OutputInterface') + ->disableOriginalConstructor() + ->getMock(); + + $exception = new \RuntimeException('some error message'); + + $adapter->expects($this->at(0))->method('execute')->with( + 'fetch', + array('origin'), + '/home/my/vcs/repo' + ); + + $adapter->expects($this->at(1))->method('execute')->with( + 'diff', + array('--ignore-all-space', '0.2.5', 'master'), + '/home/my/vcs/repo' + )->will($this->throwException($exception)); + + $git = $this->getMockBuilder('Webcreate\\Vcs\\Git') + ->disableOriginalConstructor() + ->setMethods(array('getAdapter')) + ->getMock(); + $git->expects($this->any())->method('getAdapter')->will($this->returnValue($adapter)); + + $driver = $this->givenADriver(); + + $driver->expects($this->exactly(2))->method('getGit')->will( + $this->returnValue($git) + ); + + /** @var GitDriver $driver */ + $this->assertTrue($driver->hasChangesSinceTag('0.2.5', 'master', '/home/my/vcs/repo', $output)); + } + + /** + * @test * @param array $references * @dataProvider references */ @@ -532,6 +539,125 @@ public function shouldThrowException() $driver->commit(array('myfile.ext'), '/home/my/vcs/repo', 'my message'); } + /** + * @test + */ + public function shouldCheckoutBranch() + { + $adapter = $this->givenAnAdapter(); + $adapter->expects($this->at(0))->method('execute')->with( + 'checkout', + array('-b', 'bugfix/branch', 'origin/bugfix/branch'), + '/home/my/vcs/repo' + ); + + $git = $this->getMockBuilder('Webcreate\\Vcs\\Git') + ->disableOriginalConstructor() + ->setMethods(array('getAdapter')) + ->getMock(); + $git->expects($this->any())->method('getAdapter')->will($this->returnValue($adapter)); + + $git = $this->givenAGitClient($adapter); + + $driver = $this->givenADriver(); + + $driver->expects($this->exactly(1))->method('getGit')->will( + $this->returnValue($git) + ); + + $output = $this->getMockBuilder('Symfony\\Component\\Console\\Output\\OutputInterface') + ->disableOriginalConstructor() + ->getMock(); + + $driver->checkoutBranch('bugfix/branch', '/home/my/vcs/repo', $output); + } + + /** + * @test + */ + public function shouldCheckoutLocalBranch() + { + $adapter = $this->givenAnAdapter(); + + $adapter->expects($this->at(0))->method('execute')->with( + 'checkout', + array('-b', 'bugfix/branch', 'origin/bugfix/branch'), + '/home/my/vcs/repo' + )->willThrowException(new \Exception("branch bugfix/branch already exists")); + + $adapter->expects($this->at(1))->method('execute')->with( + 'checkout', + array('bugfix/branch'), + '/home/my/vcs/repo' + ); + + $git = $this->getMockBuilder('Webcreate\\Vcs\\Git') + ->disableOriginalConstructor() + ->setMethods(array('getAdapter')) + ->getMock(); + $git->expects($this->any())->method('getAdapter')->will($this->returnValue($adapter)); + + $git = $this->givenAGitClient($adapter); + + $driver = $this->givenADriver(); + + $driver->expects($this->exactly(2))->method('getGit')->will( + $this->returnValue($git) + ); + + $output = $this->getMockBuilder('Symfony\\Component\\Console\\Output\\OutputInterface') + ->disableOriginalConstructor() + ->getMock(); + + $driver->checkoutBranch('bugfix/branch', '/home/my/vcs/repo', $output); + } + + /** + * @test + * @expectedException \Exception + */ + public function shouldThrowExceptionWhileCheckingOutLocalBranch() + { + $adapter = $this->givenAnAdapter(); + + $exception = new \Exception("some exception"); + + $adapter->expects($this->at(0))->method('execute')->with( + 'checkout', + array('-b', 'bugfix/branch', 'origin/bugfix/branch'), + '/home/my/vcs/repo' + )->willThrowException($exception); + + $git = $this->getMockBuilder('Webcreate\\Vcs\\Git') + ->disableOriginalConstructor() + ->setMethods(array('getAdapter')) + ->getMock(); + $git->expects($this->any())->method('getAdapter')->will($this->returnValue($adapter)); + + $git = $this->givenAGitClient($adapter); + + $driver = $this->givenADriver(); + + $driver->expects($this->exactly(1))->method('getGit')->will( + $this->returnValue($git) + ); + + $output = $this->getMockBuilder('Symfony\\Component\\Console\\Output\\OutputInterface') + ->disableOriginalConstructor() + ->getMock(); + + $driver->checkoutBranch('bugfix/branch', '/home/my/vcs/repo', $output); + } + + /** + * @test + */ + public function shouldGetGit() + { + $driver = new GitDriver('https://url', 'git'); + $this->invokeMethod($driver, 'getGit'); + } + /** * @return array */ @@ -592,7 +718,7 @@ private function givenADriver() { $driver = $this->getMockBuilder('AOE\\Tagging\\Vcs\\Driver\\GitDriver') ->disableOriginalConstructor() - ->setMethods(array('getGit', 'checkoutBranch')) + ->setMethods(array('getGit')) ->getMock(); return $driver; } @@ -607,7 +733,7 @@ private function givenAGitClient($adapter) ->disableOriginalConstructor() ->setMethods(array('getAdapter')) ->getMock(); - $git->expects($this->exactly(2))->method('getAdapter')->will($this->returnValue($adapter)); + $git->expects($this->any())->method('getAdapter')->will($this->returnValue($adapter)); return $git; } }