Updated to match psr-2 spec, fixed header comments

main
mwhittom 11 years ago
parent f737e49aae
commit 774a55befd

@ -6,9 +6,6 @@
* (c) Nils Adermann <naderman@naderman.de> * (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be> * Jordi Boggiano <j.boggiano@seld.be>
* *
* Contributor: Matt Whittom <Matt.Whittom@veteransunited.com>
* Date: 7/17/13
*
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
@ -46,7 +43,8 @@ class PerforceDownloader extends VcsDownloader
$this->perforce->cleanupClientSpec(); $this->perforce->cleanupClientSpec();
} }
private function initPerforce($package, $path, $ref){ private function initPerforce($package, $path, $ref)
{
if ($this->perforceInjected) { if ($this->perforceInjected) {
return; return;
} }
@ -58,7 +56,8 @@ class PerforceDownloader extends VcsDownloader
$this->perforce = Perforce::createPerforce($repoConfig, $package->getSourceUrl(), $path); $this->perforce = Perforce::createPerforce($repoConfig, $package->getSourceUrl(), $path);
} }
private function getRepoConfig(VcsRepository $repository){ private function getRepoConfig(VcsRepository $repository)
{
return $repository->getRepoConfig(); return $repository->getRepoConfig();
} }
@ -75,7 +74,7 @@ class PerforceDownloader extends VcsDownloader
*/ */
public function getLocalChanges(PackageInterface $package, $path) public function getLocalChanges(PackageInterface $package, $path)
{ {
print ("Perforce driver does not check for local changes before overriding\n"); $this->io->write("Perforce driver does not check for local changes before overriding", true);
return; return;
} }
@ -89,10 +88,9 @@ class PerforceDownloader extends VcsDownloader
return $commitLogs; return $commitLogs;
} }
public function injectPerforce($perforce){ public function injectPerforce($perforce)
{
$this->perforce = $perforce; $this->perforce = $perforce;
$this->perforceInjected = true; $this->perforceInjected = true;
} }
} }

