1
0
Fork 0
composer/tests/Composer/Test/Downloader/FileDownloaderTest.php

522 lines
20 KiB
PHP
Raw Normal View History

<?php
/*
* 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\Config;
use Composer\Downloader\FileDownloader;
use Composer\EventDispatcher\EventDispatcher;
use Composer\Plugin\PluginEvents;
use Composer\Plugin\PreFileDownloadEvent;
use Composer\Test\TestCase;
use Composer\Test\Mock\ProcessExecutorMock;
use Composer\Util\Filesystem;
use Composer\Util\Http\Response;
use Composer\Util\Loop;
class FileDownloaderTest extends TestCase
{
/** @var \Composer\Util\HttpDownloader&\PHPUnit\Framework\MockObject\MockObject */
2019-01-07 15:22:41 +00:00
private $httpDownloader;
/** @var \Composer\Config&\PHPUnit\Framework\MockObject\MockObject */
private $config;
2019-01-07 15:22:41 +00:00
2021-12-08 16:03:05 +00:00
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)
2012-03-15 00:28:10 +00:00
{
$io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$config = $config ?: $this->config;
$httpDownloader = $httpDownloader ?: $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock();
$httpDownloader
->expects($this->any())
->method('addCopy')
->will($this->returnValue(\React\Promise\resolve(new Response(array('url' => 'http://example.org/'), 200, array(), 'file~'))));
$this->httpDownloader = $httpDownloader;
2012-06-14 10:10:01 +00:00
return new FileDownloader($io, $this->config, $httpDownloader, $eventDispatcher, $cache, $filesystem);
2012-03-15 00:28:10 +00:00
}
public function testDownloadForPackageWithoutDistReference()
{
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
$packageMock->expects($this->once())
->method('getDistUrl')
->will($this->returnValue(null))
;
2021-12-09 19:55:26 +00:00
self::expectException('InvalidArgumentException');
2012-03-15 00:28:10 +00:00
$downloader = $this->getDownloader();
$downloader->download($packageMock, '/path');
}
2012-03-25 23:56:24 +00:00
public function testDownloadToExistingFile()
{
$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')))
;
2021-12-10 12:14:04 +00:00
$path = $this->createTempFile($this->getUniqueTmpDirectory());
2012-03-15 00:28:10 +00:00
$downloader = $this->getDownloader();
try {
$downloader->download($packageMock, $path);
$this->fail();
} catch (\Exception $e) {
2012-11-10 22:17:36 +00:00
if (is_dir($path)) {
$fs = new Filesystem();
$fs->removeDirectory($path);
} elseif (is_file($path)) {
unlink($path);
}
2012-03-25 23:56:24 +00:00
$this->assertInstanceOf('RuntimeException', $e);
$this->assertStringContainsString('exists and is not a directory', $e->getMessage());
}
}
public function testGetFileName()
{
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
$packageMock->expects($this->once())
->method('getDistUrl')
->will($this->returnValue('http://example.com/script.js'))
;
2012-03-15 00:28:10 +00:00
$downloader = $this->getDownloader();
$method = new \ReflectionMethod($downloader, 'getFileName');
$method->setAccessible(true);
$this->config->expects($this->once())
->method('get')
->with('vendor-dir')
->will($this->returnValue('/vendor'));
2020-09-11 09:27:26 +00:00
$this->assertMatchesRegularExpression('#/vendor/composer/tmp-[a-z0-9]+\.js#', $method->invoke($downloader, $packageMock, '/path'));
}
public function testDownloadButFileIsUnsaved()
{
$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()))
;
$path = $this->getUniqueTmpDirectory();
$ioMock = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$ioMock->expects($this->any())
->method('write')
2014-06-10 14:02:44 +00:00
->will($this->returnCallback(function ($messages, $newline = true) use ($path) {
if (is_file($path.'/script.js')) {
unlink($path.'/script.js');
}
2012-06-14 10:10:01 +00:00
return $messages;
}))
;
2012-03-15 00:28:10 +00:00
$downloader = $this->getDownloader($ioMock);
$this->config->expects($this->once())
->method('get')
->with('vendor-dir')
->will($this->returnValue($path.'/vendor'));
try {
$loop = new Loop($this->httpDownloader);
$promise = $downloader->download($packageMock, $path);
$loop->wait(array($promise));
$this->fail('Download was expected to throw');
} catch (\Exception $e) {
if (is_dir($path)) {
$fs = new Filesystem();
$fs->removeDirectory($path);
} elseif (is_file($path)) {
unlink($path);
}
$this->assertInstanceOf('UnexpectedValueException', $e, $e->getMessage());
$this->assertStringContainsString('could not be saved to', $e->getMessage());
}
}
public function testDownloadWithCustomProcessedUrl()
{
$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')));
$rootPackageMock = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock();
$rootPackageMock->expects($this->any())
->method('getScripts')
->will($this->returnValue(array()));
$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';
}
}));
2020-11-22 13:48:56 +00:00
$expectedUrl = 'foobar';
$expectedCacheKey = '/'.sha1($expectedUrl).'.';
$dispatcher = new EventDispatcher(
$composerMock,
$this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
$this->getProcessExecutorMock()
);
2020-11-22 13:48:56 +00:00
$dispatcher->addListener(PluginEvents::PRE_FILE_DOWNLOAD, function (PreFileDownloadEvent $event) use ($expectedUrl) {
$event->setProcessedUrl($expectedUrl);
});
$cacheMock = $this->getMockBuilder('Composer\Cache')
->disableOriginalConstructor()
->getMock();
$cacheMock
->expects($this->any())
->method('copyTo')
2022-01-03 14:40:32 +00:00
->will($this->returnCallback(function ($cacheKey) use ($expectedCacheKey) {
$this->assertEquals($expectedCacheKey, $cacheKey, 'Failed assertion on $cacheKey argument of Cache::copyTo method:');
return false;
}));
$cacheMock
->expects($this->any())
->method('copyFrom')
2022-01-03 14:40:32 +00:00
->will($this->returnCallback(function ($cacheKey) use ($expectedCacheKey) {
$this->assertEquals($expectedCacheKey, $cacheKey, 'Failed assertion on $cacheKey argument of Cache::copyFrom method:');
return false;
}));
$httpDownloaderMock = $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock();
$httpDownloaderMock
->expects($this->any())
->method('addCopy')
2022-01-03 14:40:32 +00:00
->will($this->returnCallback(function ($url) use ($expectedUrl) {
$this->assertEquals($expectedUrl, $url, 'Failed assertion on $url argument of HttpDownloader::addCopy method:');
return \React\Promise\resolve(
new Response(array('url' => 'http://example.org/'), 200, array(), 'file~')
);
}));
$downloader = $this->getDownloader(null, $this->config, $dispatcher, $cacheMock, $httpDownloaderMock);
try {
$loop = new Loop($this->httpDownloader);
$promise = $downloader->download($packageMock, $path);
$loop->wait(array($promise));
$this->fail('Download was expected to throw');
} catch (\Exception $e) {
if (is_dir($path)) {
$fs = new Filesystem();
$fs->removeDirectory($path);
} elseif (is_file($path)) {
unlink($path);
}
$this->assertInstanceOf('UnexpectedValueException', $e, $e->getMessage());
$this->assertStringContainsString('could not be saved to', $e->getMessage());
}
}
public function testDownloadWithCustomCacheKey()
{
$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')));
$rootPackageMock = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock();
$rootPackageMock->expects($this->any())
->method('getScripts')
->will($this->returnValue(array()));
$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';
}
}));
2020-11-22 13:48:56 +00:00
$expectedUrl = 'url';
$customCacheKey = 'xyzzy';
$expectedCacheKey = '/'.sha1($customCacheKey).'.';
$dispatcher = new EventDispatcher(
$composerMock,
$this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
$this->getProcessExecutorMock()
);
2020-11-22 13:48:56 +00:00
$dispatcher->addListener(PluginEvents::PRE_FILE_DOWNLOAD, function (PreFileDownloadEvent $event) use ($customCacheKey) {
$event->setCustomCacheKey($customCacheKey);
});
$cacheMock = $this->getMockBuilder('Composer\Cache')
->disableOriginalConstructor()
->getMock();
$cacheMock
->expects($this->any())
->method('copyTo')
2022-01-03 14:40:32 +00:00
->will($this->returnCallback(function ($cacheKey) use ($expectedCacheKey) {
$this->assertEquals($expectedCacheKey, $cacheKey, 'Failed assertion on $cacheKey argument of Cache::copyTo method:');
return false;
}));
$cacheMock
->expects($this->any())
->method('copyFrom')
2022-01-03 14:40:32 +00:00
->will($this->returnCallback(function ($cacheKey) use ($expectedCacheKey) {
$this->assertEquals($expectedCacheKey, $cacheKey, 'Failed assertion on $cacheKey argument of Cache::copyFrom method:');
return false;
}));
$httpDownloaderMock = $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock();
$httpDownloaderMock
->expects($this->any())
->method('addCopy')
2022-01-03 14:40:32 +00:00
->will($this->returnCallback(function ($url) use ($expectedUrl) {
$this->assertEquals($expectedUrl, $url, 'Failed assertion on $url argument of HttpDownloader::addCopy method:');
return \React\Promise\resolve(
new Response(array('url' => 'http://example.org/'), 200, array(), 'file~')
);
}));
$downloader = $this->getDownloader(null, $this->config, $dispatcher, $cacheMock, $httpDownloaderMock);
try {
$loop = new Loop($this->httpDownloader);
$promise = $downloader->download($packageMock, $path);
$loop->wait(array($promise));
$this->fail('Download was expected to throw');
} catch (\Exception $e) {
if (is_dir($path)) {
$fs = new Filesystem();
$fs->removeDirectory($path);
2012-06-14 10:10:01 +00:00
} elseif (is_file($path)) {
2012-11-10 22:17:36 +00:00
unlink($path);
}
$this->assertInstanceOf('UnexpectedValueException', $e, $e->getMessage());
$this->assertStringContainsString('could not be saved to', $e->getMessage());
}
}
public function testCacheGarbageCollectionIsCalled()
{
$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'],
]);
$cacheMock = $this->getMockBuilder('Composer\Cache')
->disableOriginalConstructor()
->getMock();
$cacheMock
->expects($this->any())
->method('gcIsNecessary')
->will($this->returnValue(true));
$cacheMock
->expects($this->once())
->method('gc')
->with($expectedTtl, $this->anything());
$downloader = $this->getDownloader(null, $this->config, null, $cacheMock, null, null);
}
public function testDownloadFileWithInvalidChecksum()
{
$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)))
;
$filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
$path = $this->getUniqueTmpDirectory();
$downloader = $this->getDownloader(null, null, null, null, null, $filesystem);
2012-03-15 00:28:10 +00:00
// 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);
mkdir(dirname($dlFile), 0777, true);
touch($dlFile);
2012-03-15 00:28:10 +00:00
try {
$loop = new Loop($this->httpDownloader);
$promise = $downloader->download($packageMock, $path);
$loop->wait(array($promise));
$this->fail('Download was expected to throw');
} catch (\Exception $e) {
if (is_dir($path)) {
$fs = new Filesystem();
$fs->removeDirectory($path);
2012-06-14 10:10:01 +00:00
} elseif (is_file($path)) {
2012-11-10 22:17:36 +00:00
unlink($path);
}
$this->assertInstanceOf('UnexpectedValueException', $e, $e->getMessage());
$this->assertStringContainsString('checksum verification', $e->getMessage());
}
}
public function testDowngradeShowsAppropriateMessage()
{
2019-02-18 13:03:23 +00:00
$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'));
2019-02-18 13:03:23 +00:00
$newPackage = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
2019-01-29 13:22:20 +00:00
$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)));
2019-02-18 13:03:23 +00:00
$ioMock = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$ioMock->expects($this->atLeast(2))
->method('writeError')
->withConsecutive(
[$this->stringContains('Downloading')],
[$this->stringContains('Downgrading')]
);
$path = $this->getUniqueTmpDirectory();
2019-02-18 13:03:23 +00:00
$filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
$filesystem->expects($this->once())
2021-02-25 10:28:07 +00:00
->method('removeDirectoryAsync')
->will($this->returnValue(\React\Promise\resolve(true)));
$downloader = $this->getDownloader($ioMock, null, 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);
mkdir(dirname($dlFile), 0777, true);
touch($dlFile);
$loop = new Loop($this->httpDownloader);
$promise = $downloader->download($newPackage, $path, $oldPackage);
$loop->wait(array($promise));
$downloader->update($oldPackage, $newPackage, $path);
}
}