1
0
Fork 0

phpstan: update tests/Composer/Test/Downloader/* to level 6 standard (#10238)

pull/10245/head
megubyte 2021-10-30 09:21:50 +01:00 committed by GitHub
parent ec63178d38
commit 8b7e2b600d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 125 additions and 29 deletions

View File

@ -16,6 +16,7 @@ use Composer\Test\TestCase;
class ArchiveDownloaderTest extends TestCase class ArchiveDownloaderTest extends TestCase
{ {
/** @var \Composer\Config&\PHPUnit\Framework\MockObject\MockObject */
protected $config; protected $config;
public function testGetFileName() public function testGetFileName()

View File

@ -17,7 +17,10 @@ use Composer\Test\TestCase;
class DownloadManagerTest extends TestCase class DownloadManagerTest extends TestCase
{ {
/** @var \Composer\Util\Filesystem&\PHPUnit\Framework\MockObject\MockObject */
protected $filesystem; protected $filesystem;
/** @var \Composer\IO\IOInterface&\PHPUnit\Framework\MockObject\MockObject */
protected $io; protected $io;
public function setUp() public function setUp()
@ -1091,12 +1094,18 @@ class DownloadManagerTest extends TestCase
$manager->download($package, 'target_dir'); $manager->download($package, 'target_dir');
} }
/**
* @return \Composer\Downloader\DownloaderInterface&\PHPUnit\Framework\MockObject\MockObject
*/
private function createDownloaderMock() private function createDownloaderMock()
{ {
return $this->getMockBuilder('Composer\Downloader\DownloaderInterface') return $this->getMockBuilder('Composer\Downloader\DownloaderInterface')
->getMock(); ->getMock();
} }
/**
* @return \Composer\Package\PackageInterface&\PHPUnit\Framework\MockObject\MockObject
*/
private function createPackageMock() private function createPackageMock()
{ {
return $this->getMockBuilder('Composer\Package\PackageInterface') return $this->getMockBuilder('Composer\Package\PackageInterface')

View File

@ -25,13 +25,30 @@ use Composer\Util\Loop;
class FileDownloaderTest extends TestCase class FileDownloaderTest extends TestCase
{ {
/** @var \Composer\Util\HttpDownloader&\PHPUnit\Framework\MockObject\MockObject */
private $httpDownloader; private $httpDownloader;
/** @var \Composer\Config&\PHPUnit\Framework\MockObject\MockObject */
private $config; private $config;
public function setUp()
{
$this->httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock();
$this->config = $this->getMockBuilder('Composer\Config')->getMock();
}
/**
* @param \Composer\IO\IOInterface $io
* @param \Composer\Config&\PHPUnit\Framework\MockObject\MockObject $config
* @param \Composer\EventDispatcher\EventDispatcher $eventDispatcher
* @param \Composer\Cache $cache
* @param \Composer\Util\HttpDownloader&\PHPUnit\Framework\MockObject\MockObject $httpDownloader
* @param \Composer\Util\Filesystem $filesystem
* @return \Composer\Downloader\FileDownloader
*/
protected function getDownloader($io = null, $config = null, $eventDispatcher = null, $cache = null, $httpDownloader = null, $filesystem = null) protected function getDownloader($io = null, $config = null, $eventDispatcher = null, $cache = null, $httpDownloader = null, $filesystem = null)
{ {
$io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); $io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$this->config = $config ?: $this->getMockBuilder('Composer\Config')->getMock(); $config = $config ?: $this->config;
$httpDownloader = $httpDownloader ?: $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(); $httpDownloader = $httpDownloader ?: $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock();
$httpDownloader $httpDownloader
->expects($this->any()) ->expects($this->any())
@ -166,7 +183,6 @@ class FileDownloaderTest extends TestCase
$self = $this; $self = $this;
$path = $this->getUniqueTmpDirectory(); $path = $this->getUniqueTmpDirectory();
$config = new Config(false, $path);
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(); $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
$packageMock->expects($this->any()) $packageMock->expects($this->any())
@ -187,7 +203,16 @@ class FileDownloaderTest extends TestCase
->will($this->returnValue($rootPackageMock)); ->will($this->returnValue($rootPackageMock));
$composerMock->expects($this->any()) $composerMock->expects($this->any())
->method('getConfig') ->method('getConfig')
->will($this->returnValue($config)); ->will($this->returnValue($this->config));
$this->config->expects($this->any())
->method('get')
->will($this->returnCallback(function ($key) use ($path) {
if ($key === 'vendor-dir') {
return $path.'/vendor';
} elseif ($key === 'bin-dir') {
return $path.'/vendor/bin';
}
}));
$expectedUrl = 'foobar'; $expectedUrl = 'foobar';
$expectedCacheKey = '/'.sha1($expectedUrl).'.'; $expectedCacheKey = '/'.sha1($expectedUrl).'.';
@ -233,7 +258,7 @@ class FileDownloaderTest extends TestCase
); );
})); }));
$downloader = $this->getDownloader(null, $config, $dispatcher, $cacheMock, $httpDownloaderMock); $downloader = $this->getDownloader(null, $this->config, $dispatcher, $cacheMock, $httpDownloaderMock);
try { try {
$loop = new Loop($this->httpDownloader); $loop = new Loop($this->httpDownloader);
@ -259,7 +284,6 @@ class FileDownloaderTest extends TestCase
$self = $this; $self = $this;
$path = $this->getUniqueTmpDirectory(); $path = $this->getUniqueTmpDirectory();
$config = new Config(false, $path);
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(); $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
$packageMock->expects($this->any()) $packageMock->expects($this->any())
@ -280,7 +304,16 @@ class FileDownloaderTest extends TestCase
->will($this->returnValue($rootPackageMock)); ->will($this->returnValue($rootPackageMock));
$composerMock->expects($this->any()) $composerMock->expects($this->any())
->method('getConfig') ->method('getConfig')
->will($this->returnValue($config)); ->will($this->returnValue($this->config));
$this->config->expects($this->any())
->method('get')
->will($this->returnCallback(function ($key) use ($path) {
if ($key === 'vendor-dir') {
return $path.'/vendor';
} elseif ($key === 'bin-dir') {
return $path.'/vendor/bin';
}
}));
$expectedUrl = 'url'; $expectedUrl = 'url';
$customCacheKey = 'xyzzy'; $customCacheKey = 'xyzzy';
@ -327,7 +360,7 @@ class FileDownloaderTest extends TestCase
); );
})); }));
$downloader = $this->getDownloader(null, $config, $dispatcher, $cacheMock, $httpDownloaderMock); $downloader = $this->getDownloader(null, $this->config, $dispatcher, $cacheMock, $httpDownloaderMock);
try { try {
$loop = new Loop($this->httpDownloader); $loop = new Loop($this->httpDownloader);
@ -352,13 +385,13 @@ class FileDownloaderTest extends TestCase
{ {
$expectedTtl = '99999999'; $expectedTtl = '99999999';
$configMock = $this->getMockBuilder('Composer\Config')->getMock(); $this->config = $this->getMockBuilder('Composer\Config')->getMock();
$configMock $this->config
->expects($this->at(0)) ->expects($this->at(0))
->method('get') ->method('get')
->with('cache-files-ttl') ->with('cache-files-ttl')
->will($this->returnValue($expectedTtl)); ->will($this->returnValue($expectedTtl));
$configMock $this->config
->expects($this->at(1)) ->expects($this->at(1))
->method('get') ->method('get')
->with('cache-files-maxsize') ->with('cache-files-maxsize')
@ -376,7 +409,7 @@ class FileDownloaderTest extends TestCase
->method('gc') ->method('gc')
->with($expectedTtl, $this->anything()); ->with($expectedTtl, $this->anything());
$downloader = $this->getDownloader(null, $configMock, null, $cacheMock, null, null); $downloader = $this->getDownloader(null, $this->config, null, $cacheMock, null, null);
} }
public function testDownloadFileWithInvalidChecksum() public function testDownloadFileWithInvalidChecksum()

View File

@ -35,6 +35,12 @@ class FossilDownloaderTest extends TestCase
} }
} }
/**
* @param \Composer\IO\IOInterface $io
* @param \Composer\Config $config
* @param \Composer\Test\Mock\ProcessExecutorMock $executor
* @param \Composer\Util\Filesystem $filesystem
*/
protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null) protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null)
{ {
$io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); $io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock();

View File

@ -45,6 +45,10 @@ class GitDownloaderTest extends TestCase
$this->initGitVersion(false); $this->initGitVersion(false);
} }
/**
* @param string|bool $version
* @return void
*/
private function initGitVersion($version) private function initGitVersion($version)
{ {
// reset the static version cache // reset the static version cache
@ -53,6 +57,10 @@ class GitDownloaderTest extends TestCase
$refl->setValue(null, $version); $refl->setValue(null, $version);
} }
/**
* @param ?\Composer\Config $config
* @return \Composer\Config
*/
protected function setupConfig($config = null) protected function setupConfig($config = null)
{ {
if (!$config) { if (!$config) {
@ -66,6 +74,12 @@ class GitDownloaderTest extends TestCase
return $config; return $config;
} }
/**
* @param \Composer\IO\IOInterface $io
* @param \Composer\Config $config
* @param \Composer\Test\Mock\ProcessExecutorMock $executor
* @param \Composer\Util\Filesystem $filesystem
*/
protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null) protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null)
{ {
$io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); $io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
@ -641,6 +655,10 @@ composer https://github.com/old/url (push)
$this->assertEquals('source', $downloader->getInstallationSource()); $this->assertEquals('source', $downloader->getInstallationSource());
} }
/**
* @param string $cmd
* @return string
*/
private function winCompat($cmd) private function winCompat($cmd)
{ {
if (Platform::isWindows()) { if (Platform::isWindows()) {

View File

@ -35,6 +35,12 @@ class HgDownloaderTest extends TestCase
} }
} }
/**
* @param \Composer\IO\IOInterface $io
* @param \Composer\Config $config
* @param \Composer\Test\Mock\ProcessExecutorMock $executor
* @param \Composer\Util\Filesystem $filesystem
*/
protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null) protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null)
{ {
$io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); $io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock();

View File

@ -26,14 +26,21 @@ use Composer\Test\Mock\ProcessExecutorMock;
*/ */
class PerforceDownloaderTest extends TestCase class PerforceDownloaderTest extends TestCase
{ {
/** @var \Composer\Config */
protected $config; protected $config;
/** @var ?PerforceDownloader */ /** @var \Composer\Downloader\PerforceDownloader */
protected $downloader; protected $downloader;
/** @var \Composer\IO\IOInterface&\PHPUnit\Framework\MockObject\MockObject */
protected $io; protected $io;
/** @var \Composer\Package\PackageInterface&\PHPUnit\Framework\MockObject\MockObject */
protected $package; protected $package;
/** @var \Composer\Test\Mock\ProcessExecutorMock */
protected $processExecutor; protected $processExecutor;
/** @var string[] */
protected $repoConfig; protected $repoConfig;
/** @var \Composer\Repository\VcsRepository&\PHPUnit\Framework\MockObject\MockObject */
protected $repository; protected $repository;
/** @var string */
protected $testPath; protected $testPath;
protected function setUp() protected function setUp()
@ -48,20 +55,9 @@ class PerforceDownloaderTest extends TestCase
$this->downloader = new PerforceDownloader($this->io, $this->config, $this->processExecutor); $this->downloader = new PerforceDownloader($this->io, $this->config, $this->processExecutor);
} }
protected function tearDown() /**
{ * @return \Composer\Config
$this->downloader = null; */
$this->package = null;
$this->repository = null;
$this->io = null;
$this->config = null;
$this->repoConfig = null;
if (is_dir($this->testPath)) {
$fs = new Filesystem;
$fs->removeDirectory($this->testPath);
}
}
protected function getConfig() protected function getConfig()
{ {
$config = new Config(); $config = new Config();
@ -71,11 +67,17 @@ class PerforceDownloaderTest extends TestCase
return $config; return $config;
} }
/**
* @return \Composer\IO\IOInterface&\PHPUnit\Framework\MockObject\MockObject
*/
protected function getMockIoInterface() protected function getMockIoInterface()
{ {
return $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); return $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
} }
/**
* @return \Composer\Package\PackageInterface&\PHPUnit\Framework\MockObject\MockObject
*/
protected function getMockPackageInterface(VcsRepository $repository) protected function getMockPackageInterface(VcsRepository $repository)
{ {
$package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(); $package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
@ -84,11 +86,20 @@ class PerforceDownloaderTest extends TestCase
return $package; return $package;
} }
/**
* @return string[]
*/
protected function getRepoConfig() protected function getRepoConfig()
{ {
return array('url' => 'TEST_URL', 'p4user' => 'TEST_USER'); return array('url' => 'TEST_URL', 'p4user' => 'TEST_USER');
} }
/**
* @param string[] $repoConfig
* @param \Composer\IO\IOInterface $io
* @param \Composer\Config $config
* @return \Composer\Repository\VcsRepository&\PHPUnit\Framework\MockObject\MockObject
*/
protected function getMockRepository(array $repoConfig, IOInterface $io, Config $config) protected function getMockRepository(array $repoConfig, IOInterface $io, Config $config)
{ {
$repository = $this->getMockBuilder('Composer\Repository\VcsRepository') $repository = $this->getMockBuilder('Composer\Repository\VcsRepository')

View File

@ -21,13 +21,15 @@ use Composer\Util\Loop;
class ZipDownloaderTest extends TestCase class ZipDownloaderTest extends TestCase
{ {
/** /** @var string */
* @var string
*/
private $testDir; private $testDir;
/** @var \Composer\Util\HttpDownloader */
private $httpDownloader; private $httpDownloader;
/** @var \Composer\IO\IOInterface&\PHPUnit\Framework\MockObject\MockObject */
private $io; private $io;
/** @var \Composer\Config&\PHPUnit\Framework\MockObject\MockObject */
private $config; private $config;
/** @var \Composer\Package\PackageInterface&\PHPUnit\Framework\MockObject\MockObject */
private $package; private $package;
public function setUp() public function setUp()
@ -47,6 +49,12 @@ class ZipDownloaderTest extends TestCase
$this->setPrivateProperty('hasZipArchive', null); $this->setPrivateProperty('hasZipArchive', null);
} }
/**
* @param string $name
* @param mixed $value
* @param ?\Composer\Test\Downloader\MockedZipDownloader $obj
* @return void
*/
public function setPrivateProperty($name, $value, $obj = null) public function setPrivateProperty($name, $value, $obj = null)
{ {
$reflectionClass = new \ReflectionClass('Composer\Downloader\ZipDownloader'); $reflectionClass = new \ReflectionClass('Composer\Downloader\ZipDownloader');
@ -296,6 +304,10 @@ class ZipDownloaderTest extends TestCase
$this->wait($promise); $this->wait($promise);
} }
/**
* @param ?\React\Promise\PromiseInterface $promise
* @return void
*/
private function wait($promise) private function wait($promise)
{ {
if (null === $promise) { if (null === $promise) {