@ -6,9 +6,6 @@
* (c) Nils Adermann <naderman@naderman.de> * (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be> * Jordi Boggiano <j.boggiano@seld.be>
* *
* Contributor: Matt Whittom <Matt.Whittom@veteransunited.com>
* Date: 7/17/13
*
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
@ -24,7 +21,8 @@ use Composer\Util\Perforce;
/** /**
* @author Matt Whittom <Matt.Whittom@veteransunited.com> * @author Matt Whittom <Matt.Whittom@veteransunited.com>
*/ */
class PerforceDriver extends VcsDriver { class PerforceDriver extends VcsDriver
{
protected $depot; protected $depot;
protected $branch; protected $branch;
protected $perforce; protected $perforce;
@ -49,7 +47,7 @@ class PerforceDriver extends VcsDriver {
$this->perforce->writeP4ClientSpec(); $this->perforce->writeP4ClientSpec();
$this->perforce->connectClient(); $this->perforce->connectClient();
return TRUE; return true;
} }
private function initPerforce($repoConfig) private function initPerforce($repoConfig)
@ -68,8 +66,7 @@ class PerforceDriver extends VcsDriver {
public function getComposerInformation($identifier) public function getComposerInformation($identifier)
{ {
if (isset($this->composer_info_identifier)) { if (isset($this->composer_info_identifier)) {
if (strcmp($identifier, $this->composer_info_identifier) === 0 ) if (strcmp($identifier, $this->composer_info_identifier) === 0) {
{
return $this->composer_info; return $this->composer_info;
} }
} }
@ -111,7 +108,7 @@ class PerforceDriver extends VcsDriver {
*/ */
public function getDist($identifier) public function getDist($identifier)
{ {
return NULL; return null;
} }
/** /**
@ -156,13 +153,13 @@ class PerforceDriver extends VcsDriver {
*/ */
public function getContents($url) public function getContents($url)
{ {
return FALSE; return false;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public static function supports(IOInterface $io, $url, $deep = FALSE) public static function supports(IOInterface $io, $url, $deep = false)
{ {
return Perforce::checkServerExists($url, new ProcessExecutor); return Perforce::checkServerExists($url, new ProcessExecutor);
} }

@ -5,9 +5,6 @@
* (c) Nils Adermann <naderman@naderman.de> * (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be> * Jordi Boggiano <j.boggiano@seld.be>
* *
* Contributor: Matt Whittom <Matt.Whittom@veteransunited.com>
* Date: 7/17/13
*
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
@ -20,7 +17,8 @@ use Composer\IO\IOInterface;
/** /**
* @author Matt Whittom <Matt.Whittom@veteransunited.com> * @author Matt Whittom <Matt.Whittom@veteransunited.com>
*/ */
class Perforce { class Perforce
{
protected $path; protected $path;
protected $p4Depot; protected $p4Depot;
@ -37,7 +35,8 @@ class Perforce {
protected $windowsFlag; protected $windowsFlag;
public static function createPerforce($repoConfig, $port, $path, ProcessExecutor $process = NULL) { public static function createPerforce($repoConfig, $port, $path, ProcessExecutor $process = null)
{
if (!isset($process)) { if (!isset($process)) {
$process = new ProcessExecutor; $process = new ProcessExecutor;
} }
@ -47,7 +46,8 @@ class Perforce {
return $perforce; return $perforce;
} }
public function __construct($repoConfig, $port, $path, ProcessExecutor $process, $isWindows) { public function __construct($repoConfig, $port, $path, ProcessExecutor $process, $isWindows)
{
$this->windowsFlag = $isWindows; $this->windowsFlag = $isWindows;
$this->p4Port = $port; $this->p4Port = $port;
$this->path = $path; $this->path = $path;
@ -57,7 +57,8 @@ class Perforce {
$this->initialize($repoConfig); $this->initialize($repoConfig);
} }
public function initialize($repoConfig){ public function initialize($repoConfig)
{
$this->unique_perforce_client_name = $this->generateUniquePerforceClientName(); $this->unique_perforce_client_name = $this->generateUniquePerforceClientName();
if (!isset ($repoConfig)) { if (!isset ($repoConfig)) {
return; return;
@ -74,8 +75,7 @@ class Perforce {
} }
if (isset($repoConfig['p4user'])) { if (isset($repoConfig['p4user'])) {
$this->p4User = $repoConfig['p4user']; $this->p4User = $repoConfig['p4user'];
} } else {
else {
$this->p4User = $this->getP4variable("P4USER"); $this->p4User = $this->getP4variable("P4USER");
} }
if (isset($repoConfig['p4password'])) { if (isset($repoConfig['p4password'])) {
@ -83,7 +83,8 @@ class Perforce {
} }
} }
public function initializeDepotAndBranch($depot, $branch){ public function initializeDepotAndBranch($depot, $branch)
{
if (isset($depot)) { if (isset($depot)) {
$this->p4Depot = $depot; $this->p4Depot = $depot;
} }
@ -92,11 +93,13 @@ class Perforce {
} }
} }
public function generateUniquePerforceClientName(){ public function generateUniquePerforceClientName()
{
return gethostname() . "_" . time(); return gethostname() . "_" . time();
} }
public function cleanupClientSpec(){ public function cleanupClientSpec()
{
$client = $this->getClient(); $client = $this->getClient();
$command = "p4 client -d $client"; $command = "p4 client -d $client";
$this->executeCommand($command); $this->executeCommand($command);
@ -105,14 +108,16 @@ class Perforce {
$fileSystem->remove($clientSpec); $fileSystem->remove($clientSpec);
} }
protected function executeCommand($command) { protected function executeCommand($command)
{
$result = ""; $result = "";
$this->process->execute($command, $result); $this->process->execute($command, $result);
return $result; return $result;
} }
public function getClient() { public function getClient()
{
if (!isset($this->p4Client)) { if (!isset($this->p4Client)) {
$clean_stream_name = str_replace("@", "", str_replace("/", "_", str_replace("//", "", $this->getStream()))); $clean_stream_name = str_replace("@", "", str_replace("/", "_", str_replace("//", "", $this->getStream())));
$this->p4Client = "composer_perforce_" . $this->unique_perforce_client_name . "_" . $clean_stream_name; $this->p4Client = "composer_perforce_" . $this->unique_perforce_client_name . "_" . $clean_stream_name;
@ -121,15 +126,18 @@ class Perforce {
return $this->p4Client; return $this->p4Client;
} }
protected function getPath() { protected function getPath()
{
return $this->path; return $this->path;
} }
protected function getPort() { protected function getPort()
{
return $this->p4Port; return $this->p4Port;
} }
public function setStream($stream) { public function setStream($stream)
{
$this->p4Stream = $stream; $this->p4Stream = $stream;
$index = strrpos($stream, "/"); $index = strrpos($stream, "/");
//Stream format is //depot/stream, while non-streaming depot is //depot //Stream format is //depot/stream, while non-streaming depot is //depot
@ -138,42 +146,47 @@ class Perforce {
} }
} }
public function isStream() { public function isStream()
{
return (strcmp($this->p4DepotType, "stream") === 0); return (strcmp($this->p4DepotType, "stream") === 0);
} }
public function getStream() { public function getStream()
{
if (!isset($this->p4Stream)) { if (!isset($this->p4Stream)) {
if ($this->isStream()) { if ($this->isStream()) {
$this->p4Stream = "//$this->p4Depot/$this->p4Branch"; $this->p4Stream = "//$this->p4Depot/$this->p4Branch";
} } else {
else {
$this->p4Stream = "//$this->p4Depot"; $this->p4Stream = "//$this->p4Depot";
} }
} }
return $this->p4Stream; return $this->p4Stream;
} }
public function getStreamWithoutLabel($stream) { public function getStreamWithoutLabel($stream)
{
$index = strpos($stream, "@"); $index = strpos($stream, "@");
if ($index === FALSE) { if ($index === false) {
return $stream; return $stream;
} }
return substr($stream, 0, $index); return substr($stream, 0, $index);
} }
public function getP4ClientSpec() { public function getP4ClientSpec()
{
$p4clientSpec = $this->path . "/" . $this->getClient() . ".p4.spec"; $p4clientSpec = $this->path . "/" . $this->getClient() . ".p4.spec";
return $p4clientSpec; return $p4clientSpec;
} }
public function getUser() { public function getUser()
{
return $this->p4User; return $this->p4User;
} }
public function queryP4User(IOInterface $io) { public function queryP4User(IOInterface $io)
{
$this->getUser(); $this->getUser();
if (strlen($this->p4User) > 0) { if (strlen($this->p4User) > 0) {
return; return;
@ -185,14 +198,14 @@ class Perforce {
$this->p4User = $io->ask("Enter P4 User:"); $this->p4User = $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 {
$command = "export P4USER=$this->p4User"; $command = "export P4USER=$this->p4User";
} }
$result = $this->executeCommand($command); $result = $this->executeCommand($command);
} }
protected function getP4variable($name) { protected function getP4variable($name)
{
if ($this->windowsFlag) { if ($this->windowsFlag) {
$command = "p4 set"; $command = "p4 set";
$result = $this->executeCommand($command); $result = $this->executeCommand($command);
@ -201,10 +214,9 @@ class Perforce {
$fields = explode("=", $line); $fields = explode("=", $line);
if (strcmp($name, $fields[0]) == 0) { if (strcmp($name, $fields[0]) == 0) {
$index = strpos($fields[1], " "); $index = strpos($fields[1], " ");
if ($index === FALSE) { if ($index === false) {
$value = $fields[1]; $value = $fields[1];
} } else {
else {
$value = substr($fields[1], 0, $index); $value = substr($fields[1], 0, $index);
} }
$value = trim($value); $value = trim($value);
@ -212,8 +224,7 @@ class Perforce {
return $value; return $value;
} }
} }
} } else {
else {
$command = 'echo $' . $name; $command = 'echo $' . $name;
$result = trim($this->executeCommand($command)); $result = trim($this->executeCommand($command));
@ -221,7 +232,8 @@ class Perforce {
} }
} }
public function queryP4Password(IOInterface $io) { public function queryP4Password(IOInterface $io)
{
if (isset($this->p4Password)) { if (isset($this->p4Password)) {
return $this->p4Password; return $this->p4Password;
} }
@ -234,7 +246,8 @@ class Perforce {
return $password; return $password;
} }
public function generateP4Command($command, $useClient = TRUE) { public function generateP4Command($command, $useClient = true)
{
$p4Command = "p4 "; $p4Command = "p4 ";
$p4Command = $p4Command . "-u " . $this->getUser() . " "; $p4Command = $p4Command . "-u " . $this->getUser() . " ";
if ($useClient) { if ($useClient) {
@ -246,22 +259,25 @@ class Perforce {
return $p4Command; return $p4Command;
} }
public function isLoggedIn() { public function isLoggedIn()
$command = $this->generateP4Command("login -s", FALSE); {
$command = $this->generateP4Command("login -s", false);
$result = trim($this->executeCommand($command)); $result = trim($this->executeCommand($command));
$index = strpos($result, $this->getUser()); $index = strpos($result, $this->getUser());
if ($index === FALSE) { if ($index === false) {
return FALSE; return false;
} }
return TRUE; return true;
} }
public function connectClient() { public function connectClient()
{
$p4CreateClientCommand = $this->generateP4Command("client -i < " . $this->getP4ClientSpec()); $p4CreateClientCommand = $this->generateP4Command("client -i < " . $this->getP4ClientSpec());
$this->executeCommand($p4CreateClientCommand); $this->executeCommand($p4CreateClientCommand);
} }
public function syncCodeBase($label) { public function syncCodeBase($label)
{
$prevDir = getcwd(); $prevDir = getcwd();
chdir($this->path); chdir($this->path);
@ -278,7 +294,8 @@ class Perforce {
chdir($prevDir); chdir($prevDir);
} }
public function writeClientSpecToFile($spec) { public function writeClientSpecToFile($spec)
{
fwrite($spec, "Client: " . $this->getClient() . "\n\n"); fwrite($spec, "Client: " . $this->getClient() . "\n\n");
fwrite($spec, "Update: " . date("Y/m/d H:i:s") . "\n\n"); fwrite($spec, "Update: " . date("Y/m/d H:i:s") . "\n\n");
fwrite($spec, "Access: " . date("Y/m/d H:i:s") . "\n"); fwrite($spec, "Access: " . date("Y/m/d H:i:s") . "\n");
@ -292,17 +309,20 @@ class Perforce {
if ($this->isStream()) { if ($this->isStream()) {
fwrite($spec, "Stream:\n"); fwrite($spec, "Stream:\n");
fwrite($spec, " " . $this->getStreamWithoutLabel($this->p4Stream) . "\n"); fwrite($spec, " " . $this->getStreamWithoutLabel($this->p4Stream) . "\n");
} } else {
else {
fwrite( fwrite(
$spec, "View: " . $this->getStream() . "/... //" . $this->getClient() . "/" . str_replace( $spec,
"//", "", $this->getStream() "View: " . $this->getStream() . "/... //" . $this->getClient() . "/" . str_replace(
"//",
"",
$this->getStream()
) . "/... \n" ) . "/... \n"
); );
} }
} }
public function writeP4ClientSpec() { public function writeP4ClientSpec()
{
$clientSpec = $this->getP4ClientSpec(); $clientSpec = $this->getP4ClientSpec();
$spec = fopen($clientSpec, 'w'); $spec = fopen($clientSpec, 'w');
try { try {
@ -315,19 +335,21 @@ class Perforce {
} }
protected function read($pipe, $name) { protected function read($pipe, $name)
{
if (feof($pipe)) { if (feof($pipe)) {
return; return;
} }
$line = fgets($pipe); $line = fgets($pipe);
while ($line != FALSE) { while ($line != false) {
$line = fgets($pipe); $line = fgets($pipe);
} }
return; return;
} }
public function windowsLogin($password) { public function windowsLogin($password)
{
$descriptorspec = array( $descriptorspec = array(
0 => array("pipe", "r"), 0 => array("pipe", "r"),
1 => array("pipe", "w"), 1 => array("pipe", "w"),
@ -336,7 +358,7 @@ class Perforce {
$command = $this->generateP4Command(" login -a"); $command = $this->generateP4Command(" login -a");
$process = proc_open($command, $descriptorspec, $pipes); $process = proc_open($command, $descriptorspec, $pipes);
if (!is_resource($process)) { if (!is_resource($process)) {
return FALSE; return false;
} }
fwrite($pipes[0], $password); fwrite($pipes[0], $password);
fclose($pipes[0]); fclose($pipes[0]);
@ -353,54 +375,56 @@ class Perforce {
} }
public function p4Login(IOInterface $io) { public function p4Login(IOInterface $io)
{
$this->queryP4User($io); $this->queryP4User($io);
if (!$this->isLoggedIn()) { if (!$this->isLoggedIn()) {
$password = $this->queryP4Password($io); $password = $this->queryP4Password($io);
if ($this->windowsFlag) { if ($this->windowsFlag) {
$this->windowsLogin($password); $this->windowsLogin($password);
} } else {
else { $command = "echo $password | " . $this->generateP4Command(" login -a", false);
$command = "echo $password | " . $this->generateP4Command(" login -a", FALSE);
$this->executeCommand($command); $this->executeCommand($command);
} }
} }
} }
public static function checkServerExists($url, ProcessExecutor $process_executor) { public static function checkServerExists($url, ProcessExecutor $process_executor)
{
$process = $process_executor ? : new ProcessExecutor; $process = $process_executor ? : new ProcessExecutor;
$result = ""; $result = "";
$process->execute("p4 -p $url info -s", $result); $process->execute("p4 -p $url info -s", $result);
$index = strpos($result, "error"); $index = strpos($result, "error");
if ($index === FALSE) { if ($index === false) {
return TRUE; return true;
} }
return FALSE; return false;
} }
public function getComposerInformation($identifier) { public function getComposerInformation($identifier)
{
$index = strpos($identifier, "@"); $index = strpos($identifier, "@");
if ($index === FALSE) { if ($index === false) {
$composer_json = "$identifier/composer.json"; $composer_json = "$identifier/composer.json";
return $this->getComposerInformationFromPath($composer_json); return $this->getComposerInformationFromPath($composer_json);
} } else {
else {
return $this->getComposerInformationFromLabel($identifier, $index); return $this->getComposerInformationFromLabel($identifier, $index);
} }
} }
public function getComposerInformationFromPath($composer_json) { public function getComposerInformationFromPath($composer_json)
{
$command = $this->generateP4Command(" print $composer_json"); $command = $this->generateP4Command(" print $composer_json");
$result = $this->executeCommand($command); $result = $this->executeCommand($command);
$index = strpos($result, "{"); $index = strpos($result, "{");
if ($index === FALSE) { if ($index === false) {
return ""; return "";
} }
if ($index >= 0) { if ($index >= 0) {
$rawData = substr($result, $index); $rawData = substr($result, $index);
$composer_info = json_decode($rawData, TRUE); $composer_info = json_decode($rawData, true);
return $composer_info; return $composer_info;
} }
@ -408,14 +432,15 @@ class Perforce {
return ""; return "";
} }
public function getComposerInformationFromLabel($identifier, $index) { public function getComposerInformationFromLabel($identifier, $index)
{
$composer_json_path = substr($identifier, 0, $index) . "/composer.json" . substr($identifier, $index); $composer_json_path = substr($identifier, 0, $index) . "/composer.json" . substr($identifier, $index);
$command = $this->generateP4Command(" files $composer_json_path", FALSE); $command = $this->generateP4Command(" files $composer_json_path", false);
$result = $this->executeCommand($command); $result = $this->executeCommand($command);
$index2 = strpos($result, "no such file(s)."); $index2 = strpos($result, "no such file(s).");
if ($index2 === FALSE) { if ($index2 === false) {
$index3 = strpos($result, "change"); $index3 = strpos($result, "change");
if (!($index3 === FALSE)) { if (!($index3 === false)) {
$phrase = trim(substr($result, $index3)); $phrase = trim(substr($result, $index3));
$fields = explode(" ", $phrase); $fields = explode(" ", $phrase);
$id = $fields[1]; $id = $fields[1];
@ -428,12 +453,12 @@ class Perforce {
return ""; return "";
} }
public function getBranches() { public function getBranches()
{
$possible_branches = array(); $possible_branches = array();
if (!$this->isStream()) { if (!$this->isStream()) {
$possible_branches[$this->p4Branch] = $this->getStream(); $possible_branches[$this->p4Branch] = $this->getStream();
} } else {
else {
$command = $this->generateP4Command("streams //$this->p4Depot/..."); $command = $this->generateP4Command("streams //$this->p4Depot/...");
$result = $this->executeCommand($command); $result = $this->executeCommand($command);
$resArray = explode("\n", $result); $resArray = explode("\n", $result);
@ -451,14 +476,15 @@ class Perforce {
return $branches; return $branches;
} }
public function getTags() { public function getTags()
{
$command = $this->generateP4Command("labels"); $command = $this->generateP4Command("labels");
$result = $this->executeCommand($command); $result = $this->executeCommand($command);
$resArray = explode("\n", $result); $resArray = explode("\n", $result);
$tags = array(); $tags = array();
foreach ($resArray as $line) { foreach ($resArray as $line) {
$index = strpos($line, "Label"); $index = strpos($line, "Label");
if (!($index === FALSE)) { if (!($index === false)) {
$fields = explode(" ", $line); $fields = explode(" ", $line);
$tags[$fields[1]] = $this->getStream() . "@" . $fields[1]; $tags[$fields[1]] = $this->getStream() . "@" . $fields[1];
} }
@ -467,13 +493,14 @@ class Perforce {
return $tags; return $tags;
} }
public function checkStream() { public function checkStream()
$command = $this->generateP4Command("depots", FALSE); {
$command = $this->generateP4Command("depots", false);
$result = $this->executeCommand($command); $result = $this->executeCommand($command);
$resArray = explode("\n", $result); $resArray = explode("\n", $result);
foreach ($resArray as $line) { foreach ($resArray as $line) {
$index = strpos($line, "Depot"); $index = strpos($line, "Depot");
if (!($index === FALSE)) { if (!($index === false)) {
$fields = explode(" ", $line); $fields = explode(" ", $line);
if (strcmp($this->p4Depot, $fields[1]) === 0) { if (strcmp($this->p4Depot, $fields[1]) === 0) {
$this->p4DepotType = $fields[3]; $this->p4DepotType = $fields[3];
@ -483,10 +510,11 @@ class Perforce {
} }
} }
return FALSE; return false;
} }
protected function getChangeList($reference){ protected function getChangeList($reference)
{
$index = strpos($reference, "@"); $index = strpos($reference, "@");
if ($index === false) { if ($index === false) {
return; return;
@ -501,7 +529,9 @@ class Perforce {
$changeList = $fields[1]; $changeList = $fields[1];
return $changeList; return $changeList;
} }
public function getCommitLogs($fromReference, $toReference){
public function getCommitLogs($fromReference, $toReference)
{
$fromChangeList = $this->getChangeList($fromReference); $fromChangeList = $this->getChangeList($fromReference);
if ($fromChangeList == null) { if ($fromChangeList == null) {
return; return;

@ -5,9 +5,6 @@
* (c) Nils Adermann <naderman@naderman.de> * (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be> * Jordi Boggiano <j.boggiano@seld.be>
* *
* Contributor: Matt Whittom <Matt.Whittom@veteransunited.com>
* Date: 7/17/13
*
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
@ -22,14 +19,16 @@ use Composer\Repository\VcsRepository;
/** /**
* @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 $io;
private $config; private $config;
private $testPath; private $testPath;
public static $repository; public static $repository;
function setUp() { function setUp()
{
$this->testPath = sys_get_temp_dir() . '/composer-test'; $this->testPath = sys_get_temp_dir() . '/composer-test';
$this->config = new Config(); $this->config = new Config();
$this->config->merge( $this->config->merge(
@ -43,11 +42,16 @@ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase {
} }
public function testDoDownloadGetRepoConfig() { public function testDoDownloadGetRepoConfig()
{
$downloader = new PerforceDownloader($this->io, $this->config); $downloader = new PerforceDownloader($this->io, $this->config);
$package = $this->getMock('Composer\Package\PackageInterface'); $package = $this->getMock('Composer\Package\PackageInterface');
$repoConfig = array('url' => 'TEST_URL', 'p4user' => 'TEST_USER'); $repoConfig = array('url' => 'TEST_URL', 'p4user' => 'TEST_USER');
$repository = $this->getMock('Composer\Repository\VcsRepository', array('getRepoConfig'), array($repoConfig, $this->io, $this->config)); $repository = $this->getMock(
'Composer\Repository\VcsRepository',
array('getRepoConfig'),
array($repoConfig, $this->io, $this->config)
);
$package->expects($this->at(0)) $package->expects($this->at(0))
->method('getSourceReference') ->method('getSourceReference')
->will($this->returnValue("SOURCE_REF")); ->will($this->returnValue("SOURCE_REF"));
@ -63,13 +67,18 @@ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase {
$downloader->doDownload($package, $path); $downloader->doDownload($package, $path);
} }
public function testDoDownload() { public function testDoDownload()
{
$downloader = new PerforceDownloader($this->io, $this->config); $downloader = new PerforceDownloader($this->io, $this->config);
$repoConfig = array("depot" => "TEST_DEPOT", "branch" => "TEST_BRANCH", "p4user" => "TEST_USER"); $repoConfig = array("depot" => "TEST_DEPOT", "branch" => "TEST_BRANCH", "p4user" => "TEST_USER");
$port = "TEST_PORT"; $port = "TEST_PORT";
$path = "TEST_PATH"; $path = "TEST_PATH";
$process = $this->getmock('Composer\Util\ProcessExecutor'); $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")); $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)) $perforce->expects($this->at(0))
@ -95,6 +104,5 @@ class PerforceDownloaderTest extends \PHPUnit_Framework_TestCase {
->will($this->returnValue($label)); ->will($this->returnValue($label));
$path = $this->testPath; $path = $this->testPath;
$downloader->doDownload($package, $path); $downloader->doDownload($package, $path);
} }
} }

@ -5,9 +5,6 @@
* (c) Nils Adermann <naderman@naderman.de> * (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be> * Jordi Boggiano <j.boggiano@seld.be>
* *
* Contributor: matt-whittom
* Date: 7/17/13
*
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
@ -22,14 +19,16 @@ use Composer\Config;
/** /**
* @author Matt Whittom <Matt.Whittom@veteransunited.com> * @author Matt Whittom <Matt.Whittom@veteransunited.com>
*/ */
class PerforceDriverTest extends \PHPUnit_Framework_TestCase { class PerforceDriverTest extends \PHPUnit_Framework_TestCase
{
private $config; private $config;
private $io; private $io;
private $process; private $process;
private $remoteFileSystem; private $remoteFileSystem;
private $testPath; private $testPath;
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 = new Config();
$this->config->merge( $this->config->merge(
@ -42,15 +41,18 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase {
$this->io = $this->getMock('Composer\IO\IOInterface'); $this->io = $this->getMock('Composer\IO\IOInterface');
$this->process = $this->getMock('Composer\Util\ProcessExecutor'); $this->process = $this->getMock('Composer\Util\ProcessExecutor');
$this->remoteFileSystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')->disableOriginalConstructor()->getMock(); $this->remoteFileSystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')->disableOriginalConstructor()
->getMock();
} }
public function tearDown() { public function tearDown()
{
$fs = new Filesystem; $fs = new Filesystem;
$fs->removeDirectory($this->testPath); $fs->removeDirectory($this->testPath);
} }
public function testInitializeCapturesVariablesFromRepoConfig() { public function testInitializeCapturesVariablesFromRepoConfig()
{
$this->setUp(); $this->setUp();
$repo_config = array( $repo_config = array(
'url' => 'TEST_PERFORCE_URL', 'url' => 'TEST_PERFORCE_URL',
@ -59,7 +61,14 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase {
); );
$driver = new PerforceDriver($repo_config, $this->io, $this->config, $this->process, $this->remoteFileSystem); $driver = new PerforceDriver($repo_config, $this->io, $this->config, $this->process, $this->remoteFileSystem);
$process = $this->getMock('Composer\Util\ProcessExecutor'); $process = $this->getMock('Composer\Util\ProcessExecutor');
$arguments = array(array('depot'=>'TEST_DEPOT', 'branch'=>'TEST_BRANCH'), 'port'=>'TEST_PORT', 'path'=>$this->testPath, $process, true, "TEST"); $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); $perforce = $this->getMock('Composer\Util\Perforce', null, $arguments);
$driver->injectPerforce($perforce); $driver->injectPerforce($perforce);
$driver->initialize(); $driver->initialize();
@ -68,7 +77,8 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals("TEST_BRANCH_CONFIG", $driver->getBranch()); $this->assertEquals("TEST_BRANCH_CONFIG", $driver->getBranch());
} }
public function testInitializeLogsInAndConnectsClient() { public function testInitializeLogsInAndConnectsClient()
{
$this->setUp(); $this->setUp();
$repo_config = array( $repo_config = array(
'url' => 'TEST_PERFORCE_URL', 'url' => 'TEST_PERFORCE_URL',
@ -92,7 +102,8 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase {
$driver->initialize(); $driver->initialize();
} }
public function testHasComposerFile() { public function testHasComposerFile()
{
$this->setUp(); $this->setUp();
$repo_config = array( $repo_config = array(
'url' => 'TEST_PERFORCE_URL', 'url' => 'TEST_PERFORCE_URL',
@ -101,7 +112,14 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase {
); );
$driver = new PerforceDriver($repo_config, $this->io, $this->config, $this->process, $this->remoteFileSystem); $driver = new PerforceDriver($repo_config, $this->io, $this->config, $this->process, $this->remoteFileSystem);
$process = $this->getMock('Composer\Util\ProcessExecutor'); $process = $this->getMock('Composer\Util\ProcessExecutor');
$arguments = array(array('depot'=>'TEST_DEPOT', 'branch'=>'TEST_BRANCH'), 'port'=>'TEST_PORT', 'path'=>$this->testPath, $process, true, "TEST"); $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 = $this->getMock('Composer\Util\Perforce', array('getComposerInformation'), $arguments);
$perforce->expects($this->at(0)) $perforce->expects($this->at(0))
->method('getComposerInformation') ->method('getComposerInformation')
@ -113,6 +131,5 @@ class PerforceDriverTest extends \PHPUnit_Framework_TestCase {
$result = $driver->hasComposerFile($identifier); $result = $driver->hasComposerFile($identifier);
$this->assertTrue($result); $this->assertTrue($result);
} }
} }

@ -5,9 +5,6 @@
* (c) Nils Adermann <naderman@naderman.de> * (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be> * Jordi Boggiano <j.boggiano@seld.be>
* *
* Contributor: Matt Whittom <Matt.Whittom@veteransunited.com>
* Date: 7/17/13
*
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
@ -21,18 +18,26 @@ use Composer\Util\ProcessExecutor;
/** /**
* @author Matt Whittom <Matt.Whittom@veteransunited.com> * @author Matt Whittom <Matt.Whittom@veteransunited.com>
*/ */
class PerforceTest extends \PHPUnit_Framework_TestCase { class PerforceTest extends \PHPUnit_Framework_TestCase
{
protected $perforce; protected $perforce;
protected $processExecutor; protected $processExecutor;
public function setUp() { public function setUp()
{
$this->processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); $this->processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
$repoConfig = array("depot"=>"depot", "branch"=>"branch", "p4user"=>"user", "unique_perforce_client_name" => "TEST"); $repoConfig = array(
"depot" => "depot",
"branch" => "branch",
"p4user" => "user",
"unique_perforce_client_name" => "TEST"
);
$this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true); $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true);
} }
public function testGetClientWithoutStream() { public function testGetClientWithoutStream()
{
$client = $this->perforce->getClient(); $client = $this->perforce->getClient();
$hostname = gethostname(); $hostname = gethostname();
$timestamp = time(); $timestamp = time();
@ -41,7 +46,8 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals($expected, $client); $this->assertEquals($expected, $client);
} }
public function testGetClientFromStream() { public function testGetClientFromStream()
{
$this->setPerforceToStream(); $this->setPerforceToStream();
$client = $this->perforce->getClient(); $client = $this->perforce->getClient();
@ -50,12 +56,14 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals($expected, $client); $this->assertEquals($expected, $client);
} }
public function testGetStreamWithoutStream() { public function testGetStreamWithoutStream()
{
$stream = $this->perforce->getStream(); $stream = $this->perforce->getStream();
$this->assertEquals("//depot", $stream); $this->assertEquals("//depot", $stream);
} }
public function testGetStreamWithStream() { public function testGetStreamWithStream()
{
$this->setPerforceToStream(); $this->setPerforceToStream();
$stream = $this->perforce->getStream(); $stream = $this->perforce->getStream();
@ -63,30 +71,35 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
} }
public function testGetStreamWithoutLabelWithStreamWithoutLabel(){ public function testGetStreamWithoutLabelWithStreamWithoutLabel()
{
$stream = $this->perforce->getStreamWithoutLabel("//depot/branch"); $stream = $this->perforce->getStreamWithoutLabel("//depot/branch");
$this->assertEquals("//depot/branch", $stream); $this->assertEquals("//depot/branch", $stream);
} }
public function testGetStreamWithoutLabelWithStreamWithLabel(){ public function testGetStreamWithoutLabelWithStreamWithLabel()
{
$stream = $this->perforce->getStreamWithoutLabel("//depot/branching@label"); $stream = $this->perforce->getStreamWithoutLabel("//depot/branching@label");
$this->assertEquals("//depot/branching", $stream); $this->assertEquals("//depot/branching", $stream);
} }
public function testGetClientSpec() { public function testGetClientSpec()
{
$clientSpec = $this->perforce->getP4ClientSpec(); $clientSpec = $this->perforce->getP4ClientSpec();
$expected = "path/composer_perforce_TEST_depot.p4.spec"; $expected = "path/composer_perforce_TEST_depot.p4.spec";
$this->assertEquals($expected, $clientSpec); $this->assertEquals($expected, $clientSpec);
} }
public function testGenerateP4Command() { public function testGenerateP4Command()
{
$command = "do something"; $command = "do something";
$p4Command = $this->perforce->generateP4Command($command); $p4Command = $this->perforce->generateP4Command($command);
$expected = "p4 -u user -c composer_perforce_TEST_depot -p port do something"; $expected = "p4 -u user -c composer_perforce_TEST_depot -p port do something";
$this->assertEquals($expected, $p4Command); $this->assertEquals($expected, $p4Command);
} }
public function testQueryP4UserWithUserAlreadySet(){ public function testQueryP4UserWithUserAlreadySet()
{
$io = $this->getMock('Composer\IO\IOInterface'); $io = $this->getMock('Composer\IO\IOInterface');
$repoConfig = array("depot" => "depot", "branch" => "branch", "p4user" => "TEST_USER"); $repoConfig = array("depot" => "depot", "branch" => "branch", "p4user" => "TEST_USER");
@ -96,7 +109,8 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals("TEST_USER", $this->perforce->getUser()); $this->assertEquals("TEST_USER", $this->perforce->getUser());
} }
public function testQueryP4UserWithUserSetInP4VariablesWithWindowsOS(){ public function testQueryP4UserWithUserSetInP4VariablesWithWindowsOS()
{
$repoConfig = array("depot" => "depot", "branch" => "branch"); $repoConfig = array("depot" => "depot", "branch" => "branch");
$this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true, "TEST"); $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true, "TEST");
@ -105,13 +119,21 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$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($this->returnCallback(function($command, &$output) {$output = "P4USER=TEST_P4VARIABLE_USER\n"; return true;})); ->will(
$this->returnCallback(
function ($command, &$output) {
$output = "P4USER=TEST_P4VARIABLE_USER\n";
return true;
}
)
);
$this->perforce->queryP4user($io); $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"); $repoConfig = array("depot" => "depot", "branch" => "branch");
$this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST");
@ -120,13 +142,21 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$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($this->returnCallback(function($command, &$output) {$output = "TEST_P4VARIABLE_USER\n"; return true;})); ->will(
$this->returnCallback(
function ($command, &$output) {
$output = "TEST_P4VARIABLE_USER\n";
return true;
}
)
);
$this->perforce->queryP4user($io); $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"); $repoConfig = array("depot" => "depot", "branch" => "branch");
$this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST");
$io = $this->getMock('Composer\IO\IOInterface'); $io = $this->getMock('Composer\IO\IOInterface');
@ -140,7 +170,8 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$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"); $repoConfig = array("depot" => "depot", "branch" => "branch");
$this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true, "TEST"); $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, true, "TEST");
@ -159,7 +190,8 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$this->perforce->queryP4user($io); $this->perforce->queryP4user($io);
} }
public function testQueryP4UserStoresResponseToQueryForUserWithoutWindows(){ public function testQueryP4UserStoresResponseToQueryForUserWithoutWindows()
{
$repoConfig = array("depot" => "depot", "branch" => "branch"); $repoConfig = array("depot" => "depot", "branch" => "branch");
$this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST");
@ -178,8 +210,14 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$this->perforce->queryP4user($io); $this->perforce->queryP4user($io);
} }
public function testQueryP4PasswordWithPasswordAlreadySet(){ public function testQueryP4PasswordWithPasswordAlreadySet()
$repoConfig = array("depot"=>"depot", "branch"=>"branch", "p4user"=>"user", "p4password"=>"TEST_PASSWORD"); {
$repoConfig = array(
"depot" => "depot",
"branch" => "branch",
"p4user" => "user",
"p4password" => "TEST_PASSWORD"
);
$this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST");
$io = $this->getMock('Composer\IO\IOInterface'); $io = $this->getMock('Composer\IO\IOInterface');
@ -187,20 +225,29 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals("TEST_PASSWORD", $password); $this->assertEquals("TEST_PASSWORD", $password);
} }
public function testQueryP4PasswordWithPasswordSetInP4VariablesWithWindowsOS(){ public function testQueryP4PasswordWithPasswordSetInP4VariablesWithWindowsOS()
{
$io = $this->getMock('Composer\IO\IOInterface'); $io = $this->getMock('Composer\IO\IOInterface');
$expectedCommand = "p4 set"; $expectedCommand = "p4 set";
$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($this->returnCallback(function($command, &$output) {$output = "P4PASSWD=TEST_P4VARIABLE_PASSWORD\n"; return true;})); ->will(
$this->returnCallback(
function ($command, &$output) {
$output = "P4PASSWD=TEST_P4VARIABLE_PASSWORD\n";
return true;
}
)
);
$password = $this->perforce->queryP4Password($io); $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"); $repoConfig = array("depot" => "depot", "branch" => "branch", "p4user" => "user");
$this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST"); $this->perforce = new Perforce($repoConfig, "port", "path", $this->processExecutor, false, "TEST");
@ -209,13 +256,21 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$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($this->returnCallback(function($command, &$output) {$output = "TEST_P4VARIABLE_PASSWORD\n"; return true;})); ->will(
$this->returnCallback(
function ($command, &$output) {
$output = "TEST_P4VARIABLE_PASSWORD\n";
return true;
}
)
);
$password = $this->perforce->queryP4Password($io); $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'); $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)) $io->expects($this->at(0))
@ -227,12 +282,13 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals("TEST_QUERY_PASSWORD", $password); $this->assertEquals("TEST_QUERY_PASSWORD", $password);
} }
public function testWriteP4ClientSpecWithoutStream() { public function testWriteP4ClientSpecWithoutStream()
{
$stream = fopen("php://memory", 'w+'); $stream = fopen("php://memory", 'w+');
$this->perforce->writeClientSpecToFile($stream); $this->perforce->writeClientSpecToFile($stream);
rewind($stream); rewind($stream);
$expectedArray = $this->getExpectedClientSpec(FALSE); $expectedArray = $this->getExpectedClientSpec(false);
try { try {
foreach ($expectedArray as $expected) { foreach ($expectedArray as $expected) {
$this->assertStringStartsWith($expected, fgets($stream)); $this->assertStringStartsWith($expected, fgets($stream));
@ -245,14 +301,15 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
fclose($stream); fclose($stream);
} }
public function testWriteP4ClientSpecWithStream() { public function testWriteP4ClientSpecWithStream()
{
$this->setPerforceToStream(); $this->setPerforceToStream();
$stream = fopen("php://memory", 'w+'); $stream = fopen("php://memory", 'w+');
$this->perforce->writeClientSpecToFile($stream); $this->perforce->writeClientSpecToFile($stream);
rewind($stream); rewind($stream);
$expectedArray = $this->getExpectedClientSpec(TRUE); $expectedArray = $this->getExpectedClientSpec(true);
try { try {
foreach ($expectedArray as $expected) { foreach ($expectedArray as $expected) {
$this->assertStringStartsWith($expected, fgets($stream)); $this->assertStringStartsWith($expected, fgets($stream));
@ -265,7 +322,8 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
fclose($stream); fclose($stream);
} }
public function testIsLoggedIn() { public function testIsLoggedIn()
{
$expectedCommand = "p4 -u user -p port login -s"; $expectedCommand = "p4 -u user -p port login -s";
$this->processExecutor->expects($this->at(0)) $this->processExecutor->expects($this->at(0))
->method('execute') ->method('execute')
@ -275,7 +333,8 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$this->perforce->isLoggedIn(); $this->perforce->isLoggedIn();
} }
public function testConnectClient() { public function testConnectClient()
{
$expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port client -i < path/composer_perforce_TEST_depot.p4.spec"; $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port client -i < path/composer_perforce_TEST_depot.p4.spec";
$this->processExecutor->expects($this->at(0)) $this->processExecutor->expects($this->at(0))
->method('execute') ->method('execute')
@ -285,70 +344,112 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$this->perforce->connectClient(); $this->perforce->connectClient();
} }
public function testGetBranchesWithStream() { public function testGetBranchesWithStream()
{
$this->setPerforceToStream(); $this->setPerforceToStream();
$expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port streams //depot/..."; $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port streams //depot/...";
$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($this->returnCallback(function($command, &$output) {$output = "Stream //depot/branch mainline none 'branch'\n"; return true;})); ->will(
$this->returnCallback(
function ($command, &$output) {
$output = "Stream //depot/branch mainline none 'branch'\n";
return true;
}
)
);
$branches = $this->perforce->getBranches(); $branches = $this->perforce->getBranches();
$this->assertEquals("//depot/branch", $branches['master']); $this->assertEquals("//depot/branch", $branches['master']);
} }
public function testGetBranchesWithoutStream() { public function testGetBranchesWithoutStream()
{
$branches = $this->perforce->getBranches(); $branches = $this->perforce->getBranches();
$this->assertEquals("//depot", $branches['master']); $this->assertEquals("//depot", $branches['master']);
} }
public function testGetTagsWithoutStream() { public function testGetTagsWithoutStream()
{
$expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port labels"; $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port labels";
$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($this->returnCallback(function($command, &$output) {$output = "Label 0.0.1 2013/07/31 'First Label!'\nLabel 0.0.2 2013/08/01 'Second Label!'\n"; return true;})); ->will(
$this->returnCallback(
function ($command, &$output) {
$output = "Label 0.0.1 2013/07/31 'First Label!'\nLabel 0.0.2 2013/08/01 'Second Label!'\n";
return true;
}
)
);
$tags = $this->perforce->getTags(); $tags = $this->perforce->getTags();
$this->assertEquals("//depot@0.0.1", $tags['0.0.1']); $this->assertEquals("//depot@0.0.1", $tags['0.0.1']);
$this->assertEquals("//depot@0.0.2", $tags['0.0.2']); $this->assertEquals("//depot@0.0.2", $tags['0.0.2']);
} }
public function testGetTagsWithStream() { public function testGetTagsWithStream()
{
$this->setPerforceToStream(); $this->setPerforceToStream();
$expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port labels"; $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port labels";
$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($this->returnCallback(function($command, &$output) {$output = "Label 0.0.1 2013/07/31 'First Label!'\nLabel 0.0.2 2013/08/01 'Second Label!'\n"; return true;})); ->will(
$this->returnCallback(
function ($command, &$output) {
$output = "Label 0.0.1 2013/07/31 'First Label!'\nLabel 0.0.2 2013/08/01 'Second Label!'\n";
return true;
}
)
);
$tags = $this->perforce->getTags(); $tags = $this->perforce->getTags();
$this->assertEquals("//depot/branch@0.0.1", $tags['0.0.1']); $this->assertEquals("//depot/branch@0.0.1", $tags['0.0.1']);
$this->assertEquals("//depot/branch@0.0.2", $tags['0.0.2']); $this->assertEquals("//depot/branch@0.0.2", $tags['0.0.2']);
} }
public function testCheckStreamWithoutStream() { public function testCheckStreamWithoutStream()
{
$result = $this->perforce->checkStream("depot"); $result = $this->perforce->checkStream("depot");
$this->assertFalse($result); $this->assertFalse($result);
$this->assertFalse($this->perforce->isStream()); $this->assertFalse($this->perforce->isStream());
} }
public function testCheckStreamWithStream() { public function testCheckStreamWithStream()
{
$this->processExecutor->expects($this->any())->method('execute') $this->processExecutor->expects($this->any())->method('execute')
->will($this->returnCallback(function($command, &$output) {$output = "Depot depot 2013/06/25 stream /p4/1/depots/depot/... 'Created by Me'"; return true;})); ->will(
$this->returnCallback(
function ($command, &$output) {
$output = "Depot depot 2013/06/25 stream /p4/1/depots/depot/... 'Created by Me'";
return true;
}
)
);
$result = $this->perforce->checkStream("depot"); $result = $this->perforce->checkStream("depot");
$this->assertTrue($result); $this->assertTrue($result);
$this->assertTrue($this->perforce->isStream()); $this->assertTrue($this->perforce->isStream());
} }
public function testGetComposerInformationWithoutLabelWithoutStream() { public function testGetComposerInformationWithoutLabelWithoutStream()
{
$expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json"; $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json";
$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($this->returnCallback(function($command, &$output) {$output = PerforceTest::getComposerJson(); return true;})); ->will(
$this->returnCallback(
function ($command, &$output) {
$output = PerforceTest::getComposerJson();
return true;
}
)
);
$result = $this->perforce->getComposerInformation("//depot"); $result = $this->perforce->getComposerInformation("//depot");
$expected = array( $expected = array(
@ -360,18 +461,33 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
public function testGetComposerInformationWithLabelWithoutStream() { public function testGetComposerInformationWithLabelWithoutStream()
{
$expectedCommand = "p4 -u user -p port files //depot/composer.json@0.0.1"; $expectedCommand = "p4 -u user -p port files //depot/composer.json@0.0.1";
$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($this->returnCallback(function($command, &$output) {$output = "//depot/composer.json#1 - branch change 10001 (text)"; return true;})); ->will(
$this->returnCallback(
function ($command, &$output) {
$output = "//depot/composer.json#1 - branch change 10001 (text)";
return true;
}
)
);
$expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json@10001"; $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port print //depot/composer.json@10001";
$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->returnCallback(function($command, &$output) {$output = PerforceTest::getComposerJson(); return true;})); ->will(
$this->returnCallback(
function ($command, &$output) {
$output = PerforceTest::getComposerJson();
return true;
}
)
);
$result = $this->perforce->getComposerInformation("//depot@0.0.1"); $result = $this->perforce->getComposerInformation("//depot@0.0.1");
@ -384,14 +500,22 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
public function testGetComposerInformationWithoutLabelWithStream() { public function testGetComposerInformationWithoutLabelWithStream()
{
$this->setPerforceToStream(); $this->setPerforceToStream();
$expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json"; $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json";
$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($this->returnCallback(function($command, &$output) {$output = PerforceTest::getComposerJson(); return true;})); ->will(
$this->returnCallback(
function ($command, &$output) {
$output = PerforceTest::getComposerJson();
return true;
}
)
);
$result = $this->perforce->getComposerInformation("//depot/branch"); $result = $this->perforce->getComposerInformation("//depot/branch");
@ -404,19 +528,34 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
public function testGetComposerInformationWithLabelWithStream() { public function testGetComposerInformationWithLabelWithStream()
{
$this->setPerforceToStream(); $this->setPerforceToStream();
$expectedCommand = "p4 -u user -p port files //depot/branch/composer.json@0.0.1"; $expectedCommand = "p4 -u user -p port files //depot/branch/composer.json@0.0.1";
$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($this->returnCallback(function($command, &$output) {$output = "//depot/composer.json#1 - branch change 10001 (text)"; return true;})); ->will(
$this->returnCallback(
function ($command, &$output) {
$output = "//depot/composer.json#1 - branch change 10001 (text)";
return true;
}
)
);
$expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json@10001"; $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port print //depot/branch/composer.json@10001";
$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->returnCallback(function($command, &$output) {$output = PerforceTest::getComposerJson(); return true;})); ->will(
$this->returnCallback(
function ($command, &$output) {
$output = PerforceTest::getComposerJson();
return true;
}
)
);
$result = $this->perforce->getComposerInformation("//depot/branch@0.0.1"); $result = $this->perforce->getComposerInformation("//depot/branch@0.0.1");
@ -429,7 +568,8 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
public function testSyncCodeBaseWithoutStream() { public function testSyncCodeBaseWithoutStream()
{
$expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port sync -f @label"; $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot -p port sync -f @label";
$this->processExecutor->expects($this->at(1)) $this->processExecutor->expects($this->at(1))
->method('execute') ->method('execute')
@ -439,7 +579,8 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$this->perforce->syncCodeBase("label"); $this->perforce->syncCodeBase("label");
} }
public function testSyncCodeBaseWithStream() { public function testSyncCodeBaseWithStream()
{
$this->setPerforceToStream(); $this->setPerforceToStream();
$expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port sync -f @label"; $expectedCommand = "p4 -u user -c composer_perforce_TEST_depot_branch -p port sync -f @label";
$this->processExecutor->expects($this->at(1)) $this->processExecutor->expects($this->at(1))
@ -450,7 +591,8 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$this->perforce->syncCodeBase("label"); $this->perforce->syncCodeBase("label");
} }
public function testCheckServerExists() { public function testCheckServerExists()
{
$processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
$expectedCommand = "p4 -p perforce.does.exist:port info -s"; $expectedCommand = "p4 -p perforce.does.exist:port info -s";
@ -463,7 +605,8 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$this->assertTrue($result); $this->assertTrue($result);
} }
public function testCheckServerExistsWithFailure() { public function testCheckServerExistsWithFailure()
{
$processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
$expectedCommand = "p4 -p perforce.does.not.exist:port info -s"; $expectedCommand = "p4 -p perforce.does.not.exist:port info -s";
@ -476,7 +619,8 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
$this->assertTrue($result); $this->assertTrue($result);
} }
public static function getComposerJson() { public static function getComposerJson()
{
$composer_json = array( $composer_json = array(
'{', '{',
'"name": "test/perforce",', '"name": "test/perforce",',
@ -492,7 +636,8 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
return implode($composer_json); return implode($composer_json);
} }
private function getExpectedClientSpec($withStream) { private function getExpectedClientSpec($withStream)
{
$expectedArray = array( $expectedArray = array(
"Client: composer_perforce_TEST_depot", "Client: composer_perforce_TEST_depot",
"\n", "\n",
@ -516,15 +661,15 @@ class PerforceTest extends \PHPUnit_Framework_TestCase {
if ($withStream) { if ($withStream) {
$expectedArray[] = "Stream:"; $expectedArray[] = "Stream:";
$expectedArray[] = " //depot/branch"; $expectedArray[] = " //depot/branch";
} } else {
else {
$expectedArray[] = "View: //depot/... //composer_perforce_TEST_depot/depot/..."; $expectedArray[] = "View: //depot/... //composer_perforce_TEST_depot/depot/...";
} }
return $expectedArray; return $expectedArray;
} }
private function setPerforceToStream(){ private function setPerforceToStream()
{
$this->perforce->setStream("//depot/branch"); $this->perforce->setStream("//depot/branch");
} }
} }

Loading…
Cancel
Save