1
0
Fork 0

added new dependency to Perforce object, updating some tests.

pull/2834/head
Clark Stuth 2014-03-14 14:45:31 -05:00
parent 4a49533988
commit 454c6eca81
5 changed files with 122 additions and 76 deletions

View File

@ -22,7 +22,7 @@ use Composer\Util\Perforce;
class PerforceDownloader extends VcsDownloader class PerforceDownloader extends VcsDownloader
{ {
protected $perforce; protected $perforce;
protected $perforceInjected = false; // protected $perforceInjected = false;
/** /**
* {@inheritDoc} * {@inheritDoc}
@ -54,7 +54,7 @@ class PerforceDownloader extends VcsDownloader
if ($repository instanceof VcsRepository) { if ($repository instanceof VcsRepository) {
$repoConfig = $this->getRepoConfig($repository); $repoConfig = $this->getRepoConfig($repository);
} }
$this->perforce = Perforce::create($repoConfig, $package->getSourceUrl(), $path); $this->perforce = Perforce::create($repoConfig, $package->getSourceUrl(), $path, $this->process, $this->io);
} }
private function getRepoConfig(VcsRepository $repository) private function getRepoConfig(VcsRepository $repository)

View File

@ -56,7 +56,7 @@ class PerforceDriver extends VcsDriver
} }
$repoDir = $this->config->get('cache-vcs-dir') . '/' . $this->depot; $repoDir = $this->config->get('cache-vcs-dir') . '/' . $this->depot;
$this->perforce = Perforce::create($repoConfig, $this->getUrl(), $repoDir, $this->process); $this->perforce = Perforce::create($repoConfig, $this->getUrl(), $repoDir, $this->process, $this->io);
} }
/** /**

View File

@ -35,27 +35,36 @@ class Perforce
protected $windowsFlag; protected $windowsFlag;
protected $commandResult; protected $commandResult;
public function __construct($repoConfig, $port, $path, ProcessExecutor $process, $isWindows) protected $io;
public function __construct($repoConfig, $port, $path, ProcessExecutor $process, $isWindows, IOInterface $io)
{ {
$this->windowsFlag = $isWindows; $this->windowsFlag = $isWindows;
$this->p4Port = $port; $this->p4Port = $port;
$this->initializePath($path); $this->initializePath($path);
$this->process = $process; $this->process = $process;
$this->initialize($repoConfig); $this->initialize($repoConfig);
$this->io = $io;
} }
public static function create($repoConfig, $port, $path, ProcessExecutor $process = null) public static function create($repoConfig, $port, $path, ProcessExecutor $process = null, IOInterface $io)
{ {
if (!isset($process)) { if (!isset($process)) {
$process = new ProcessExecutor; $process = new ProcessExecutor;
} }
$isWindows = defined('PHP_WINDOWS_VERSION_BUILD'); $isWindows = defined('PHP_WINDOWS_VERSION_BUILD');
$perforce = new Perforce($repoConfig, $port, $path, $process, $isWindows); $perforce = new Perforce($repoConfig, $port, $path, $process, $isWindows, $io);
return $perforce; return $perforce;
} }
public static function checkServerExists($url, ProcessExecutor $processExecutor)
{
$output = null;
return 0 === $processExecutor->execute('p4 -p ' . $url . ' info -s', $output);
}
public function initialize($repoConfig) public function initialize($repoConfig)
{ {
$this->uniquePerforceClientName = $this->generateUniquePerforceClientName(); $this->uniquePerforceClientName = $this->generateUniquePerforceClientName();
@ -382,12 +391,6 @@ class Perforce
} }
} }
public static function checkServerExists($url, ProcessExecutor $processExecutor)
{
$output = null;
return 0 === $processExecutor->execute('p4 -p ' . $url . ' info -s', $output);
}
public function getComposerInformation($identifier) public function getComposerInformation($identifier)
{ {
$index = strpos($identifier, '@'); $index = strpos($identifier, '@');

View File

@ -15,86 +15,120 @@ namespace Composer\Test\Downloader;
use Composer\Downloader\PerforceDownloader; use Composer\Downloader\PerforceDownloader;
use Composer\Config; use Composer\Config;
use Composer\Repository\VcsRepository; use Composer\Repository\VcsRepository;
use Composer\IO\IOInterface;
/** /**
* @author Matt Whittom <Matt.Whittom@veteransunited.com> * @author Matt Whittom <Matt.Whittom@veteransunited.com>
*/ */
class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase
{ {
private $io;
private $config;
private $testPath;
public static $repository;
protected function setUp() protected $config;
protected $downloader;
protected $io;
protected $package;
protected $processExecutor;
protected $repoConfig;
protected $repository;
protected $testPath;
public function setUp()
{ {
$this->testPath = sys_get_temp_dir() . '/composer-test'; $this->testPath = sys_get_temp_dir() . '/composer-test';
$this->config = new Config(); $this->repoConfig = $this->getRepoConfig();
$this->config->merge( $this->config = $this->getConfig();
array( $this->io = $this->getMockIoInterface();
'config' => array( $this->processExecutor = $this->getMockProcessExecutor();
'home' => $this->testPath, $this->repository = $this->getMockRepository($this->repoConfig, $this->io, $this->config);
), $this->package = $this->getMockPackageInterface($this->repository);
) $this->downloader = new PerforceDownloader($this->io, $this->config, $this->processExecutor);
);
$this->io = $this->getMock('Composer\IO\IOInterface');
} }
public function testInitPerforceGetRepoConfig() public function tearDown()
{ {
$downloader = new PerforceDownloader($this->io, $this->config); $this->downloader = null;
$package = $this->getMock('Composer\Package\PackageInterface'); $this->package = null;
$repoConfig = array('url' => 'TEST_URL', 'p4user' => 'TEST_USER'); $this->repository = null;
$repository = $this->getMock( $this->io = null;
'Composer\Repository\VcsRepository', $this->config = null;
array('getRepoConfig'), $this->repoConfig = null;
array($repoConfig, $this->io, $this->config) $this->testPath = null;
);
$package->expects($this->at(0))
->method('getRepository')
->will($this->returnValue($repository));
$repository->expects($this->at(0))
->method('getRepoConfig');
$path = $this->testPath;
$downloader->initPerforce($package, $path, 'SOURCE_REF');
} }
protected function getMockProcessExecutor()
{
return $this->getMock('Composer\Util\ProcessExecutor');
}
protected function getConfig()
{
$config = new Config();
$settings = array('config' => array('home' => $this->testPath));
$config->merge($settings);
return $config;
}
protected function getMockIoInterface()
{
return $this->getMock('Composer\IO\IOInterface');
}
protected function getMockPackageInterface(VcsRepository $repository)
{
$package = $this->getMock('Composer\Package\PackageInterface');
$package->expects($this->any())->method('getRepository')->will($this->returnValue($repository));
return $package;
}
protected function getRepoConfig()
{
return array('url' => 'TEST_URL', 'p4user' => 'TEST_USER');
}
protected function getMockRepository(array $repoConfig, IOInterface $io, Config $config)
{
$class = 'Composer\Repository\VcsRepository';
$methods = array('getRepoConfig');
$args = array($repoConfig, $io, $config);
$repository = $this->getMock($class, $methods, $args);
$repository->expects($this->any())->method('getRepoConfig')->will($this->returnValue($repoConfig));
return $repository;
}
public function testInitPerforceInstantiatesANewPerforceObject()
{
$this->downloader->initPerforce($this->package, $this->testPath, 'SOURCE_REF');
}
public function testInitPerforceDoesNothingIfPerforceAlreadySet()
{
$perforce = $this->getMockBuilder('Composer\Util\Perforce')->disableOriginalConstructor()->getMock();
$this->downloader->setPerforce($perforce);
$this->repository->expects($this->never())->method('getRepoConfig');
$this->downloader->initPerforce($this->package, $this->testPath, 'SOURCE_REF');
}
/**
* @depends testInitPerforceInstantiatesANewPerforceObject
* @depends testInitPerforceDoesNothingIfPerforceAlreadySet
*/
public function testDoDownload() public function testDoDownload()
{ {
$downloader = new PerforceDownloader($this->io, $this->config); //I really don't like this test but the logic of each Perforce method is tested in the Perforce class. Really I am just enforcing workflow.
$repoConfig = array('depot' => 'TEST_DEPOT', 'branch' => 'TEST_BRANCH', 'p4user' => 'TEST_USER');
$port = 'TEST_PORT';
$path = 'TEST_PATH';
$process = $this->getmock('Composer\Util\ProcessExecutor');
$perforce = $this->getMock(
'Composer\Util\Perforce',
array('setStream', 'queryP4User', 'writeP4ClientSpec', 'connectClient', 'syncCodeBase'),
array($repoConfig, $port, $path, $process, true, 'TEST')
);
$ref = 'SOURCE_REF'; $ref = 'SOURCE_REF';
$label = 'LABEL'; $label = 'LABEL';
$perforce->expects($this->at(0)) $this->package->expects($this->once())->method('getSourceReference')->will($this->returnValue($ref));
->method('setStream') $this->package->expects($this->once())->method('getPrettyVersion')->will($this->returnValue($label));
->with($this->equalTo($ref)); $this->io->expects($this->once())->method('write')->with($this->stringContains('Cloning '.$ref));
$perforce->expects($this->at(1)) $perforceMethods = array('setStream', 'p4Login', 'writeP4ClientSpec', 'connectClient', 'syncCodeBase', 'cleanupClientSpec');
->method('queryP4User') $perforce = $this->getMockBuilder('Composer\Util\Perforce', $perforceMethods)->disableOriginalConstructor()->getMock();
->with($this->io); $perforce->expects($this->at(0))->method('setStream')->with($this->equalTo($ref));
$perforce->expects($this->at(2)) $perforce->expects($this->at(1))->method('p4Login')->with($this->identicalTo($this->io));
->method('writeP4ClientSpec'); $perforce->expects($this->at(2))->method('writeP4ClientSpec');
$perforce->expects($this->at(3)) $perforce->expects($this->at(3))->method('connectClient');
->method('connectClient'); $perforce->expects($this->at(4))->method('syncCodeBase');
$perforce->expects($this->at(4)) $perforce->expects($this->at(5))->method('cleanupClientSpec');
->method('syncCodeBase') $this->downloader->setPerforce($perforce);
->with($this->equalTo($label)); $this->downloader->doDownload($this->package, $this->testPath);
$downloader->setPerforce($perforce);
$package = $this->getMock('Composer\Package\PackageInterface');
$package->expects($this->at(0))
->method('getSourceReference')
->will($this->returnValue($ref));
$package->expects($this->at(1))
->method('getPrettyVersion')
->will($this->returnValue($label));
$path = $this->testPath;
$downloader->doDownload($package, $path);
} }
} }

View File

@ -22,6 +22,7 @@ class PerforceTest extends \PHPUnit_Framework_TestCase
{ {
protected $perforce; protected $perforce;
protected $processExecutor; protected $processExecutor;
protected $io;
public function setUp() public function setUp()
{ {
@ -32,7 +33,15 @@ class PerforceTest extends \PHPUnit_Framework_TestCase
'p4user' => 'user', 'p4user' => 'user',
'unique_perforce_client_name' => 'TEST' 'unique_perforce_client_name' => 'TEST'
); );
$this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, true); $io = $this->getMock('Composer\IO\IOInterface');
$this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, true, $io);
}
public function tearDown()
{
$this->perforce = null;
$this->io = null;
$this->processExecutor = null;
} }
public function testGetClientWithoutStream() public function testGetClientWithoutStream()