1
0
Fork 0
composer/tests/Composer/Test/Util/PerforceTest.php

742 lines
26 KiB
PHP
Raw Normal View History

2013-08-09 19:24:58 +00:00
<?php
2013-10-11 23:12:02 +00:00
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
2013-08-09 19:24:58 +00:00
*/
namespace Composer\Test\Util;
use Composer\Util\Perforce;
2020-02-07 03:18:45 +00:00
use Composer\Test\TestCase;
use Composer\Util\ProcessExecutor;
2013-08-09 19:24:58 +00:00
/**
* @author Matt Whittom <Matt.Whittom@veteransunited.com>
*/
class PerforceTest extends TestCase
{
2021-10-27 14:18:46 +00:00
/** @var Perforce */
2013-08-09 19:24:58 +00:00
protected $perforce;
2021-10-27 14:18:46 +00:00
/** @var \PHPUnit\Framework\MockObject\MockObject&\Composer\Util\ProcessExecutor */
2013-08-09 19:24:58 +00:00
protected $processExecutor;
2021-10-27 14:18:46 +00:00
/** @var array<string, string> */
protected $repoConfig;
2021-10-27 14:18:46 +00:00
/** @var \PHPUnit\Framework\MockObject\MockObject&\Composer\IO\IOInterface */
protected $io;
2013-08-09 19:24:58 +00:00
2017-03-08 14:07:29 +00:00
const TEST_DEPOT = 'depot';
const TEST_BRANCH = 'branch';
const TEST_P4USER = 'user';
const TEST_CLIENT_NAME = 'TEST';
2017-03-08 14:07:29 +00:00
const TEST_PORT = 'port';
const TEST_PATH = 'path';
protected function setUp()
{
$this->processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
$this->repoConfig = $this->getTestRepoConfig();
$this->io = $this->getMockIOInterface();
$this->createNewPerforceWithWindowsFlag(true);
}
2021-10-27 14:18:46 +00:00
/**
* @return array<string, string>
*/
public function getTestRepoConfig()
{
return array(
2017-03-08 14:07:29 +00:00
'depot' => self::TEST_DEPOT,
'branch' => self::TEST_BRANCH,
'p4user' => self::TEST_P4USER,
2015-09-28 09:51:14 +00:00
'unique_perforce_client_name' => self::TEST_CLIENT_NAME,
);
}
2021-10-27 14:18:46 +00:00
/**
* @return \PHPUnit\Framework\MockObject\MockObject&\Composer\IO\IOInterface
*/
public function getMockIOInterface()
{
return $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
}
2021-10-27 14:18:46 +00:00
/**
* @param bool $flag
*
* @return void
*/
protected function createNewPerforceWithWindowsFlag($flag)
{
$this->perforce = new Perforce($this->repoConfig, self::TEST_PORT, self::TEST_PATH, $this->processExecutor, $flag, $this->io);
}
public function testGetClientWithoutStream()
{
$client = $this->perforce->getClient();
$hostname = gethostname();
$timestamp = time();
$expected = 'composer_perforce_TEST_depot';
2013-08-09 19:24:58 +00:00
$this->assertEquals($expected, $client);
}
public function testGetClientFromStream()
{
$this->setPerforceToStream();
$client = $this->perforce->getClient();
2013-08-09 19:24:58 +00:00
$expected = 'composer_perforce_TEST_depot_branch';
2013-08-09 19:24:58 +00:00
$this->assertEquals($expected, $client);
}
public function testGetStreamWithoutStream()
{
$stream = $this->perforce->getStream();
2013-08-09 19:24:58 +00:00
$this->assertEquals("//depot", $stream);
}
public function testGetStreamWithStream()
{
$this->setPerforceToStream();
$stream = $this->perforce->getStream();
$this->assertEquals('//depot/branch', $stream);
2013-08-09 19:24:58 +00:00
}
public function testGetStreamWithoutLabelWithStreamWithoutLabel()
{
$stream = $this->perforce->getStreamWithoutLabel('//depot/branch');
$this->assertEquals('//depot/branch', $stream);
}
public function testGetStreamWithoutLabelWithStreamWithLabel()
{
$stream = $this->perforce->getStreamWithoutLabel('//depot/branching@label');
$this->assertEquals('//depot/branching', $stream);
2013-08-09 19:24:58 +00:00
}
public function testGetClientSpec()
{
$clientSpec = $this->perforce->getP4ClientSpec();
$expected = 'path/composer_perforce_TEST_depot.p4.spec';
2013-08-09 19:24:58 +00:00
$this->assertEquals($expected, $clientSpec);
}
public function testGenerateP4Command()
{
$command = 'do something';
$p4Command = $this->perforce->generateP4Command($command);
$expected = 'p4 -u user -c composer_perforce_TEST_depot -p port do something';
2013-08-09 19:24:58 +00:00
$this->assertEquals($expected, $p4Command);
}
public function testQueryP4UserWithUserAlreadySet()
{
$this->perforce->queryP4user();
$this->assertEquals(self::TEST_P4USER, $this->perforce->getUser());
2013-08-09 19:24:58 +00:00
}
public function testQueryP4UserWithUserSetInP4VariablesWithWindowsOS()
{
$this->createNewPerforceWithWindowsFlag(true);
$this->perforce->setUser(null);
$expectedCommand = 'p4 set';
2014-06-10 14:02:44 +00:00
$callback = function ($command, &$output) {
2017-03-08 14:07:29 +00:00
$output = 'P4USER=TEST_P4VARIABLE_USER' . PHP_EOL;
2014-06-10 14:02:44 +00:00
2017-03-08 14:07:29 +00:00
return true;
};
2013-08-09 19:24:58 +00:00
$this->processExecutor->expects($this->at(0))
->method('execute')
->with($this->equalTo($expectedCommand))
->will($this->returnCallback($callback));
$this->perforce->queryP4user();
$this->assertEquals('TEST_P4VARIABLE_USER', $this->perforce->getUser());
2013-08-09 19:24:58 +00:00
}
public function testQueryP4UserWithUserSetInP4VariablesNotWindowsOS()
{
$this->createNewPerforceWithWindowsFlag(false);
$this->perforce->setUser(null);
2013-08-09 19:24:58 +00:00
$expectedCommand = 'echo $P4USER';
2014-06-10 14:02:44 +00:00
$callback = function ($command, &$output) {
2017-03-08 14:07:29 +00:00
$output = 'TEST_P4VARIABLE_USER' . PHP_EOL;
2014-06-10 14:02:44 +00:00
2017-03-08 14:07:29 +00:00
return true;
};
2013-08-09 19:24:58 +00:00
$this->processExecutor->expects($this->at(0))
->method('execute')
->with($this->equalTo($expectedCommand))
->will($this->returnCallback($callback));
$this->perforce->queryP4user();
$this->assertEquals('TEST_P4VARIABLE_USER', $this->perforce->getUser());
2013-08-09 19:24:58 +00:00
}
public function testQueryP4UserQueriesForUser()
{
$this->perforce->setUser(null);
$expectedQuestion = 'Enter P4 User:';
$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());
2013-08-09 19:24:58 +00:00
}
public function testQueryP4UserStoresResponseToQueryForUserWithWindows()
{
$this->createNewPerforceWithWindowsFlag(true);
$this->perforce->setUser(null);
$expectedQuestion = 'Enter P4 User:';
2017-03-08 14:07:29 +00:00
$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'));
2013-08-09 19:24:58 +00:00
$this->processExecutor->expects($this->at(1))
->method('execute')
->with($this->equalTo($expectedCommand))
->will($this->returnValue(0));
$this->perforce->queryP4user();
2013-08-09 19:24:58 +00:00
}
public function testQueryP4UserStoresResponseToQueryForUserWithoutWindows()
{
$this->createNewPerforceWithWindowsFlag(false);
$this->perforce->setUser(null);
$expectedQuestion = 'Enter P4 User:';
2017-03-08 14:07:29 +00:00
$expectedCommand = 'export P4USER=TEST_QUERY_USER';
$this->io->expects($this->at(0))
->method('ask')
->with($this->equalTo($expectedQuestion))
->will($this->returnValue('TEST_QUERY_USER'));
2013-08-09 19:24:58 +00:00
$this->processExecutor->expects($this->at(1))
->method('execute')
->with($this->equalTo($expectedCommand))
->will($this->returnValue(0));
$this->perforce->queryP4user();
2013-08-09 19:24:58 +00:00
}
public function testQueryP4PasswordWithPasswordAlreadySet()
{
$repoConfig = array(
2017-03-08 14:07:29 +00:00
'depot' => 'depot',
'branch' => 'branch',
'p4user' => 'user',
2015-09-28 09:51:14 +00:00
'p4password' => 'TEST_PASSWORD',
);
$this->perforce = new Perforce($repoConfig, 'port', 'path', $this->processExecutor, false, $this->getMockIOInterface());
$password = $this->perforce->queryP4Password();
$this->assertEquals('TEST_PASSWORD', $password);
2013-08-09 19:24:58 +00:00
}
public function testQueryP4PasswordWithPasswordSetInP4VariablesWithWindowsOS()
{
$this->createNewPerforceWithWindowsFlag(true);
$expectedCommand = 'p4 set';
2014-06-10 14:02:44 +00:00
$callback = function ($command, &$output) {
2017-03-08 14:07:29 +00:00
$output = 'P4PASSWD=TEST_P4VARIABLE_PASSWORD' . PHP_EOL;
2014-06-10 14:02:44 +00:00
2017-03-08 14:07:29 +00:00
return true;
};
2013-08-09 19:24:58 +00:00
$this->processExecutor->expects($this->at(0))
->method('execute')
->with($this->equalTo($expectedCommand))
->will($this->returnCallback($callback));
$password = $this->perforce->queryP4Password();
$this->assertEquals('TEST_P4VARIABLE_PASSWORD', $password);
2013-08-09 19:24:58 +00:00
}
public function testQueryP4PasswordWithPasswordSetInP4VariablesNotWindowsOS()
{
$this->createNewPerforceWithWindowsFlag(false);
2013-08-09 19:24:58 +00:00
$expectedCommand = 'echo $P4PASSWD';
2014-06-10 14:02:44 +00:00
$callback = function ($command, &$output) {
2017-03-08 14:07:29 +00:00
$output = 'TEST_P4VARIABLE_PASSWORD' . PHP_EOL;
2014-06-10 14:02:44 +00:00
2017-03-08 14:07:29 +00:00
return true;
};
2013-08-09 19:24:58 +00:00
$this->processExecutor->expects($this->at(0))
->method('execute')
->with($this->equalTo($expectedCommand))
->will($this->returnCallback($callback));
$password = $this->perforce->queryP4Password();
$this->assertEquals('TEST_P4VARIABLE_PASSWORD', $password);
2013-08-09 19:24:58 +00:00
}
public function testQueryP4PasswordQueriesForPassword()
{
$expectedQuestion = 'Enter password for Perforce user user: ';
$this->io->expects($this->at(0))
->method('askAndHideAnswer')
->with($this->equalTo($expectedQuestion))
->will($this->returnValue('TEST_QUERY_PASSWORD'));
2013-08-09 19:24:58 +00:00
$password = $this->perforce->queryP4Password();
$this->assertEquals('TEST_QUERY_PASSWORD', $password);
2013-08-09 19:24:58 +00:00
}
public function testWriteP4ClientSpecWithoutStream()
{
$stream = fopen('php://memory', 'w+');
$this->perforce->writeClientSpecToFile($stream);
rewind($stream);
$expectedArray = $this->getExpectedClientSpec(false);
2013-08-09 19:24:58 +00:00
try {
foreach ($expectedArray as $expected) {
$this->assertStringStartsWith($expected, fgets($stream));
2013-08-09 19:24:58 +00:00
}
$this->assertFalse(fgets($stream));
} catch (\Exception $e) {
fclose($stream);
2013-08-09 19:24:58 +00:00
throw $e;
}
fclose($stream);
2013-08-09 19:24:58 +00:00
}
public function testWriteP4ClientSpecWithStream()
{
$this->setPerforceToStream();
$stream = fopen('php://memory', 'w+');
$this->perforce->writeClientSpecToFile($stream);
rewind($stream);
$expectedArray = $this->getExpectedClientSpec(true);
2013-08-09 19:24:58 +00:00
try {
foreach ($expectedArray as $expected) {
$this->assertStringStartsWith($expected, fgets($stream));
2013-08-09 19:24:58 +00:00
}
$this->assertFalse(fgets($stream));
} catch (\Exception $e) {
fclose($stream);
2013-08-09 19:24:58 +00:00
throw $e;
}
fclose($stream);
2013-08-09 19:24:58 +00:00
}
public function testIsLoggedIn()
{
$expectedCommand = 'p4 -u user -p port login -s';
2013-08-09 19:24:58 +00:00
$this->processExecutor->expects($this->at(0))
->method('execute')
->with($this->equalTo($expectedCommand), $this->equalTo(null))
->will($this->returnValue(0));
2013-08-09 19:24:58 +00:00
$this->perforce->isLoggedIn();
2013-08-09 19:24:58 +00:00
}
public function testConnectClient()
{
$expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port client -i < '.ProcessExecutor::escape('path/composer_perforce_TEST_depot.p4.spec');
2013-08-09 19:24:58 +00:00
$this->processExecutor->expects($this->at(0))
->method('execute')
->with($this->equalTo($expectedCommand), $this->equalTo(null))
->will($this->returnValue(0));
2013-08-09 19:24:58 +00:00
$this->perforce->connectClient();
}
public function testGetBranchesWithStream()
{
$this->setPerforceToStream();
$expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port streams '.ProcessExecutor::escape('//depot/...');
2013-08-09 19:24:58 +00:00
$this->processExecutor->expects($this->at(0))
->method('execute')
->with($this->equalTo($expectedCommand))
->will(
$this->returnCallback(
function ($command, &$output) {
$output = 'Stream //depot/branch mainline none \'branch\'' . PHP_EOL;
return true;
}
)
);
$expectedCommand2 = 'p4 -u user -p port changes '.ProcessExecutor::escape('//depot/branch/...');
2014-06-10 14:02:44 +00:00
$expectedCallback = function ($command, &$output) {
2017-03-08 14:07:29 +00:00
$output = 'Change 1234 on 2014/03/19 by Clark.Stuth@Clark.Stuth_test_client \'test changelist\'';
2014-06-10 14:02:44 +00:00
2017-03-08 14:07:29 +00:00
return true;
};
$this->processExecutor->expects($this->at(1))
->method('execute')
->with($this->equalTo($expectedCommand2))
->will($this->returnCallback($expectedCallback));
2013-08-09 19:24:58 +00:00
$branches = $this->perforce->getBranches();
$this->assertEquals('//depot/branch@1234', $branches['master']);
2013-08-09 19:24:58 +00:00
}
public function testGetBranchesWithoutStream()
{
$expectedCommand = 'p4 -u user -p port changes '.ProcessExecutor::escape('//depot/...');
2014-06-10 14:02:44 +00:00
$expectedCallback = function ($command, &$output) {
2017-03-08 14:07:29 +00:00
$output = 'Change 5678 on 2014/03/19 by Clark.Stuth@Clark.Stuth_test_client \'test changelist\'';
2014-06-10 14:02:44 +00:00
2017-03-08 14:07:29 +00:00
return true;
};
$this->processExecutor->expects($this->once())
->method('execute')
->with($this->equalTo($expectedCommand))
->will($this->returnCallback($expectedCallback));
2013-08-09 19:24:58 +00:00
$branches = $this->perforce->getBranches();
$this->assertEquals('//depot@5678', $branches['master']);
2013-08-09 19:24:58 +00:00
}
public function testGetTagsWithoutStream()
{
$expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port labels';
2013-08-09 19:24:58 +00:00
$this->processExecutor->expects($this->at(0))
->method('execute')
->with($this->equalTo($expectedCommand))
->will(
$this->returnCallback(
function ($command, &$output) {
$output = 'Label 0.0.1 2013/07/31 \'First Label!\'' . PHP_EOL . 'Label 0.0.2 2013/08/01 \'Second Label!\'' . PHP_EOL;
return true;
}
)
);
2013-08-09 19:24:58 +00:00
$tags = $this->perforce->getTags();
$this->assertEquals('//depot@0.0.1', $tags['0.0.1']);
$this->assertEquals('//depot@0.0.2', $tags['0.0.2']);
2013-08-09 19:24:58 +00:00
}
public function testGetTagsWithStream()
{
$this->setPerforceToStream();
$expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port labels';
2013-08-09 19:24:58 +00:00
$this->processExecutor->expects($this->at(0))
->method('execute')
->with($this->equalTo($expectedCommand))
->will(
$this->returnCallback(
function ($command, &$output) {
$output = 'Label 0.0.1 2013/07/31 \'First Label!\'' . PHP_EOL . 'Label 0.0.2 2013/08/01 \'Second Label!\'' . PHP_EOL;
return true;
}
)
);
2013-08-09 19:24:58 +00:00
$tags = $this->perforce->getTags();
$this->assertEquals('//depot/branch@0.0.1', $tags['0.0.1']);
$this->assertEquals('//depot/branch@0.0.2', $tags['0.0.2']);
2013-08-09 19:24:58 +00:00
}
public function testCheckStreamWithoutStream()
{
2021-10-27 14:18:46 +00:00
$result = $this->perforce->checkStream();
2013-08-09 19:24:58 +00:00
$this->assertFalse($result);
$this->assertFalse($this->perforce->isStream());
2013-08-09 19:24:58 +00:00
}
public function testCheckStreamWithStream()
{
$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;
}
)
);
2021-10-27 14:18:46 +00:00
$result = $this->perforce->checkStream();
$this->assertTrue($result);
$this->assertTrue($this->perforce->isStream());
2013-08-09 19:24:58 +00:00
}
public function testGetComposerInformationWithoutLabelWithoutStream()
{
$expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port print '.ProcessExecutor::escape('//depot/composer.json');
2013-08-09 19:24:58 +00:00
$this->processExecutor->expects($this->at(0))
->method('execute')
->with($this->equalTo($expectedCommand))
->will(
$this->returnCallback(
function ($command, &$output) {
$output = PerforceTest::getComposerJson();
return true;
}
)
);
2013-08-09 19:24:58 +00:00
$result = $this->perforce->getComposerInformation('//depot');
2013-08-09 19:24:58 +00:00
$expected = array(
2017-03-08 14:07:29 +00:00
'name' => 'test/perforce',
'description' => 'Basic project for testing',
'minimum-stability' => 'dev',
2017-03-08 14:07:29 +00:00
'autoload' => array('psr-0' => array()),
2013-08-09 19:24:58 +00:00
);
$this->assertEquals($expected, $result);
}
public function testGetComposerInformationWithLabelWithoutStream()
{
$expectedCommand = 'p4 -u user -p port files '.ProcessExecutor::escape('//depot/composer.json@0.0.1');
2013-08-09 19:24:58 +00:00
$this->processExecutor->expects($this->at(0))
->method('execute')
->with($this->equalTo($expectedCommand))
->will(
$this->returnCallback(
function ($command, &$output) {
$output = '//depot/composer.json#1 - branch change 10001 (text)';
return true;
}
)
);
2013-08-09 19:24:58 +00:00
$expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port print '.ProcessExecutor::escape('//depot/composer.json@10001');
2013-08-09 19:24:58 +00:00
$this->processExecutor->expects($this->at(1))
->method('execute')
->with($this->equalTo($expectedCommand))
->will(
$this->returnCallback(
function ($command, &$output) {
$output = PerforceTest::getComposerJson();
return true;
}
)
);
2013-08-09 19:24:58 +00:00
$result = $this->perforce->getComposerInformation('//depot@0.0.1');
2013-08-09 19:24:58 +00:00
$expected = array(
2017-03-08 14:07:29 +00:00
'name' => 'test/perforce',
'description' => 'Basic project for testing',
'minimum-stability' => 'dev',
2017-03-08 14:07:29 +00:00
'autoload' => array('psr-0' => array()),
2013-08-09 19:24:58 +00:00
);
$this->assertEquals($expected, $result);
}
public function testGetComposerInformationWithoutLabelWithStream()
{
$this->setPerforceToStream();
$expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port print '.ProcessExecutor::escape('//depot/branch/composer.json');
2013-08-09 19:24:58 +00:00
$this->processExecutor->expects($this->at(0))
->method('execute')
->with($this->equalTo($expectedCommand))
->will(
$this->returnCallback(
function ($command, &$output) {
$output = PerforceTest::getComposerJson();
return true;
}
)
);
2013-08-09 19:24:58 +00:00
$result = $this->perforce->getComposerInformation('//depot/branch');
2013-08-09 19:24:58 +00:00
$expected = array(
2017-03-08 14:07:29 +00:00
'name' => 'test/perforce',
'description' => 'Basic project for testing',
'minimum-stability' => 'dev',
2017-03-08 14:07:29 +00:00
'autoload' => array('psr-0' => array()),
2013-08-09 19:24:58 +00:00
);
$this->assertEquals($expected, $result);
}
public function testGetComposerInformationWithLabelWithStream()
{
$this->setPerforceToStream();
$expectedCommand = 'p4 -u user -p port files '.ProcessExecutor::escape('//depot/branch/composer.json@0.0.1');
2013-08-09 19:24:58 +00:00
$this->processExecutor->expects($this->at(0))
->method('execute')
->with($this->equalTo($expectedCommand))
->will(
$this->returnCallback(
function ($command, &$output) {
$output = '//depot/composer.json#1 - branch change 10001 (text)';
return true;
}
)
);
2013-08-09 19:24:58 +00:00
$expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port print '.ProcessExecutor::escape('//depot/branch/composer.json@10001');
2013-08-09 19:24:58 +00:00
$this->processExecutor->expects($this->at(1))
->method('execute')
->with($this->equalTo($expectedCommand))
->will(
$this->returnCallback(
function ($command, &$output) {
$output = PerforceTest::getComposerJson();
return true;
}
)
);
2013-08-09 19:24:58 +00:00
$result = $this->perforce->getComposerInformation('//depot/branch@0.0.1');
2013-08-09 19:24:58 +00:00
$expected = array(
2017-03-08 14:07:29 +00:00
'name' => 'test/perforce',
'description' => 'Basic project for testing',
'minimum-stability' => 'dev',
2017-03-08 14:07:29 +00:00
'autoload' => array('psr-0' => array()),
2013-08-09 19:24:58 +00:00
);
$this->assertEquals($expected, $result);
}
public function testSyncCodeBaseWithoutStream()
{
$expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port sync -f @label';
2013-10-11 23:22:50 +00:00
$this->processExecutor->expects($this->at(0))
->method('execute')
->with($this->equalTo($expectedCommand), $this->equalTo(null))
->will($this->returnValue(0));
2013-08-09 19:24:58 +00:00
$this->perforce->syncCodeBase('label');
2013-08-09 19:24:58 +00:00
}
public function testSyncCodeBaseWithStream()
{
$this->setPerforceToStream();
$expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port sync -f @label';
2013-10-11 23:22:50 +00:00
$this->processExecutor->expects($this->at(0))
->method('execute')
->with($this->equalTo($expectedCommand))
->will($this->returnValue(0));
2013-08-09 19:24:58 +00:00
$this->perforce->syncCodeBase('label');
2013-08-09 19:24:58 +00:00
}
public function testCheckServerExists()
{
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
2013-08-09 19:24:58 +00:00
$expectedCommand = 'p4 -p '.ProcessExecutor::escape('perforce.does.exist:port').' info -s';
2013-08-09 19:24:58 +00:00
$processExecutor->expects($this->at(0))
->method('execute')
->with($this->equalTo($expectedCommand), $this->equalTo(null))
->will($this->returnValue(0));
2013-08-09 19:24:58 +00:00
$result = $this->perforce->checkServerExists('perforce.does.exist:port', $processExecutor);
2013-08-09 19:24:58 +00:00
$this->assertTrue($result);
}
2014-06-10 14:02:44 +00:00
/**
* Test if "p4" command is missing.
2014-06-10 14:02:44 +00:00
*
* @covers \Composer\Util\Perforce::checkServerExists
2014-06-10 14:02:44 +00:00
*
* @return void
*/
public function testCheckServerClientError()
{
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
$expectedCommand = 'p4 -p '.ProcessExecutor::escape('perforce.does.exist:port').' info -s';
$processExecutor->expects($this->at(0))
->method('execute')
->with($this->equalTo($expectedCommand), $this->equalTo(null))
->will($this->returnValue(127));
2014-06-10 14:02:44 +00:00
$result = $this->perforce->checkServerExists('perforce.does.exist:port', $processExecutor);
$this->assertFalse($result);
}
2013-08-09 19:24:58 +00:00
2021-10-27 14:18:46 +00:00
/**
* @return string
*/
public static function getComposerJson()
{
2013-08-09 19:24:58 +00:00
$composer_json = array(
'{',
'"name": "test/perforce",',
'"description": "Basic project for testing",',
'"minimum-stability": "dev",',
'"autoload": {',
'"psr-0" : {',
'}',
'}',
2015-09-28 09:51:14 +00:00
'}',
2013-08-09 19:24:58 +00:00
);
return implode($composer_json);
}
2021-10-27 14:18:46 +00:00
/**
* @param bool $withStream
*
* @return string[]
*/
private function getExpectedClientSpec($withStream)
{
2013-08-09 19:24:58 +00:00
$expectedArray = array(
'Client: composer_perforce_TEST_depot',
PHP_EOL,
'Update:',
PHP_EOL,
'Access:',
'Owner: user',
PHP_EOL,
'Description:',
' Created by user from composer.',
PHP_EOL,
'Root: path',
PHP_EOL,
'Options: noallwrite noclobber nocompress unlocked modtime rmdir',
PHP_EOL,
'SubmitOptions: revertunchanged',
PHP_EOL,
'LineEnd: local',
2015-09-28 09:51:14 +00:00
PHP_EOL,
2013-08-09 19:24:58 +00:00
);
if ($withStream) {
$expectedArray[] = 'Stream:';
$expectedArray[] = ' //depot/branch';
} else {
$expectedArray[] = 'View: //depot/... //composer_perforce_TEST_depot/...';
2013-08-09 19:24:58 +00:00
}
return $expectedArray;
}
2021-10-27 14:18:46 +00:00
/**
* @return void
*/
private function setPerforceToStream()
{
$this->perforce->setStream('//depot/branch');
2013-08-09 19:24:58 +00:00
}
2014-03-18 19:39:47 +00:00
public function testCleanupClientSpecShouldDeleteClient()
{
$fs = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
2014-03-18 19:39:47 +00:00
$this->perforce->setFilesystem($fs);
$testClient = $this->perforce->getClient();
$expectedCommand = 'p4 -u ' . self::TEST_P4USER . ' -p ' . self::TEST_PORT . ' client -d ' . ProcessExecutor::escape($testClient);
2014-03-18 19:39:47 +00:00
$this->processExecutor->expects($this->once())->method('execute')->with($this->equalTo($expectedCommand));
$fs->expects($this->once())->method('remove')->with($this->perforce->getP4ClientSpec());
$this->perforce->cleanupClientSpec();
}
2013-08-09 19:24:58 +00:00
}