1
0
Fork 0

almost all unit tests passing after IOInterface dependency refactor. no longer passing IOInterface into any Perforce methods

pull/2834/head
Clark Stuth 2014-03-17 16:06:19 -05:00
parent 454c6eca81
commit 34a68f4020
6 changed files with 239 additions and 229 deletions

View File

@ -44,7 +44,7 @@ class PerforceDownloader extends VcsDownloader
public function initPerforce($package, $path) public function initPerforce($package, $path)
{ {
if ($this->perforce) { if (!empty($this->perforce)) {
$this->perforce->initializePath($path); $this->perforce->initializePath($path);
return; return;
} }

View File

@ -35,7 +35,7 @@ class PerforceDriver extends VcsDriver
{ {
$this->depot = $this->repoConfig['depot']; $this->depot = $this->repoConfig['depot'];
$this->branch = ''; $this->branch = '';
if (isset($this->repoConfig['branch'])) { if (!empty($this->repoConfig['branch'])) {
$this->branch = $this->repoConfig['branch']; $this->branch = $this->repoConfig['branch'];
} }
@ -51,7 +51,7 @@ class PerforceDriver extends VcsDriver
private function initPerforce($repoConfig) private function initPerforce($repoConfig)
{ {
if (isset($this->perforce)) { if (!empty($this->perforce)) {
return; return;
} }
@ -64,7 +64,7 @@ class PerforceDriver extends VcsDriver
*/ */
public function getComposerInformation($identifier) public function getComposerInformation($identifier)
{ {
if (isset($this->composerInfoIdentifier)) { if (!empty($this->composerInfoIdentifier)) {
if (strcmp($identifier, $this->composerInfoIdentifier) === 0) { if (strcmp($identifier, $this->composerInfoIdentifier) === 0) {
return $this->composerInfo; return $this->composerInfo;
} }
@ -141,7 +141,7 @@ class PerforceDriver extends VcsDriver
$this->composerInfo = $this->perforce->getComposerInformation('//' . $this->depot . '/' . $identifier); $this->composerInfo = $this->perforce->getComposerInformation('//' . $this->depot . '/' . $identifier);
$this->composerInfoIdentifier = $identifier; $this->composerInfoIdentifier = $identifier;
$result = false; $result = false;
if (isset($this->composerInfo)) { if (!empty($this->composerInfo)) {
$result = count($this->composerInfo) > 0; $result = count($this->composerInfo) > 0;
} }

View File

@ -200,7 +200,12 @@ class Perforce
return $this->p4User; return $this->p4User;
} }
public function queryP4User(IOInterface $io) public function setUser($user)
{
$this->p4User = $user;
}
public function queryP4User()
{ {
$this->getUser(); $this->getUser();
if (strlen($this->p4User) > 0) { if (strlen($this->p4User) > 0) {
@ -210,7 +215,7 @@ class Perforce
if (strlen($this->p4User) > 0) { if (strlen($this->p4User) > 0) {
return; return;
} }
$this->p4User = $io->ask('Enter P4 User:'); $this->p4User = $this->io->ask('Enter P4 User:');
if ($this->windowsFlag) { if ($this->windowsFlag) {
$command = 'p4 set P4USER=' . $this->p4User; $command = 'p4 set P4USER=' . $this->p4User;
} else { } else {
@ -248,14 +253,14 @@ class Perforce
} }
} }
public function queryP4Password(IOInterface $io) public function queryP4Password()
{ {
if (isset($this->p4Password)) { if (isset($this->p4Password)) {
return $this->p4Password; return $this->p4Password;
} }
$password = $this->getP4variable('P4PASSWD'); $password = $this->getP4variable('P4PASSWD');
if (strlen($password) <= 0) { if (strlen($password) <= 0) {
$password = $io->askAndHideAnswer('Enter password for Perforce user ' . $this->getUser() . ': '); $password = $this->io->askAndHideAnswer('Enter password for Perforce user ' . $this->getUser() . ': ');
} }
$this->p4Password = $password; $this->p4Password = $password;
@ -365,6 +370,16 @@ class Perforce
return; return;
} }
public function getWindowsFlag()
{
return $this->windowsFlag;
}
public function setWindowsFlag($flag)
{
$this->windowsFlag = $flag;
}
public function windowsLogin($password) public function windowsLogin($password)
{ {
$command = $this->generateP4Command(' login -a'); $command = $this->generateP4Command(' login -a');
@ -373,11 +388,11 @@ class Perforce
return $process->run(); return $process->run();
} }
public function p4Login(IOInterface $io) public function p4Login()
{ {
$this->queryP4User($io); $this->queryP4User();
if (!$this->isLoggedIn()) { if (!$this->isLoggedIn()) {
$password = $this->queryP4Password($io); $password = $this->queryP4Password();
if ($this->windowsFlag) { if ($this->windowsFlag) {
$this->windowsLogin($password); $this->windowsLogin($password);
} else { } else {

View File

@ -122,12 +122,13 @@ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase
$this->io->expects($this->once())->method('write')->with($this->stringContains('Cloning '.$ref)); $this->io->expects($this->once())->method('write')->with($this->stringContains('Cloning '.$ref));
$perforceMethods = array('setStream', 'p4Login', 'writeP4ClientSpec', 'connectClient', 'syncCodeBase', 'cleanupClientSpec'); $perforceMethods = array('setStream', 'p4Login', 'writeP4ClientSpec', 'connectClient', 'syncCodeBase', 'cleanupClientSpec');
$perforce = $this->getMockBuilder('Composer\Util\Perforce', $perforceMethods)->disableOriginalConstructor()->getMock(); $perforce = $this->getMockBuilder('Composer\Util\Perforce', $perforceMethods)->disableOriginalConstructor()->getMock();
$perforce->expects($this->at(0))->method('setStream')->with($this->equalTo($ref)); $perforce->expects($this->at(0))->method('initializePath')->with($this->equalTo($this->testPath));
$perforce->expects($this->at(1))->method('p4Login')->with($this->identicalTo($this->io)); $perforce->expects($this->at(1))->method('setStream')->with($this->equalTo($ref));
$perforce->expects($this->at(2))->method('writeP4ClientSpec'); $perforce->expects($this->at(2))->method('p4Login')->with($this->identicalTo($this->io));
$perforce->expects($this->at(3))->method('connectClient'); $perforce->expects($this->at(3))->method('writeP4ClientSpec');
$perforce->expects($this->at(4))->method('syncCodeBase'); $perforce->expects($this->at(4))->method('connectClient');
$perforce->expects($this->at(5))->method('cleanupClientSpec'); $perforce->expects($this->at(5))->method('syncCodeBase');
$perforce->expects($this->at(6))->method('cleanupClientSpec');
$this->downloader->setPerforce($perforce); $this->downloader->setPerforce($perforce);
$this->downloader->doDownload($this->package, $this->testPath); $this->downloader->doDownload($this->package, $this->testPath);
} }

View File

@ -21,113 +21,129 @@ use Composer\Config;
*/ */
class PerforceDriverTest extends \PHPUnit_Framework_TestCase class PerforceDriverTest extends \PHPUnit_Framework_TestCase
{ {
private $config; protected $config;
private $io; protected $io;
private $process; protected $process;
private $remoteFileSystem; protected $remoteFileSystem;
private $testPath; protected $testPath;
protected $driver;
protected $repoConfig;
const TEST_URL = 'TEST_PERFORCE_URL';
const TEST_DEPOT = 'TEST_DEPOT_CONFIG';
const TEST_BRANCH = 'TEST_BRANCH_CONFIG';
public function setUp() public function setUp()
{ {
$this->testPath = sys_get_temp_dir() . '/composer-test'; $this->testPath = sys_get_temp_dir() . '/composer-test';
$this->config = new Config(); $this->config = $this->getTestConfig($this->testPath);
$this->config->merge( $this->repoConfig = $this->getTestRepoConfig();
array( $this->io = $this->getMockIOInterface();
'config' => array( $this->process = $this->getMockProcessExecutor();
'home' => $this->testPath, $this->remoteFileSystem = $this->getMockRemoteFilesystem();
), $this->perforce = $this->getMockPerforce();
) $this->driver = new PerforceDriver($this->repoConfig, $this->io, $this->config, $this->process, $this->remoteFileSystem);
);
$this->io = $this->getMock('Composer\IO\IOInterface');
$this->process = $this->getMock('Composer\Util\ProcessExecutor');
$this->remoteFileSystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')->disableOriginalConstructor()
->getMock();
} }
public function tearDown() public function tearDown()
{ {
//cleanup directory under test path
$fs = new Filesystem; $fs = new Filesystem;
$fs->removeDirectory($this->testPath); $fs->removeDirectory($this->testPath);
$this->driver = null;
$this->perforce = null;
$this->remoteFileSystem = null;
$this->process = null;
$this->io = null;
$this->repoConfig = null;
$this->config = null;
$this->testPath = null;
}
protected function getTestConfig($testPath)
{
$config = new Config();
$config->merge(array('config'=>array('home'=>$testPath)));
return $config;
}
protected function getTestRepoConfig()
{
return array(
'url' => self::TEST_URL,
'depot' => self::TEST_DEPOT,
'branch' => self::TEST_BRANCH,
);
}
protected function getMockIOInterface()
{
return $this->getMock('Composer\IO\IOInterface');
}
protected function getMockProcessExecutor()
{
return $this->getMock('Composer\Util\ProcessExecutor');
}
protected function getMockRemoteFilesystem()
{
return $this->getMockBuilder('Composer\Util\RemoteFilesystem')->disableOriginalConstructor()->getMock();
}
protected function getMockPerforce()
{
$methods = array('p4login', 'checkStream', 'writeP4ClientSpec', 'connectClient', 'getComposerInformation');
return $this->getMockBuilder('Composer\Util\Perforce', $methods)->disableOriginalConstructor()->getMock();
} }
public function testInitializeCapturesVariablesFromRepoConfig() public function testInitializeCapturesVariablesFromRepoConfig()
{ {
$this->setUp(); $driver = new PerforceDriver($this->repoConfig, $this->io, $this->config, $this->process, $this->remoteFileSystem);
$repoConfig = array( $driver->setPerforce($this->perforce);
'url' => 'TEST_PERFORCE_URL',
'depot' => 'TEST_DEPOT_CONFIG',
'branch' => 'TEST_BRANCH_CONFIG'
);
$driver = new PerforceDriver($repoConfig, $this->io, $this->config, $this->process, $this->remoteFileSystem);
$process = $this->getMock('Composer\Util\ProcessExecutor');
$arguments = array(
array('depot' => 'TEST_DEPOT', 'branch' => 'TEST_BRANCH'),
'port' => 'TEST_PORT',
'path' => $this->testPath,
$process,
true,
'TEST'
);
$perforce = $this->getMock('Composer\Util\Perforce', null, $arguments);
$driver->setPerforce($perforce);
$driver->initialize(); $driver->initialize();
$this->assertEquals('TEST_PERFORCE_URL', $driver->getUrl()); $this->assertEquals(self::TEST_URL, $driver->getUrl());
$this->assertEquals('TEST_DEPOT_CONFIG', $driver->getDepot()); $this->assertEquals(self::TEST_DEPOT, $driver->getDepot());
$this->assertEquals('TEST_BRANCH_CONFIG', $driver->getBranch()); $this->assertEquals(self::TEST_BRANCH, $driver->getBranch());
} }
public function testInitializeLogsInAndConnectsClient() public function testInitializeLogsInAndConnectsClient()
{ {
$this->setUp(); $this->driver->setPerforce($this->perforce);
$repoConfig = array( $this->perforce->expects($this->at(0))->method('p4Login')->with($this->identicalTo($this->io));
'url' => 'TEST_PERFORCE_URL', $this->perforce->expects($this->at(1))->method('checkStream')->with($this->equalTo(self::TEST_DEPOT));
'depot' => 'TEST_DEPOT_CONFIG', $this->perforce->expects($this->at(2))->method('writeP4ClientSpec');
'branch' => 'TEST_BRANCH_CONFIG' $this->perforce->expects($this->at(3))->method('connectClient');
); $this->driver->initialize();
$driver = new PerforceDriver($repoConfig, $this->io, $this->config, $this->process, $this->remoteFileSystem);
$perforce = $this->getMockBuilder('Composer\Util\Perforce')->disableOriginalConstructor()->getMock();
$perforce->expects($this->at(0))
->method('p4Login')
->with($this->io);
$perforce->expects($this->at(1))
->method('checkStream')
->with($this->equalTo('TEST_DEPOT_CONFIG'));
$perforce->expects($this->at(2))
->method('writeP4ClientSpec');
$perforce->expects($this->at(3))
->method('connectClient');
$driver->setPerforce($perforce);
$driver->initialize();
} }
public function testHasComposerFile() /**
* @depends testInitializeCapturesVariablesFromRepoConfig
* @depends testInitializeLogsInAndConnectsClient
*/
public function testHasComposerFileReturnsFalseOnNoComposerFile()
{ {
$repoConfig = array(
'url' => 'TEST_PERFORCE_URL',
'depot' => 'TEST_DEPOT_CONFIG',
'branch' => 'TEST_BRANCH_CONFIG'
);
$driver = new PerforceDriver($repoConfig, $this->io, $this->config, $this->process, $this->remoteFileSystem);
$process = $this->getMock('Composer\Util\ProcessExecutor');
$arguments = array(
array('depot' => 'TEST_DEPOT', 'branch' => 'TEST_BRANCH'),
'port' => 'TEST_PORT',
'path' => $this->testPath,
$process,
true,
'TEST'
);
$perforce = $this->getMock('Composer\Util\Perforce', array('getComposerInformation'), $arguments);
$perforce->expects($this->at(0))
->method('getComposerInformation')
->with($this->equalTo('//TEST_DEPOT_CONFIG/TEST_IDENTIFIER'))
->will($this->returnValue('Some json stuff'));
$driver->setPerforce($perforce);
$driver->initialize();
$identifier = 'TEST_IDENTIFIER'; $identifier = 'TEST_IDENTIFIER';
$result = $driver->hasComposerFile($identifier); $formatted_depot_path = '//' . self::TEST_DEPOT . '/' . $identifier;
$this->driver->setPerforce($this->perforce);
$this->perforce->expects($this->at(0))->method('getComposerInformation')->with($this->equalTo($formatted_depot_path))->will($this->returnValue(array()));
$this->driver->initialize();
$result = $this->driver->hasComposerFile($identifier);
$this->assertFalse($result);
}
/**
* @depends testInitializeCapturesVariablesFromRepoConfig
* @depends testInitializeLogsInAndConnectsClient
*/
public function testHasComposerFileReturnsTrueWithOneOrMoreComposerFiles()
{
$identifier = 'TEST_IDENTIFIER';
$formatted_depot_path = '//' . self::TEST_DEPOT . '/' . $identifier;
$this->driver->setPerforce($this->perforce);
$this->perforce->expects($this->at(0))->method('getComposerInformation')->with($this->equalTo($formatted_depot_path))->will($this->returnValue(array('')));
$this->driver->initialize();
$result = $this->driver->hasComposerFile($identifier);
$this->assertTrue($result); $this->assertTrue($result);
} }

View File

@ -24,26 +24,44 @@ class PerforceTest extends \PHPUnit_Framework_TestCase
protected $processExecutor; protected $processExecutor;
protected $io; protected $io;
const TEST_DEPOT = 'depot';
const TEST_BRANCH = 'branch';
const TEST_P4USER = 'user';
const TEST_CLIENT_NAME = 'TEST';
const TEST_PORT = 'port';
const TEST_PATH = 'path';
public function setUp() public function setUp()
{ {
$this->processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); $this->processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
$repoConfig = array( $this->repoConfig = $this->getTestRepoConfig();
'depot' => 'depot', $this->io = $this->getMockIOInterface();
'branch' => 'branch', $this->perforce = new Perforce($this->repoConfig, self::TEST_PORT, self::TEST_PATH, $this->processExecutor, true, $this->io);
'p4user' => 'user',
'unique_perforce_client_name' => 'TEST'
);
$io = $this->getMock('Composer\IO\IOInterface');
$this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, true, $io);
} }
public function tearDown() public function tearDown()
{ {
$this->perforce = null; $this->perforce = null;
$this->io = null; $this->io = null;
$this->repoConfig = null;
$this->processExecutor = null; $this->processExecutor = null;
} }
public function getTestRepoConfig()
{
return array(
'depot' => self::TEST_DEPOT,
'branch' => self::TEST_BRANCH,
'p4user' => self::TEST_P4USER,
'unique_perforce_client_name' => self::TEST_CLIENT_NAME
);
}
public function getMockIOInterface()
{
return $this->getMock('Composer\IO\IOInterface');
}
public function testGetClientWithoutStream() public function testGetClientWithoutStream()
{ {
$client = $this->perforce->getClient(); $client = $this->perforce->getClient();
@ -107,116 +125,90 @@ class PerforceTest extends \PHPUnit_Framework_TestCase
public function testQueryP4UserWithUserAlreadySet() public function testQueryP4UserWithUserAlreadySet()
{ {
$io = $this->getMock('Composer\IO\IOInterface'); $this->perforce->queryP4user();
$this->assertEquals(self::TEST_P4USER, $this->perforce->getUser());
$repoConfig = array('depot' => 'depot', 'branch' => 'branch', 'p4user' => 'TEST_USER');
$this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, true, 'TEST');
$this->perforce->queryP4user($io);
$this->assertEquals('TEST_USER', $this->perforce->getUser());
} }
public function testQueryP4UserWithUserSetInP4VariablesWithWindowsOS() public function testQueryP4UserWithUserSetInP4VariablesWithWindowsOS()
{ {
$repoConfig = array('depot' => 'depot', 'branch' => 'branch'); $this->perforce->setUser(null);
$this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, true, 'TEST'); $this->perforce->setWindowsFlag(true);
$io = $this->getMock('Composer\IO\IOInterface');
$expectedCommand = 'p4 set'; $expectedCommand = 'p4 set';
$callback = function($command, &$output)
{
$output = 'P4USER=TEST_P4VARIABLE_USER' . PHP_EOL;
return true;
};
$this->processExecutor->expects($this->at(0)) $this->processExecutor->expects($this->at(0))
->method('execute') ->method('execute')
->with($this->equalTo($expectedCommand)) ->with($this->equalTo($expectedCommand))
->will( ->will($this->returnCallback($callback));
$this->returnCallback( $this->perforce->queryP4user();
function ($command, &$output) {
$output = 'P4USER=TEST_P4VARIABLE_USER' . PHP_EOL ;
return true;
}
)
);
$this->perforce->queryP4user($io);
$this->assertEquals('TEST_P4VARIABLE_USER', $this->perforce->getUser()); $this->assertEquals('TEST_P4VARIABLE_USER', $this->perforce->getUser());
} }
public function testQueryP4UserWithUserSetInP4VariablesNotWindowsOS() public function testQueryP4UserWithUserSetInP4VariablesNotWindowsOS()
{ {
$repoConfig = array('depot' => 'depot', 'branch' => 'branch'); $this->perforce->setUser(null);
$this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, false, 'TEST'); $this->perforce->setWindowsFlag(false);
$io = $this->getMock('Composer\IO\IOInterface');
$expectedCommand = 'echo $P4USER'; $expectedCommand = 'echo $P4USER';
$callback = function($command, &$output)
{
$output = 'TEST_P4VARIABLE_USER' . PHP_EOL;
return true;
};
$this->processExecutor->expects($this->at(0)) $this->processExecutor->expects($this->at(0))
->method('execute') ->method('execute')
->with($this->equalTo($expectedCommand)) ->with($this->equalTo($expectedCommand))
->will( ->will($this->returnCallback($callback));
$this->returnCallback( $this->perforce->queryP4user();
function ($command, &$output) {
$output = 'TEST_P4VARIABLE_USER' . PHP_EOL;
return true;
}
)
);
$this->perforce->queryP4user($io);
$this->assertEquals('TEST_P4VARIABLE_USER', $this->perforce->getUser()); $this->assertEquals('TEST_P4VARIABLE_USER', $this->perforce->getUser());
} }
public function testQueryP4UserQueriesForUser() public function testQueryP4UserQueriesForUser()
{ {
$repoConfig = array('depot' => 'depot', 'branch' => 'branch'); $this->perforce->setUser(null);
$this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, false, 'TEST');
$io = $this->getMock('Composer\IO\IOInterface');
$expectedQuestion = 'Enter P4 User:'; $expectedQuestion = 'Enter P4 User:';
$io->expects($this->at(0)) $this->io->expects($this->at(0))
->method('ask') ->method('ask')
->with($this->equalTo($expectedQuestion)) ->with($this->equalTo($expectedQuestion))
->will($this->returnValue('TEST_QUERY_USER')); ->will($this->returnValue('TEST_QUERY_USER'));
$this->perforce->queryP4user();
$this->perforce->queryP4user($io);
$this->assertEquals('TEST_QUERY_USER', $this->perforce->getUser()); $this->assertEquals('TEST_QUERY_USER', $this->perforce->getUser());
} }
public function testQueryP4UserStoresResponseToQueryForUserWithWindows() public function testQueryP4UserStoresResponseToQueryForUserWithWindows()
{ {
$repoConfig = array('depot' => 'depot', 'branch' => 'branch'); $this->perforce->setUser(null);
$this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, true, 'TEST'); $this->perforce->setWindowsFlag(true);
$io = $this->getMock('Composer\IO\IOInterface');
$expectedQuestion = 'Enter P4 User:'; $expectedQuestion = 'Enter P4 User:';
$io->expects($this->at(0)) $expectedCommand = 'p4 set P4USER=TEST_QUERY_USER';
->method('ask') $this->io->expects($this->at(0))
->with($this->equalTo($expectedQuestion)) ->method('ask')
->will($this->returnValue('TEST_QUERY_USER')); ->with($this->equalTo($expectedQuestion))
$expectedCommand = 'p4 set P4USER=TEST_QUERY_USER'; ->will($this->returnValue('TEST_QUERY_USER'));
$this->processExecutor->expects($this->at(1)) $this->processExecutor->expects($this->at(1))
->method('execute') ->method('execute')
->with($this->equalTo($expectedCommand)) ->with($this->equalTo($expectedCommand))
->will($this->returnValue(0)); ->will($this->returnValue(0));
$this->perforce->queryP4user();
$this->perforce->queryP4user($io);
} }
public function testQueryP4UserStoresResponseToQueryForUserWithoutWindows() public function testQueryP4UserStoresResponseToQueryForUserWithoutWindows()
{ {
$repoConfig = array('depot' => 'depot', 'branch' => 'branch'); $this->perforce->setUser(null);
$this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, false, 'TEST'); $this->perforce->setWindowsFlag(false);
$io = $this->getMock('Composer\IO\IOInterface');
$expectedQuestion = 'Enter P4 User:'; $expectedQuestion = 'Enter P4 User:';
$io->expects($this->at(0)) $expectedCommand = 'export P4USER=TEST_QUERY_USER';
->method('ask') $this->io->expects($this->at(0))
->with($this->equalTo($expectedQuestion)) ->method('ask')
->will($this->returnValue('TEST_QUERY_USER')); ->with($this->equalTo($expectedQuestion))
$expectedCommand = 'export P4USER=TEST_QUERY_USER'; ->will($this->returnValue('TEST_QUERY_USER'));
$this->processExecutor->expects($this->at(1)) $this->processExecutor->expects($this->at(1))
->method('execute') ->method('execute')
->with($this->equalTo($expectedCommand)) ->with($this->equalTo($expectedCommand))
->will($this->returnValue(0)); ->will($this->returnValue(0));
$this->perforce->queryP4user();
$this->perforce->queryP4user($io);
} }
public function testQueryP4PasswordWithPasswordAlreadySet() public function testQueryP4PasswordWithPasswordAlreadySet()
@ -227,69 +219,55 @@ class PerforceTest extends \PHPUnit_Framework_TestCase
'p4user' => 'user', 'p4user' => 'user',
'p4password' => 'TEST_PASSWORD' 'p4password' => 'TEST_PASSWORD'
); );
$this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, false, 'TEST'); $this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, false, $this->getMockIOInterface(), 'TEST');
$io = $this->getMock('Composer\IO\IOInterface'); $password = $this->perforce->queryP4Password();
$password = $this->perforce->queryP4Password($io);
$this->assertEquals('TEST_PASSWORD', $password); $this->assertEquals('TEST_PASSWORD', $password);
} }
public function testQueryP4PasswordWithPasswordSetInP4VariablesWithWindowsOS() public function testQueryP4PasswordWithPasswordSetInP4VariablesWithWindowsOS()
{ {
$io = $this->getMock('Composer\IO\IOInterface'); $this->perforce->setWindowsFlag(true);
$expectedCommand = 'p4 set'; $expectedCommand = 'p4 set';
$callback = function($command, &$output)
{
$output = 'P4PASSWD=TEST_P4VARIABLE_PASSWORD' . PHP_EOL;
return true;
};
$this->processExecutor->expects($this->at(0)) $this->processExecutor->expects($this->at(0))
->method('execute') ->method('execute')
->with($this->equalTo($expectedCommand)) ->with($this->equalTo($expectedCommand))
->will( ->will($this->returnCallback($callback));
$this->returnCallback( $password = $this->perforce->queryP4Password();
function ($command, &$output) {
$output = 'P4PASSWD=TEST_P4VARIABLE_PASSWORD' . PHP_EOL;
return true;
}
)
);
$password = $this->perforce->queryP4Password($io);
$this->assertEquals('TEST_P4VARIABLE_PASSWORD', $password); $this->assertEquals('TEST_P4VARIABLE_PASSWORD', $password);
} }
public function testQueryP4PasswordWithPasswordSetInP4VariablesNotWindowsOS() public function testQueryP4PasswordWithPasswordSetInP4VariablesNotWindowsOS()
{ {
$repoConfig = array('depot' => 'depot', 'branch' => 'branch', 'p4user' => 'user'); $this->perforce->setWindowsFlag(false);
$this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, false, 'TEST');
$io = $this->getMock('Composer\IO\IOInterface');
$expectedCommand = 'echo $P4PASSWD'; $expectedCommand = 'echo $P4PASSWD';
$callback = function($command, &$output)
{
$output = 'TEST_P4VARIABLE_PASSWORD' . PHP_EOL;
return true;
};
$this->processExecutor->expects($this->at(0)) $this->processExecutor->expects($this->at(0))
->method('execute') ->method('execute')
->with($this->equalTo($expectedCommand)) ->with($this->equalTo($expectedCommand))
->will( ->will($this->returnCallback($callback));
$this->returnCallback(
function ($command, &$output) {
$output = 'TEST_P4VARIABLE_PASSWORD' . PHP_EOL;
return true; $password = $this->perforce->queryP4Password();
}
)
);
$password = $this->perforce->queryP4Password($io);
$this->assertEquals('TEST_P4VARIABLE_PASSWORD', $password); $this->assertEquals('TEST_P4VARIABLE_PASSWORD', $password);
} }
public function testQueryP4PasswordQueriesForPassword() public function testQueryP4PasswordQueriesForPassword()
{ {
$io = $this->getMock('Composer\IO\IOInterface');
$expectedQuestion = 'Enter password for Perforce user user: '; $expectedQuestion = 'Enter password for Perforce user user: ';
$io->expects($this->at(0)) $this->io->expects($this->at(0))
->method('askAndHideAnswer') ->method('askAndHideAnswer')
->with($this->equalTo($expectedQuestion)) ->with($this->equalTo($expectedQuestion))
->will($this->returnValue('TEST_QUERY_PASSWORD')); ->will($this->returnValue('TEST_QUERY_PASSWORD'));
$password = $this->perforce->queryP4Password($io); $password = $this->perforce->queryP4Password();
$this->assertEquals('TEST_QUERY_PASSWORD', $password); $this->assertEquals('TEST_QUERY_PASSWORD', $password);
} }