@ -13,6 +13,7 @@
namespace Composer\Test\Util;
use Composer\Json\JsonFile;
use Composer\Test\Mock\ProcessExecutorMock;
use Composer\Util\Perforce;
use Composer\Test\TestCase;
use Composer\Util\ProcessExecutor;
@ -24,7 +25,7 @@ class PerforceTest extends TestCase
{
/** @var Perforce */
protected $perforce;
/** @var \PHPUnit\Framework\MockObject\MockObject& \Composer\Util\ ProcessExecutor */
/** @var ProcessExecutorMock */
protected $processExecutor;
/** @var array< string , string > */
protected $repoConfig;
@ -40,7 +41,7 @@ class PerforceTest extends TestCase
protected function setUp(): void
{
$this->processExecutor = $this->getMockBuilder('Composer\Util\ ProcessExecutor')->get Mock();
$this->processExecutor = $this->getProcessExecutorMock();
$this->repoConfig = $this->getTestRepoConfig();
$this->io = $this->getMockIOInterface();
$this->createNewPerforceWithWindowsFlag(true);
@ -80,8 +81,6 @@ class PerforceTest extends TestCase
public function testGetClientWithoutStream()
{
$client = $this->perforce->getClient();
$hostname = gethostname();
$timestamp = time();
$expected = 'composer_perforce_TEST_depot';
$this->assertEquals($expected, $client);
@ -148,15 +147,11 @@ class PerforceTest extends TestCase
{
$this->createNewPerforceWithWindowsFlag(true);
$this->perforce->setUser(null);
$expectedCommand = 'p4 set';
$callback = function ($command, & $output) {
$output = 'P4USER=TEST_P4VARIABLE_USER' . PHP_EOL;
return true;
};
$this->processExecutor->method('execute')
->with($this->equalTo($expectedCommand))
->willReturnCallback($callback);
$this->processExecutor->expects(
[['cmd' => 'p4 set', 'stdout' => 'P4USER=TEST_P4VARIABLE_USER' . PHP_EOL, 'return' => 0]],
true
);
$this->perforce->queryP4user();
$this->assertEquals('TEST_P4VARIABLE_USER', $this->perforce->getUser());
}
@ -165,15 +160,12 @@ class PerforceTest extends TestCase
{
$this->createNewPerforceWithWindowsFlag(false);
$this->perforce->setUser(null);
$expectedCommand = 'echo $P4USER';
$callback = function ($command, & $output) {
$output = 'TEST_P4VARIABLE_USER' . PHP_EOL;
return true;
};
$this->processExecutor->method('execute')
->with($this->equalTo($expectedCommand))
->willReturnCallback($callback);
$this->processExecutor->expects(
[['cmd' => 'echo $P4USER', 'stdout' => 'TEST_P4VARIABLE_USER' . PHP_EOL, 'return' => 0]],
true
);
$this->perforce->queryP4user();
$this->assertEquals('TEST_P4VARIABLE_USER', $this->perforce->getUser());
}
@ -199,12 +191,15 @@ class PerforceTest extends TestCase
->method('ask')
->with($this->equalTo($expectedQuestion))
->willReturn('TEST_QUERY_USER');
$this->processExecutor->expects($this->exactly(2))
->method('execute')
->willReturnMap([
['p4 set', 0],
[$expectedCommand, 0],
]);
$this->processExecutor->expects(
[
'p4 set',
$expectedCommand
],
true
);
$this->perforce->queryP4user();
}
@ -218,12 +213,13 @@ class PerforceTest extends TestCase
->method('ask')
->with($this->equalTo($expectedQuestion))
->willReturn('TEST_QUERY_USER');
$this->processExecutor->expects($this->exactly(2))
->method('execute')
->willReturnMap([
['echo $P4USER', 0],
$expectedCommand => 0,
]);
$this->processExecutor->expects(
[
'echo $P4USER',
$expectedCommand
],
true
);
$this->perforce->queryP4user();
}
@ -243,15 +239,12 @@ class PerforceTest extends TestCase
public function testQueryP4PasswordWithPasswordSetInP4VariablesWithWindowsOS()
{
$this->createNewPerforceWithWindowsFlag(true);
$expectedCommand = 'p4 set';
$callback = function ($command, & $output) {
$output = 'P4PASSWD=TEST_P4VARIABLE_PASSWORD' . PHP_EOL;
return true;
};
$this->processExecutor->method('execute')
->with($this->equalTo($expectedCommand))
->willReturnCallback($callback);
$this->processExecutor->expects(
[['cmd' => 'p4 set', 'stdout' => 'P4PASSWD=TEST_P4VARIABLE_PASSWORD' . PHP_EOL, 'return' => 0]],
true
);
$password = $this->perforce->queryP4Password();
$this->assertEquals('TEST_P4VARIABLE_PASSWORD', $password);
}
@ -259,15 +252,11 @@ class PerforceTest extends TestCase
public function testQueryP4PasswordWithPasswordSetInP4VariablesNotWindowsOS()
{
$this->createNewPerforceWithWindowsFlag(false);
$expectedCommand = 'echo $P4PASSWD';
$callback = function ($command, & $output) {
$output = 'TEST_P4VARIABLE_PASSWORD' . PHP_EOL;
return true;
};
$this->processExecutor->method('execute')
->with($this->equalTo($expectedCommand))
->willReturnCallback($callback);
$this->processExecutor->expects(
[['cmd' => 'echo $P4PASSWD', 'stdout' => 'TEST_P4VARIABLE_PASSWORD' . PHP_EOL, 'return' => 0]],
true
);
$password = $this->perforce->queryP4Password();
$this->assertEquals('TEST_P4VARIABLE_PASSWORD', $password);
@ -328,22 +317,19 @@ class PerforceTest extends TestCase
public function testIsLoggedIn()
{
$expectedCommand = 'p4 -u user -p port login -s';
$this->processExecutor->expects($this->once())
->method('execute')
->with($this->equalTo($expectedCommand), $this->equalTo(null))
->willReturn(0);
$this->processExecutor->expects(
[['cmd' => 'p4 -u user -p port login -s']],
true
);
$this->perforce->isLoggedIn();
}
public function testConnectClient()
{
$expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port client -i < path / composer_perforce_TEST_depot . p4 . spec ' ;
$this->processExecutor->expects($this->once())
->method('execute')
->with($this->equalTo($expectedCommand), $this->equalTo(null))
->willReturn(0);
$this->processExecutor->expects(
['p4 -u user -c composer_perforce_TEST_depot -p port client -i < path / composer_perforce_TEST_depot . p4 . spec ' ] ,
true
);
$this->perforce->connectClient();
}
@ -352,26 +338,19 @@ class PerforceTest extends TestCase
{
$this->setPerforceToStream();
$expectations = [
['p4 -u user -c composer_perforce_TEST_depot_branch -p port streams '.ProcessExecutor::escape('//depot/...'), 'Stream //depot/branch mainline none \'branch\'' . PHP_EOL],
['p4 -u user -p port changes '.ProcessExecutor::escape('//depot/branch/...'), 'Change 1234 on 2014/03/19 by Clark.Stuth@Clark.Stuth_test_client \'test changelist\''],
];
$count = 0;
$this->processExecutor->expects($this->exactly(2))
->method('execute')
->will(
$this->returnCallback(
function ($command, & $output) use ($expectations, & $count) {
$index = $count++;
if ($command !== $expectations[$index][0]) {
throw new \InvalidArgumentException('Expected '.$expectations[$index][0].' and got '.$command);
}
$output = $expectations[$index][1];
return 0;
}
)
);
$this->processExecutor->expects(
[
[
'cmd' => 'p4 -u user -c composer_perforce_TEST_depot_branch -p port streams '.ProcessExecutor::escape('//depot/...'),
'stdout' => 'Stream //depot/branch mainline none \'branch\'' . PHP_EOL
],
[
'cmd' => 'p4 -u user -p port changes '.ProcessExecutor::escape('//depot/branch/...'),
'stdout' => 'Change 1234 on 2014/03/19 by Clark.Stuth@Clark.Stuth_test_client \'test changelist\''
],
],
true
);
$branches = $this->perforce->getBranches();
$this->assertEquals('//depot/branch@1234', $branches['master']);
@ -379,35 +358,31 @@ class PerforceTest extends TestCase
public function testGetBranchesWithoutStream()
{
$expectedCommand = 'p4 -u user -p port changes '.ProcessExecutor::escape('//depot/...');
$expectedCallback = function ($command, & $output) {
$output = 'Change 5678 on 2014/03/19 by Clark.Stuth@Clark.Stuth_test_client \'test changelist\'';
$this->processExecutor->expects(
[
[
'cmd' => 'p4 -u user -p port changes '.ProcessExecutor::escape('//depot/...'),
'stdout' => 'Change 5678 on 2014/03/19 by Clark.Stuth@Clark.Stuth_test_client \'test changelist\''
],
],
true
);
return true;
};
$this->processExecutor->expects($this->once())
->method('execute')
->with($this->equalTo($expectedCommand))
->willReturnCallback($expectedCallback);
$branches = $this->perforce->getBranches();
$this->assertEquals('//depot@5678', $branches['master']);
}
public function testGetTagsWithoutStream()
{
$expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port labels';
$this->processExecutor->expects($this->once())
->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;
}
)
);
$this->processExecutor->expects(
[
[
'cmd' => 'p4 -u user -c composer_perforce_TEST_depot -p port labels',
'stdout' => 'Label 0.0.1 2013/07/31 \'First Label!\'' . PHP_EOL . 'Label 0.0.2 2013/08/01 \'Second Label!\'' . PHP_EOL
],
],
true
);
$tags = $this->perforce->getTags();
$this->assertEquals('//depot@0.0.1', $tags['0.0.1']);
@ -418,19 +393,15 @@ class PerforceTest extends TestCase
{
$this->setPerforceToStream();
$expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port labels';
$this->processExecutor->expects($this->once())
->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;
}
)
);
$this->processExecutor->expects(
[
[
'cmd' => 'p4 -u user -c composer_perforce_TEST_depot_branch -p port labels',
'stdout' => 'Label 0.0.1 2013/07/31 \'First Label!\'' . PHP_EOL . 'Label 0.0.2 2013/08/01 \'Second Label!\'' . PHP_EOL
],
],
true
);
$tags = $this->perforce->getTags();
$this->assertEquals('//depot/branch@0.0.1', $tags['0.0.1']);
@ -446,16 +417,16 @@ class PerforceTest extends TestCase
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\'';
$this->processExecutor->expects(
[
[
'cmd' => 'p4 -u user -p port depots',
'stdout' => 'Depot depot 2013/06/25 stream /p4/1/depots/depot/... \'Created by Me\''
],
],
true
);
return true;
}
)
);
$result = $this->perforce->checkStream();
$this->assertTrue($result);
$this->assertTrue($this->perforce->isStream());
@ -463,19 +434,15 @@ class PerforceTest extends TestCase
public function testGetComposerInformationWithoutLabelWithoutStream()
{
$expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port print '.ProcessExecutor::escape('//depot/composer.json');
$this->processExecutor->expects($this->once())
->method('execute')
->with($this->equalTo($expectedCommand))
->will(
$this->returnCallback(
function ($command, & $output) {
$output = PerforceTest::getComposerJson();
return true;
}
)
);
$this->processExecutor->expects(
[
[
'cmd' => 'p4 -u user -c composer_perforce_TEST_depot -p port print '.ProcessExecutor::escape('//depot/composer.json'),
'stdout' => PerforceTest::getComposerJson()
],
],
true
);
$result = $this->perforce->getComposerInformation('//depot');
$expected = array(
@ -489,26 +456,19 @@ class PerforceTest extends TestCase
public function testGetComposerInformationWithLabelWithoutStream()
{
$expectations = [
['p4 -u user -p port files '.ProcessExecutor::escape('//depot/composer.json@0.0.1'), '//depot/composer.json#1 - branch change 10001 (text)'],
['p4 -u user -c composer_perforce_TEST_depot -p port print '.ProcessExecutor::escape('//depot/composer.json@10001'), PerforceTest::getComposerJson()],
];
$count = 0;
$this->processExecutor->expects($this->exactly(2))
->method('execute')
->will(
$this->returnCallback(
function ($command, & $output) use ($expectations, & $count) {
$index = $count++;
if ($command !== $expectations[$index][0]) {
throw new \InvalidArgumentException('Expected '.$expectations[$index][0].' and got '.$command);
}
$output = $expectations[$index][1];
return 0;
}
)
);
$this->processExecutor->expects(
[
[
'cmd' => 'p4 -u user -p port files '.ProcessExecutor::escape('//depot/composer.json@0.0.1'),
'stdout' => '//depot/composer.json#1 - branch change 10001 (text)'
],
[
'cmd' => 'p4 -u user -c composer_perforce_TEST_depot -p port print '.ProcessExecutor::escape('//depot/composer.json@10001'),
'stdout' => PerforceTest::getComposerJson()
],
],
true
);
$result = $this->perforce->getComposerInformation('//depot@0.0.1');
@ -525,19 +485,15 @@ class PerforceTest extends TestCase
{
$this->setPerforceToStream();
$expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port print '.ProcessExecutor::escape('//depot/branch/composer.json');
$this->processExecutor->expects($this->once())
->method('execute')
->with($this->equalTo($expectedCommand))
->will(
$this->returnCallback(
function ($command, & $output) {
$output = PerforceTest::getComposerJson();
return true;
}
)
);
$this->processExecutor->expects(
[
[
'cmd' => 'p4 -u user -c composer_perforce_TEST_depot_branch -p port print '.ProcessExecutor::escape('//depot/branch/composer.json'),
'stdout' => PerforceTest::getComposerJson()
],
],
true
);
$result = $this->perforce->getComposerInformation('//depot/branch');
@ -552,26 +508,19 @@ class PerforceTest extends TestCase
public function testGetComposerInformationWithLabelWithStream()
{
$expectations = [
[ 'p4 -u user -p port files '.ProcessExecutor::escape('//depot/branch/composer.json@0.0.1'), '//depot/composer.json#1 - branch change 10001 (text)'],
['p4 -u user -c composer_perforce_TEST_depot_branch -p port print '.ProcessExecutor::escape('//depot/branch/composer.json@10001'), PerforceTest::getComposerJson()],
];
$count = 0;
$this->processExecutor->expects($this->exactly(2))
->method('execute')
->will(
$this->returnCallback(
function ($command, & $output) use ($expectations, & $count) {
$index = $count++;
if ($command !== $expectations[$index][0]) {
throw new \InvalidArgumentException('Expected '.$expectations[$index][0].' and got '.$command);
}
$output = $expectations[$index][1];
return 0;
}
)
);
$this->processExecutor->expects(
[
[
'cmd' => 'p4 -u user -p port files '.ProcessExecutor::escape('//depot/branch/composer.json@0.0.1'),
'stdout' => '//depot/composer.json#1 - branch change 10001 (text)',
],
[
'cmd' => 'p4 -u user -c composer_perforce_TEST_depot_branch -p port print '.ProcessExecutor::escape('//depot/branch/composer.json@10001'),
'stdout' => PerforceTest::getComposerJson(),
],
],
true
);
$this->setPerforceToStream();
@ -588,11 +537,10 @@ class PerforceTest extends TestCase
public function testSyncCodeBaseWithoutStream()
{
$expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot -p port sync -f @label';
$this->processExecutor->expects($this->once())
->method('execute')
->with($this->equalTo($expectedCommand), $this->equalTo(null))
->willReturn(0);
$this->processExecutor->expects(
['p4 -u user -c composer_perforce_TEST_depot -p port sync -f @label'],
true
);
$this->perforce->syncCodeBase('label');
}
@ -600,26 +548,23 @@ class PerforceTest extends TestCase
public function testSyncCodeBaseWithStream()
{
$this->setPerforceToStream();
$expectedCommand = 'p4 -u user -c composer_perforce_TEST_depot_branch -p port sync -f @label';
$this->processExecutor->expects($this->once())
->method('execute')
->with($this->equalTo($expectedCommand))
->willReturn(0 );
$this->processExecutor->expects(
['p4 -u user -c composer_perforce_TEST_depot_branch -p port sync -f @label'],
true
);
$this->perforce->syncCodeBase('label');
}
public function testCheckServerExists()
{
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
$expectedCommand = 'p4 -p '.ProcessExecutor::escape('perforce.does.exist:port').' info -s';
$processExecutor->expects($this->once())
->method('execute')
->with($this->equalTo($expectedCommand), $this->equalTo(null))
->willReturn(0);
$this->processExecutor->expects(
['p4 -p '.ProcessExecutor::escape('perforce.does.exist:port').' info -s'],
true
);
$result = $this->perforce->checkServerExists('perforce.does.exist:port', $processExecutor);
$result = $this->perforce->checkServerExists('perforce.does.exist:port', $this->processExecutor);
$this->assertTrue($result);
}
@ -710,8 +655,10 @@ class PerforceTest extends TestCase
$this->perforce->setFilesystem($fs);
$testClient = $this->perforce->getClient();
$expectedCommand = 'p4 -u ' . self::TEST_P4USER . ' -p ' . self::TEST_PORT . ' client -d ' . ProcessExecutor::escape($testClient);
$this->processExecutor->expects($this->once())->method('execute')->with($this->equalTo($expectedCommand));
$this->processExecutor->expects(
['p4 -u ' . self::TEST_P4USER . ' -p ' . self::TEST_PORT . ' client -d ' . ProcessExecutor::escape($testClient)],
true
);
$fs->expects($this->once())->method('remove')->with($this->perforce->getP4ClientSpec());