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 0f7b078d6c
commit 24dd42267f
6 changed files with 239 additions and 229 deletions

View File

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

View File

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

View File

@ -200,7 +200,12 @@ class Perforce
return $this->p4User;
}
public function queryP4User(IOInterface $io)
public function setUser($user)
{
$this->p4User = $user;
}
public function queryP4User()
{
$this->getUser();
if (strlen($this->p4User) > 0) {
@ -210,7 +215,7 @@ class Perforce
if (strlen($this->p4User) > 0) {
return;
}
$this->p4User = $io->ask('Enter P4 User:');
$this->p4User = $this->io->ask('Enter P4 User:');
if ($this->windowsFlag) {
$command = 'p4 set P4USER=' . $this->p4User;
} else {
@ -248,14 +253,14 @@ class Perforce
}
}
public function queryP4Password(IOInterface $io)
public function queryP4Password()
{
if (isset($this->p4Password)) {
return $this->p4Password;
}
$password = $this->getP4variable('P4PASSWD');
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;
@ -365,6 +370,16 @@ class Perforce
return;
}
public function getWindowsFlag()
{
return $this->windowsFlag;
}
public function setWindowsFlag($flag)
{
$this->windowsFlag = $flag;
}
public function windowsLogin($password)
{
$command = $this->generateP4Command(' login -a');
@ -373,11 +388,11 @@ class Perforce
return $process->run();
}
public function p4Login(IOInterface $io)
public function p4Login()
{
$this->queryP4User($io);
$this->queryP4User();
if (!$this->isLoggedIn()) {
$password = $this->queryP4Password($io);
$password = $this->queryP4Password();
if ($this->windowsFlag) {
$this->windowsLogin($password);
} 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));
$perforceMethods = array('setStream', 'p4Login', 'writeP4ClientSpec', 'connectClient', 'syncCodeBase', 'cleanupClientSpec');
$perforce = $this->getMockBuilder('Composer\Util\Perforce', $perforceMethods)->disableOriginalConstructor()->getMock();
$perforce->expects($this->at(0))->method('setStream')->with($this->equalTo($ref));
$perforce->expects($this->at(1))->method('p4Login')->with($this->identicalTo($this->io));
$perforce->expects($this->at(2))->method('writeP4ClientSpec');
$perforce->expects($this->at(3))->method('connectClient');
$perforce->expects($this->at(4))->method('syncCodeBase');
$perforce->expects($this->at(5))->method('cleanupClientSpec');
$perforce->expects($this->at(0))->method('initializePath')->with($this->equalTo($this->testPath));
$perforce->expects($this->at(1))->method('setStream')->with($this->equalTo($ref));
$perforce->expects($this->at(2))->method('p4Login')->with($this->identicalTo($this->io));
$perforce->expects($this->at(3))->method('writeP4ClientSpec');
$perforce->expects($this->at(4))->method('connectClient');
$perforce->expects($this->at(5))->method('syncCodeBase');
$perforce->expects($this->at(6))->method('cleanupClientSpec');
$this->downloader->setPerforce($perforce);
$this->downloader->doDownload($this->package, $this->testPath);
}

View File

@ -21,113 +21,129 @@ use Composer\Config;
*/
class PerforceDriverTest extends \PHPUnit_Framework_TestCase
{
private $config;
private $io;
private $process;
private $remoteFileSystem;
private $testPath;
protected $config;
protected $io;
protected $process;
protected $remoteFileSystem;
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()
{
$this->testPath = sys_get_temp_dir() . '/composer-test';
$this->config = new Config();
$this->config->merge(
array(
'config' => array(
'home' => $this->testPath,
),
)
);
$this->io = $this->getMock('Composer\IO\IOInterface');
$this->process = $this->getMock('Composer\Util\ProcessExecutor');
$this->remoteFileSystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')->disableOriginalConstructor()
->getMock();
$this->testPath = sys_get_temp_dir() . '/composer-test';
$this->config = $this->getTestConfig($this->testPath);
$this->repoConfig = $this->getTestRepoConfig();
$this->io = $this->getMockIOInterface();
$this->process = $this->getMockProcessExecutor();
$this->remoteFileSystem = $this->getMockRemoteFilesystem();
$this->perforce = $this->getMockPerforce();
$this->driver = new PerforceDriver($this->repoConfig, $this->io, $this->config, $this->process, $this->remoteFileSystem);
}
public function tearDown()
{
//cleanup directory under test path
$fs = new Filesystem;
$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()
{
$this->setUp();
$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', null, $arguments);
$driver->setPerforce($perforce);
$driver = new PerforceDriver($this->repoConfig, $this->io, $this->config, $this->process, $this->remoteFileSystem);
$driver->setPerforce($this->perforce);
$driver->initialize();
$this->assertEquals('TEST_PERFORCE_URL', $driver->getUrl());
$this->assertEquals('TEST_DEPOT_CONFIG', $driver->getDepot());
$this->assertEquals('TEST_BRANCH_CONFIG', $driver->getBranch());
$this->assertEquals(self::TEST_URL, $driver->getUrl());
$this->assertEquals(self::TEST_DEPOT, $driver->getDepot());
$this->assertEquals(self::TEST_BRANCH, $driver->getBranch());
}
public function testInitializeLogsInAndConnectsClient()
{
$this->setUp();
$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);
$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();
$this->driver->setPerforce($this->perforce);
$this->perforce->expects($this->at(0))->method('p4Login')->with($this->identicalTo($this->io));
$this->perforce->expects($this->at(1))->method('checkStream')->with($this->equalTo(self::TEST_DEPOT));
$this->perforce->expects($this->at(2))->method('writeP4ClientSpec');
$this->perforce->expects($this->at(3))->method('connectClient');
$this->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';
$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);
}

View File

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