diff --git a/Tests/Repository/Vcs/PerforceDriverTest.php b/Tests/Repository/Vcs/PerforceDriverTest.php index 936b9651..d8018300 100644 --- a/Tests/Repository/Vcs/PerforceDriverTest.php +++ b/Tests/Repository/Vcs/PerforceDriverTest.php @@ -40,6 +40,16 @@ protected function setUp() $this->overrideDriverInternalPerforce($this->perforce); } + protected function getMockIOInterface() + { + return $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); + } + + protected function getMockProcessExecutor() + { + return $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); + } + public function testInitializeCapturesVariablesFromRepoConfig() { $driver = new PerforceDriver($this->repoConfig, $this->io, $this->config, $this->process, $this->remoteFileSystem); @@ -139,8 +149,6 @@ protected function getMockPerforce() { $methods = array('p4login', 'checkStream', 'writeP4ClientSpec', 'connectClient', 'getComposerInformation', 'cleanupClientSpec'); - return $this->getMockBuilder('Fxp\Composer\AssetPlugin\Util\Perforce', $methods) - ->disableOriginalConstructor() - ->getMock(); + return $this->createPartialMock('Fxp\Composer\AssetPlugin\Util\Perforce', $methods); } } diff --git a/Tests/Util/PerforceTest.php b/Tests/Util/PerforceTest.php index 0d15babd..269f0bec 100644 --- a/Tests/Util/PerforceTest.php +++ b/Tests/Util/PerforceTest.php @@ -11,6 +11,7 @@ namespace Fxp\Composer\AssetPlugin\Tests\Util; +use Composer\IO\IOInterface; use Composer\Test\Util\PerforceTest as BasePerforceTest; use Composer\Util\Filesystem; use Composer\Util\ProcessExecutor; @@ -38,6 +39,14 @@ class PerforceTest extends BasePerforceTest */ protected $repoConfig; + protected function setUp() + { + $this->processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); + $this->repoConfig = $this->getTestRepoConfig(); + $this->io = $this->getMockIOInterface(); + $this->createNewPerforceWithWindowsFlag(true); + } + protected function tearDown() { parent::tearDown(); @@ -46,6 +55,14 @@ protected function tearDown() $fs->remove($this::TEST_PATH); } + /** + * @return IOInterface|\PHPUnit_Framework_MockObject_MockObject + */ + public function getMockIOInterface() + { + return $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); + } + public function testQueryP4PasswordWithPasswordAlreadySet() { $repoConfig = array( @@ -249,6 +266,58 @@ function ($command, &$output) { $this->assertSame('', $result); } + public function testCheckServerExists() + { + /* @var ProcessExecutor|\PHPUnit_Framework_MockObject_MockObject $processExecutor */ + $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); + + $expectedCommand = 'p4 -p perforce.does.exist:port info -s'; + $processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedCommand), $this->equalTo(null)) + ->will($this->returnValue(0)); + + $result = $this->perforce->checkServerExists('perforce.does.exist:port', $processExecutor); + $this->assertTrue($result); + } + + /** + * Test if "p4" command is missing. + * + * @covers \Composer\Util\Perforce::checkServerExists + * + * @return void + */ + public function testCheckServerClientError() + { + /* @var ProcessExecutor|\PHPUnit_Framework_MockObject_MockObject $processExecutor */ + $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); + + $expectedCommand = 'p4 -p perforce.does.exist:port info -s'; + $processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedCommand), $this->equalTo(null)) + ->will($this->returnValue(127)); + + $result = $this->perforce->checkServerExists('perforce.does.exist:port', $processExecutor); + $this->assertFalse($result); + } + + public function testCleanupClientSpecShouldDeleteClient() + { + /* @var Filesystem|\PHPUnit_Framework_MockObject_MockObject $fs */ + $fs = $this->getMockBuilder('Composer\Util\Filesystem')->getMock(); + $this->perforce->setFilesystem($fs); + + $testClient = $this->perforce->getClient(); + $expectedCommand = 'p4 -u ' . self::TEST_P4USER . ' -p ' . self::TEST_PORT . ' client -d ' . $testClient; + $this->processExecutor->expects($this->once())->method('execute')->with($this->equalTo($expectedCommand)); + + $fs->expects($this->once())->method('remove')->with($this->perforce->getP4ClientSpec()); + + $this->perforce->cleanupClientSpec(); + } + protected function createNewPerforceWithWindowsFlag($flag) { $this->perforce = new Perforce($this->repoConfig, self::TEST_PORT, self::TEST_PATH, $this->processExecutor, $flag, $this->io);