1
0
Fork 0
composer/tests/Composer/Test/Repository/Vcs/PerforceDriverTest.php

194 lines
6.3 KiB
PHP
Raw Normal View History

2022-02-23 15:58:18 +00:00
<?php declare(strict_types=1);
2013-10-11 23:12:02 +00:00
2013-07-24 15:06:35 +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.
*/
namespace Composer\Test\Repository\Vcs;
use Composer\Repository\Vcs\PerforceDriver;
use Composer\Test\TestCase;
2013-07-24 15:06:35 +00:00
use Composer\Util\Filesystem;
use Composer\Config;
use Composer\Util\Perforce;
use Composer\Test\Mock\ProcessExecutorMock;
2013-07-24 15:06:35 +00:00
2013-08-15 19:45:42 +00:00
/**
* @author Matt Whittom <Matt.Whittom@veteransunited.com>
*/
class PerforceDriverTest extends TestCase
{
2021-10-16 08:16:06 +00:00
/**
* @var Config
*/
protected $config;
2021-10-16 08:16:06 +00:00
/**
* @var \Composer\IO\IOInterface&\PHPUnit\Framework\MockObject\MockObject
*/
protected $io;
2021-10-16 08:16:06 +00:00
/**
* @var ProcessExecutorMock
*/
protected $process;
2021-10-16 08:16:06 +00:00
/**
* @var \Composer\Util\HttpDownloader&\PHPUnit\Framework\MockObject\MockObject
*/
protected $httpDownloader;
2021-10-16 08:16:06 +00:00
/**
* @var string
*/
protected $testPath;
2021-10-16 08:16:06 +00:00
/**
* @var PerforceDriver
*/
protected $driver;
2021-10-16 08:16:06 +00:00
/**
* @var array<string, string>
*/
protected $repoConfig;
2021-10-16 08:16:06 +00:00
/**
* @var Perforce&\PHPUnit\Framework\MockObject\MockObject
*/
protected $perforce;
private const TEST_URL = 'TEST_PERFORCE_URL';
private const TEST_DEPOT = 'TEST_DEPOT_CONFIG';
private const TEST_BRANCH = 'TEST_BRANCH_CONFIG';
2013-07-24 15:06:35 +00:00
2021-12-08 16:03:05 +00:00
protected function setUp(): void
{
$this->testPath = self::getUniqueTmpDirectory();
2017-03-08 14:07:29 +00:00
$this->config = $this->getTestConfig($this->testPath);
2022-08-17 12:20:07 +00:00
$this->repoConfig = [
2021-10-16 08:16:06 +00:00
'url' => self::TEST_URL,
'depot' => self::TEST_DEPOT,
'branch' => self::TEST_BRANCH,
2022-08-17 12:20:07 +00:00
];
2017-03-08 14:07:29 +00:00
$this->io = $this->getMockIOInterface();
$this->process = $this->getProcessExecutorMock();
$this->httpDownloader = $this->getMockHttpDownloader();
2017-03-08 14:07:29 +00:00
$this->perforce = $this->getMockPerforce();
$this->driver = new PerforceDriver($this->repoConfig, $this->io, $this->config, $this->httpDownloader, $this->process);
$this->overrideDriverInternalPerforce($this->perforce);
2013-07-24 15:06:35 +00:00
}
2021-12-08 16:03:05 +00:00
protected function tearDown(): void
{
parent::tearDown();
//cleanup directory under test path
2013-07-24 15:06:35 +00:00
$fs = new Filesystem;
2013-08-13 15:54:35 +00:00
$fs->removeDirectory($this->testPath);
2013-07-24 15:06:35 +00:00
}
protected function overrideDriverInternalPerforce(Perforce $perforce): void
{
$reflectionClass = new \ReflectionClass($this->driver);
$property = $reflectionClass->getProperty('perforce');
$property->setAccessible(true);
$property->setValue($this->driver, $perforce);
}
2022-02-22 15:47:09 +00:00
protected function getTestConfig(string $testPath): Config
{
$config = new Config();
2022-08-17 12:20:07 +00:00
$config->merge(['config' => ['home' => $testPath]]);
2014-06-10 14:02:44 +00:00
return $config;
}
/**
* @return \PHPUnit\Framework\MockObject\MockObject&\Composer\IO\IOInterface
*/
protected function getMockIOInterface()
{
return $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
}
/**
* @return \PHPUnit\Framework\MockObject\MockObject&\Composer\Util\HttpDownloader
*/
protected function getMockHttpDownloader()
{
return $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock();
}
/**
* @return \PHPUnit\Framework\MockObject\MockObject&\Composer\Util\Perforce
*/
protected function getMockPerforce()
{
2022-08-17 12:20:07 +00:00
$methods = ['p4login', 'checkStream', 'writeP4ClientSpec', 'connectClient', 'getComposerInformation', 'cleanupClientSpec'];
2014-06-10 14:02:44 +00:00
return $this->getMockBuilder('Composer\Util\Perforce')->disableOriginalConstructor()->getMock();
}
public function testInitializeCapturesVariablesFromRepoConfig(): void
{
$driver = new PerforceDriver($this->repoConfig, $this->io, $this->config, $this->httpDownloader, $this->process);
2013-08-13 15:54:35 +00:00
$driver->initialize();
self::assertEquals(self::TEST_URL, $driver->getUrl());
self::assertEquals(self::TEST_DEPOT, $driver->getDepot());
self::assertEquals(self::TEST_BRANCH, $driver->getBranch());
2013-07-24 15:06:35 +00:00
}
public function testInitializeLogsInAndConnectsClient(): void
{
$this->perforce->expects($this->once())->method('p4Login');
$this->perforce->expects($this->once())->method('checkStream');
$this->perforce->expects($this->once())->method('writeP4ClientSpec');
$this->perforce->expects($this->once())->method('connectClient');
$this->driver->initialize();
}
/**
* @depends testInitializeCapturesVariablesFromRepoConfig
* @depends testInitializeLogsInAndConnectsClient
*/
public function testHasComposerFileReturnsFalseOnNoComposerFile(): void
{
$identifier = 'TEST_IDENTIFIER';
$formatted_depot_path = '//' . self::TEST_DEPOT . '/' . $identifier;
2022-08-17 12:20:07 +00:00
$this->perforce->expects($this->any())->method('getComposerInformation')->with($this->equalTo($formatted_depot_path))->will($this->returnValue([]));
$this->driver->initialize();
$result = $this->driver->hasComposerFile($identifier);
self::assertFalse($result);
2013-07-24 15:06:35 +00:00
}
/**
* @depends testInitializeCapturesVariablesFromRepoConfig
* @depends testInitializeLogsInAndConnectsClient
*/
public function testHasComposerFileReturnsTrueWithOneOrMoreComposerFiles(): void
{
$identifier = 'TEST_IDENTIFIER';
$formatted_depot_path = '//' . self::TEST_DEPOT . '/' . $identifier;
2022-08-17 12:20:07 +00:00
$this->perforce->expects($this->any())->method('getComposerInformation')->with($this->equalTo($formatted_depot_path))->will($this->returnValue(['']));
$this->driver->initialize();
$result = $this->driver->hasComposerFile($identifier);
self::assertTrue($result);
2013-07-24 15:06:35 +00:00
}
2013-10-16 08:07:10 +00:00
/**
* Test that supports() simply return false.
*
2013-10-16 08:07:10 +00:00
* @covers \Composer\Repository\Vcs\PerforceDriver::supports
*/
public function testSupportsReturnsFalseNoDeepCheck(): void
2013-10-16 08:07:10 +00:00
{
$this->expectOutputString('');
self::assertFalse(PerforceDriver::supports($this->io, $this->config, 'existing.url'));
2013-10-16 08:07:10 +00:00
}
2014-03-18 19:39:47 +00:00
public function testCleanup(): void
2014-03-18 19:39:47 +00:00
{
$this->perforce->expects($this->once())->method('cleanupClientSpec');
$this->driver->cleanup();
}
2013-07-24 15:06:35 +00:00
}