1
0
Fork 0
mirror of https://github.com/composer/composer synced 2025-05-09 00:22:53 +00:00

Add parameter types to all the things

This commit is contained in:
Jordi Boggiano 2022-02-22 16:47:09 +01:00
parent c9baeda95b
commit 6da38f83a0
No known key found for this signature in database
GPG key ID: 7BBD42C429EC80BC
279 changed files with 1377 additions and 1504 deletions

View file

@ -20,33 +20,30 @@ use Composer\Test\TestCase;
use Composer\Util\Filesystem;
use Composer\Util\Http\Response;
use Composer\Util\Loop;
use Composer\Config;
use Composer\Composer;
class FileDownloaderTest extends TestCase
{
/** @var \Composer\Util\HttpDownloader&\PHPUnit\Framework\MockObject\MockObject */
private $httpDownloader;
/** @var \Composer\Config&\PHPUnit\Framework\MockObject\MockObject */
private $config;
public function setUp(): void
{
$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): \Composer\Downloader\FileDownloader
protected function getDownloader(\Composer\IO\IOInterface $io = null, ?Config $config = null, EventDispatcher $eventDispatcher = null, \Composer\Cache $cache = null, $httpDownloader = null, Filesystem $filesystem = null): FileDownloader
{
$io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$config = $config ?: $this->config;
$config = $config ?: $this->getConfig();
$httpDownloader = $httpDownloader ?: $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock();
$httpDownloader
->expects($this->any())
@ -54,40 +51,29 @@ class FileDownloaderTest extends TestCase
->will($this->returnValue(\React\Promise\resolve(new Response(array('url' => 'http://example.org/'), 200, array(), 'file~'))));
$this->httpDownloader = $httpDownloader;
return new FileDownloader($io, $this->config, $httpDownloader, $eventDispatcher, $cache, $filesystem);
return new FileDownloader($io, $config, $httpDownloader, $eventDispatcher, $cache, $filesystem);
}
public function testDownloadForPackageWithoutDistReference(): void
{
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
$packageMock->expects($this->once())
->method('getDistUrl')
->will($this->returnValue(null))
;
$package = $this->getPackage();
self::expectException('InvalidArgumentException');
$downloader = $this->getDownloader();
$downloader->download($packageMock, '/path');
$downloader->download($package, '/path');
}
public function testDownloadToExistingFile(): void
{
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
$packageMock->expects($this->any())
->method('getDistUrl')
->will($this->returnValue('url'))
;
$packageMock->expects($this->any())
->method('getDistUrls')
->will($this->returnValue(array('url')))
;
$package = $this->getPackage();
$package->setDistUrl('url');
$path = $this->createTempFile($this->getUniqueTmpDirectory());
$downloader = $this->getDownloader();
try {
$downloader->download($packageMock, $path);
$downloader->download($package, $path);
$this->fail();
} catch (\Exception $e) {
if (is_dir($path)) {
@ -103,39 +89,21 @@ class FileDownloaderTest extends TestCase
public function testGetFileName(): void
{
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
$packageMock->expects($this->once())
->method('getDistUrl')
->will($this->returnValue('http://example.com/script.js'))
;
$package = $this->getPackage();
$package->setDistUrl('http://example.com/script.js');
$downloader = $this->getDownloader();
$config = $this->getConfig(['vendor-dir' => '/vendor']);
$downloader = $this->getDownloader(null, $config);
$method = new \ReflectionMethod($downloader, 'getFileName');
$method->setAccessible(true);
$this->config->expects($this->once())
->method('get')
->with('vendor-dir')
->will($this->returnValue('/vendor'));
$this->assertMatchesRegularExpression('#/vendor/composer/tmp-[a-z0-9]+\.js#', $method->invoke($downloader, $packageMock, '/path'));
$this->assertMatchesRegularExpression('#/vendor/composer/tmp-[a-z0-9]+\.js#', $method->invoke($downloader, $package, '/path'));
}
public function testDownloadButFileIsUnsaved(): void
{
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
$packageMock->expects($this->any())
->method('getDistUrl')
->will($this->returnValue($distUrl = 'http://example.com/script.js'))
;
$packageMock->expects($this->once())
->method('getDistUrls')
->will($this->returnValue(array($distUrl)))
;
$packageMock->expects($this->atLeastOnce())
->method('getTransportOptions')
->will($this->returnValue(array()))
;
$package = $this->getPackage();
$package->setDistUrl('http://example.com/script.js');
$path = $this->getUniqueTmpDirectory();
$ioMock = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
@ -150,16 +118,12 @@ class FileDownloaderTest extends TestCase
}))
;
$downloader = $this->getDownloader($ioMock);
$this->config->expects($this->once())
->method('get')
->with('vendor-dir')
->will($this->returnValue($path.'/vendor'));
$config = $this->getConfig(['vendor-dir' => $path.'/vendor']);
$downloader = $this->getDownloader($ioMock, $config);
try {
$loop = new Loop($this->httpDownloader);
$promise = $downloader->download($packageMock, $path);
$promise = $downloader->download($package, $path);
$loop->wait(array($promise));
$this->fail('Download was expected to throw');
@ -180,41 +144,25 @@ class FileDownloaderTest extends TestCase
{
$path = $this->getUniqueTmpDirectory();
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
$packageMock->expects($this->any())
->method('getDistUrl')
->will($this->returnValue('url'));
$packageMock->expects($this->any())
->method('getDistUrls')
->will($this->returnValue(array('url')));
$package = $this->getPackage();
$package->setDistUrl('url');
$rootPackageMock = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock();
$rootPackageMock->expects($this->any())
->method('getScripts')
->will($this->returnValue(array()));
$rootPackage = $this->getRootPackage();
$composerMock = $this->getMockBuilder('Composer\Composer')->getMock();
$composerMock->expects($this->any())
->method('getPackage')
->will($this->returnValue($rootPackageMock));
$composerMock->expects($this->any())
->method('getConfig')
->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';
}
}));
$config = $this->getConfig([
'vendor-dir' => $path.'/vendor',
'bin-dir' => $path.'/vendor/bin',
]);
$composer = new Composer;
$composer->setPackage($rootPackage);
$composer->setConfig($config);
$expectedUrl = 'foobar';
$expectedCacheKey = '/'.sha1($expectedUrl).'.';
$expectedCacheKey = 'dummy/pkg/'.sha1($expectedUrl).'.';
$dispatcher = new EventDispatcher(
$composerMock,
$composer,
$this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
$this->getProcessExecutorMock()
);
@ -254,11 +202,11 @@ class FileDownloaderTest extends TestCase
);
}));
$downloader = $this->getDownloader(null, $this->config, $dispatcher, $cacheMock, $httpDownloaderMock);
$downloader = $this->getDownloader(null, $config, $dispatcher, $cacheMock, $httpDownloaderMock);
try {
$loop = new Loop($this->httpDownloader);
$promise = $downloader->download($packageMock, $path);
$promise = $downloader->download($package, $path);
$loop->wait(array($promise));
$this->fail('Download was expected to throw');
@ -279,42 +227,26 @@ class FileDownloaderTest extends TestCase
{
$path = $this->getUniqueTmpDirectory();
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
$packageMock->expects($this->any())
->method('getDistUrl')
->will($this->returnValue('url'));
$packageMock->expects($this->any())
->method('getDistUrls')
->will($this->returnValue(array('url')));
$package = $this->getPackage();
$package->setDistUrl('url');
$rootPackageMock = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock();
$rootPackageMock->expects($this->any())
->method('getScripts')
->will($this->returnValue(array()));
$rootPackage = $this->getRootPackage();
$composerMock = $this->getMockBuilder('Composer\Composer')->getMock();
$composerMock->expects($this->any())
->method('getPackage')
->will($this->returnValue($rootPackageMock));
$composerMock->expects($this->any())
->method('getConfig')
->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';
}
}));
$config = $this->getConfig([
'vendor-dir' => $path.'/vendor',
'bin-dir' => $path.'/vendor/bin',
]);
$composer = new Composer;
$composer->setPackage($rootPackage);
$composer->setConfig($config);
$expectedUrl = 'url';
$customCacheKey = 'xyzzy';
$expectedCacheKey = '/'.sha1($customCacheKey).'.';
$expectedCacheKey = 'dummy/pkg/'.sha1($customCacheKey).'.';
$dispatcher = new EventDispatcher(
$composerMock,
$composer,
$this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
$this->getProcessExecutorMock()
);
@ -354,11 +286,11 @@ class FileDownloaderTest extends TestCase
);
}));
$downloader = $this->getDownloader(null, $this->config, $dispatcher, $cacheMock, $httpDownloaderMock);
$downloader = $this->getDownloader(null, $config, $dispatcher, $cacheMock, $httpDownloaderMock);
try {
$loop = new Loop($this->httpDownloader);
$promise = $downloader->download($packageMock, $path);
$promise = $downloader->download($package, $path);
$loop->wait(array($promise));
$this->fail('Download was expected to throw');
@ -379,14 +311,10 @@ class FileDownloaderTest extends TestCase
{
$expectedTtl = '99999999';
$this->config = $this->getMockBuilder('Composer\Config')->getMock();
$this->config
->expects($this->atLeast(2))
->method('get')
->willReturnMap([
['cache-files-ttl', 0, $expectedTtl],
['cache-files-maxsize', 0, '500M'],
]);
$config = $this->getConfig([
'cache-files-ttl' => $expectedTtl,
'cache-files-maxsize' => '500M',
]);
$cacheMock = $this->getMockBuilder('Composer\Cache')
->disableOriginalConstructor()
@ -400,48 +328,32 @@ class FileDownloaderTest extends TestCase
->method('gc')
->with($expectedTtl, $this->anything());
$downloader = $this->getDownloader(null, $this->config, null, $cacheMock, null, null);
$downloader = $this->getDownloader(null, $config, null, $cacheMock, null, null);
}
public function testDownloadFileWithInvalidChecksum(): void
{
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
$packageMock->expects($this->any())
->method('getDistUrl')
->will($this->returnValue($distUrl = 'http://example.com/script.js'))
;
$packageMock->expects($this->atLeastOnce())
->method('getTransportOptions')
->will($this->returnValue(array()))
;
$packageMock->expects($this->any())
->method('getDistSha1Checksum')
->will($this->returnValue('invalid'))
;
$packageMock->expects($this->once())
->method('getDistUrls')
->will($this->returnValue(array($distUrl)))
;
$package = $this->getPackage();
$package->setDistUrl($distUrl = 'http://example.com/script.js');
$package->setDistSha1Checksum('invalid');
$filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
$path = $this->getUniqueTmpDirectory();
$downloader = $this->getDownloader(null, null, null, null, null, $filesystem);
$config = $this->getConfig(['vendor-dir' => $path.'/vendor']);
$downloader = $this->getDownloader(null, $config, null, null, null, $filesystem);
// make sure the file expected to be downloaded is on disk already
$this->config->expects($this->any())
->method('get')
->with('vendor-dir')
->will($this->returnValue($path.'/vendor'));
$method = new \ReflectionMethod($downloader, 'getFileName');
$method->setAccessible(true);
$dlFile = $method->invoke($downloader, $packageMock, $path);
$dlFile = $method->invoke($downloader, $package, $path);
mkdir(dirname($dlFile), 0777, true);
touch($dlFile);
try {
$loop = new Loop($this->httpDownloader);
$promise = $downloader->download($packageMock, $path);
$promise = $downloader->download($package, $path);
$loop->wait(array($promise));
$this->fail('Download was expected to throw');
@ -460,27 +372,9 @@ class FileDownloaderTest extends TestCase
public function testDowngradeShowsAppropriateMessage(): void
{
$oldPackage = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
$oldPackage->expects($this->once())
->method('getFullPrettyVersion')
->will($this->returnValue('1.2.0'));
$oldPackage->expects($this->once())
->method('getVersion')
->will($this->returnValue('1.2.0.0'));
$newPackage = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
$newPackage->expects($this->any())
->method('getFullPrettyVersion')
->will($this->returnValue('1.0.0'));
$newPackage->expects($this->once())
->method('getVersion')
->will($this->returnValue('1.0.0.0'));
$newPackage->expects($this->any())
->method('getDistUrl')
->will($this->returnValue($distUrl = 'http://example.com/script.js'));
$newPackage->expects($this->once())
->method('getDistUrls')
->will($this->returnValue(array($distUrl)));
$oldPackage = $this->getPackage('dummy/pkg', '1.2.0');
$newPackage = $this->getPackage('dummy/pkg', '1.0.0');
$newPackage->setDistUrl($distUrl = 'http://example.com/script.js');
$ioMock = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$ioMock->expects($this->atLeast(2))
@ -491,19 +385,16 @@ class FileDownloaderTest extends TestCase
);
$path = $this->getUniqueTmpDirectory();
$config = $this->getConfig(['vendor-dir' => $path.'/vendor']);
$filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
$filesystem->expects($this->once())
->method('removeDirectoryAsync')
->will($this->returnValue(\React\Promise\resolve(true)));
$downloader = $this->getDownloader($ioMock, null, null, null, null, $filesystem);
$downloader = $this->getDownloader($ioMock, $config, null, null, null, $filesystem);
// make sure the file expected to be downloaded is on disk already
$this->config->expects($this->any())
->method('get')
->with('vendor-dir')
->will($this->returnValue($path.'/vendor'));
$method = new \ReflectionMethod($downloader, 'getFileName');
$method->setAccessible(true);
$dlFile = $method->invoke($downloader, $newPackage, $path);