1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
<?php declare(strict_types=1);
/*
* 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\Downloader;
use Composer\Downloader\PerforceDownloader;
use Composer\Config;
use Composer\Repository\VcsRepository;
use Composer\IO\IOInterface;
use Composer\Test\TestCase;
use Composer\Factory;
use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
/**
* @author Matt Whittom <Matt.Whittom@veteransunited.com>
*/
class PerforceDownloaderTest extends TestCase
{
/** @var \Composer\Config */
protected $config;
/** @var \Composer\Downloader\PerforceDownloader */
protected $downloader;
/** @var \Composer\IO\IOInterface&\PHPUnit\Framework\MockObject\MockObject */
protected $io;
/** @var \Composer\Package\PackageInterface&\PHPUnit\Framework\MockObject\MockObject */
protected $package;
/** @var \Composer\Test\Mock\ProcessExecutorMock */
protected $processExecutor;
/** @var string[] */
protected $repoConfig;
/** @var \Composer\Repository\VcsRepository&\PHPUnit\Framework\MockObject\MockObject */
protected $repository;
/** @var string */
protected $testPath;
protected function setUp(): void
{
$this->testPath = self::getUniqueTmpDirectory();
$this->repoConfig = $this->getRepoConfig();
$this->config = $this->getConfig();
$this->io = $this->getMockIoInterface();
$this->processExecutor = $this->getProcessExecutorMock();
$this->repository = $this->getMockRepository($this->repoConfig, $this->io, $this->config);
$this->package = $this->getMockPackageInterface($this->repository);
$this->downloader = new PerforceDownloader($this->io, $this->config, $this->processExecutor);
}
protected function getConfig(array $configOptions = [], bool $useEnvironment = false): Config
{
return parent::getConfig(array_merge(['home' => $this->testPath], $configOptions), $useEnvironment);
}
/**
* @return \Composer\IO\IOInterface&\PHPUnit\Framework\MockObject\MockObject
*/
protected function getMockIoInterface()
{
return $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
}
/**
* @return \Composer\Package\PackageInterface&\PHPUnit\Framework\MockObject\MockObject
*/
protected function getMockPackageInterface(VcsRepository $repository)
{
$package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
$package->expects($this->any())->method('getRepository')->willReturn($repository);
return $package;
}
/**
* @return string[]
*/
protected function getRepoConfig(): array
{
return ['url' => 'TEST_URL', 'p4user' => 'TEST_USER'];
}
/**
* @param string[] $repoConfig
* @return \Composer\Repository\VcsRepository&\PHPUnit\Framework\MockObject\MockObject
*/
protected function getMockRepository(array $repoConfig, IOInterface $io, Config $config)
{
$repository = $this->getMockBuilder('Composer\Repository\VcsRepository')
->onlyMethods(['getRepoConfig'])
->setConstructorArgs([$repoConfig, $io, $config, Factory::createHttpDownloader($io, $config)])
->getMock();
$repository->expects($this->any())->method('getRepoConfig')->willReturn($repoConfig);
return $repository;
}
#[DoesNotPerformAssertions]
public function testInitPerforceInstantiatesANewPerforceObject(): void
{
$this->downloader->initPerforce($this->package, $this->testPath, 'SOURCE_REF');
}
public function testInitPerforceDoesNothingIfPerforceAlreadySet(): void
{
$perforce = $this->getMockBuilder('Composer\Util\Perforce')->disableOriginalConstructor()->getMock();
$this->downloader->setPerforce($perforce);
$this->repository->expects($this->never())->method('getRepoConfig');
$this->downloader->initPerforce($this->package, $this->testPath, 'SOURCE_REF');
}
#[Depends('testInitPerforceInstantiatesANewPerforceObject')]
#[Depends('testInitPerforceDoesNothingIfPerforceAlreadySet')]
public function testDoInstallWithTag(): void
{
//I really don't like this test but the logic of each Perforce method is tested in the Perforce class. Really I am just enforcing workflow.
$ref = 'SOURCE_REF@123';
$label = 123;
$this->package->expects($this->once())->method('getSourceReference')->willReturn($ref);
$this->io->expects($this->once())->method('writeError')->with($this->stringContains('Cloning '.$ref));
$perforceMethods = ['setStream', 'p4Login', 'writeP4ClientSpec', 'connectClient', 'syncCodeBase', 'cleanupClientSpec'];
$perforce = $this->getMockBuilder('Composer\Util\Perforce')->disableOriginalConstructor()->getMock();
$perforce->expects($this->once())->method('initializePath')->with($this->equalTo($this->testPath));
$perforce->expects($this->once())->method('setStream')->with($this->equalTo($ref));
$perforce->expects($this->once())->method('p4Login');
$perforce->expects($this->once())->method('writeP4ClientSpec');
$perforce->expects($this->once())->method('connectClient');
$perforce->expects($this->once())->method('syncCodeBase')->with($label);
$perforce->expects($this->once())->method('cleanupClientSpec');
$this->downloader->setPerforce($perforce);
$this->downloader->doInstall($this->package, $this->testPath, 'url');
}
#[Depends('testInitPerforceInstantiatesANewPerforceObject')]
#[Depends('testInitPerforceDoesNothingIfPerforceAlreadySet')]
public function testDoInstallWithNoTag(): void
{
$ref = 'SOURCE_REF';
$label = null;
$this->package->expects($this->once())->method('getSourceReference')->willReturn($ref);
$this->io->expects($this->once())->method('writeError')->with($this->stringContains('Cloning '.$ref));
$perforceMethods = ['setStream', 'p4Login', 'writeP4ClientSpec', 'connectClient', 'syncCodeBase', 'cleanupClientSpec'];
$perforce = $this->getMockBuilder('Composer\Util\Perforce')->disableOriginalConstructor()->getMock();
$perforce->expects($this->once())->method('initializePath')->with($this->equalTo($this->testPath));
$perforce->expects($this->once())->method('setStream')->with($this->equalTo($ref));
$perforce->expects($this->once())->method('p4Login');
$perforce->expects($this->once())->method('writeP4ClientSpec');
$perforce->expects($this->once())->method('connectClient');
$perforce->expects($this->once())->method('syncCodeBase')->with($label);
$perforce->expects($this->once())->method('cleanupClientSpec');
$this->downloader->setPerforce($perforce);
$this->downloader->doInstall($this->package, $this->testPath, 'url');
}
}
|