2012-02-28 10:59:18 +00:00
|
|
|
<?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\Downloader\FileDownloader;
|
2018-11-12 14:23:32 +00:00
|
|
|
use Composer\Test\TestCase;
|
2012-02-28 10:59:18 +00:00
|
|
|
use Composer\Util\Filesystem;
|
2019-01-17 16:12:33 +00:00
|
|
|
use Composer\Util\Http\Response;
|
|
|
|
use Composer\Util\Loop;
|
2012-02-28 10:59:18 +00:00
|
|
|
|
2016-01-21 12:01:55 +00:00
|
|
|
class FileDownloaderTest extends TestCase
|
2012-02-28 10:59:18 +00:00
|
|
|
{
|
2019-01-07 15:22:41 +00:00
|
|
|
private $httpDownloader;
|
2020-03-28 19:38:50 +00:00
|
|
|
private $config;
|
2019-01-07 15:22:41 +00:00
|
|
|
|
2018-10-31 11:44:54 +00:00
|
|
|
protected function getDownloader($io = null, $config = null, $eventDispatcher = null, $cache = null, $httpDownloader = null, $filesystem = null)
|
2012-03-15 00:28:10 +00:00
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
|
2020-03-28 19:38:50 +00:00
|
|
|
$this->config = $config ?: $this->getMockBuilder('Composer\Config')->getMock();
|
2018-10-31 11:44:54 +00:00
|
|
|
$httpDownloader = $httpDownloader ?: $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock();
|
2019-01-17 16:12:33 +00:00
|
|
|
$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
|
|
|
|
2020-03-28 19:38:50 +00:00
|
|
|
return new FileDownloader($io, $this->config, $httpDownloader, $eventDispatcher, $cache, $filesystem);
|
2012-03-15 00:28:10 +00:00
|
|
|
}
|
|
|
|
|
2012-02-28 10:59:18 +00:00
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testDownloadForPackageWithoutDistReference()
|
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
|
2012-02-28 10:59:18 +00:00
|
|
|
$packageMock->expects($this->once())
|
|
|
|
->method('getDistUrl')
|
|
|
|
->will($this->returnValue(null))
|
|
|
|
;
|
|
|
|
|
2012-03-15 00:28:10 +00:00
|
|
|
$downloader = $this->getDownloader();
|
2012-02-28 10:59:18 +00:00
|
|
|
$downloader->download($packageMock, '/path');
|
|
|
|
}
|
|
|
|
|
2012-03-25 23:56:24 +00:00
|
|
|
public function testDownloadToExistingFile()
|
2012-02-28 10:59:18 +00:00
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
|
2020-03-28 19:38:50 +00:00
|
|
|
$packageMock->expects($this->any())
|
2012-02-28 10:59:18 +00:00
|
|
|
->method('getDistUrl')
|
|
|
|
->will($this->returnValue('url'))
|
|
|
|
;
|
2020-03-28 19:38:50 +00:00
|
|
|
$packageMock->expects($this->any())
|
2013-07-01 23:45:43 +00:00
|
|
|
->method('getDistUrls')
|
|
|
|
->will($this->returnValue(array('url')))
|
|
|
|
;
|
2012-02-28 10:59:18 +00:00
|
|
|
|
2016-01-21 12:01:55 +00:00
|
|
|
$path = tempnam($this->getUniqueTmpDirectory(), 'c');
|
2012-03-15 00:28:10 +00:00
|
|
|
$downloader = $this->getDownloader();
|
2016-01-21 12:01:55 +00:00
|
|
|
|
2012-02-28 10:59:18 +00:00
|
|
|
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-02-28 10:59:18 +00:00
|
|
|
}
|
2012-03-25 23:56:24 +00:00
|
|
|
$this->assertInstanceOf('RuntimeException', $e);
|
2012-02-28 10:59:18 +00:00
|
|
|
$this->assertContains('exists and is not a directory', $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetFileName()
|
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
|
2012-02-28 10:59:18 +00:00
|
|
|
$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();
|
2012-02-28 10:59:18 +00:00
|
|
|
$method = new \ReflectionMethod($downloader, 'getFileName');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
2020-03-28 19:38:50 +00:00
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('vendor-dir')
|
|
|
|
->will($this->returnValue('/vendor'));
|
|
|
|
|
2020-05-22 11:28:47 +00:00
|
|
|
$this->assertRegExp('#/vendor/composer/tmp-[a-z0-9]+\.js#', $method->invoke($downloader, $packageMock, '/path'));
|
2012-02-28 10:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDownloadButFileIsUnsaved()
|
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
|
2012-02-28 10:59:18 +00:00
|
|
|
$packageMock->expects($this->any())
|
|
|
|
->method('getDistUrl')
|
2013-07-01 23:45:43 +00:00
|
|
|
->will($this->returnValue($distUrl = 'http://example.com/script.js'))
|
|
|
|
;
|
|
|
|
$packageMock->expects($this->once())
|
|
|
|
->method('getDistUrls')
|
|
|
|
->will($this->returnValue(array($distUrl)))
|
2012-02-28 10:59:18 +00:00
|
|
|
;
|
2013-08-19 07:38:25 +00:00
|
|
|
$packageMock->expects($this->atLeastOnce())
|
2014-05-07 16:25:28 +00:00
|
|
|
->method('getTransportOptions')
|
2013-08-19 07:38:25 +00:00
|
|
|
->will($this->returnValue(array()))
|
|
|
|
;
|
2012-02-28 10:59:18 +00:00
|
|
|
|
2016-01-21 12:01:55 +00:00
|
|
|
$path = $this->getUniqueTmpDirectory();
|
2018-04-12 08:24:56 +00:00
|
|
|
$ioMock = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
|
2012-02-28 10:59:18 +00:00
|
|
|
$ioMock->expects($this->any())
|
|
|
|
->method('write')
|
2014-06-10 14:02:44 +00:00
|
|
|
->will($this->returnCallback(function ($messages, $newline = true) use ($path) {
|
2012-02-28 10:59:18 +00:00
|
|
|
if (is_file($path.'/script.js')) {
|
|
|
|
unlink($path.'/script.js');
|
|
|
|
}
|
2012-06-14 10:10:01 +00:00
|
|
|
|
2012-02-28 10:59:18 +00:00
|
|
|
return $messages;
|
|
|
|
}))
|
|
|
|
;
|
|
|
|
|
2012-03-15 00:28:10 +00:00
|
|
|
$downloader = $this->getDownloader($ioMock);
|
2020-03-28 19:38:50 +00:00
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('vendor-dir')
|
|
|
|
->will($this->returnValue($path.'/vendor'));
|
|
|
|
|
2012-02-28 10:59:18 +00:00
|
|
|
try {
|
2019-01-17 16:12:33 +00:00
|
|
|
$loop = new Loop($this->httpDownloader);
|
2020-06-05 06:33:44 +00:00
|
|
|
$promise = $downloader->download($packageMock, $path);
|
2019-01-17 16:12:33 +00:00
|
|
|
$loop->wait(array($promise));
|
|
|
|
|
|
|
|
$this->fail('Download was expected to throw');
|
2012-02-28 10:59:18 +00:00
|
|
|
} 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);
|
2012-02-28 10:59:18 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 16:12:33 +00:00
|
|
|
$this->assertInstanceOf('UnexpectedValueException', $e, $e->getMessage());
|
2012-02-28 10:59:18 +00:00
|
|
|
$this->assertContains('could not be saved to', $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-24 09:55:25 +00:00
|
|
|
public function testCacheGarbageCollectionIsCalled()
|
|
|
|
{
|
|
|
|
$expectedTtl = '99999999';
|
|
|
|
|
2018-04-12 08:24:56 +00:00
|
|
|
$configMock = $this->getMockBuilder('Composer\Config')->getMock();
|
2013-11-24 09:55:25 +00:00
|
|
|
$configMock
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('get')
|
|
|
|
->with('cache-files-ttl')
|
|
|
|
->will($this->returnValue($expectedTtl));
|
|
|
|
$configMock
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('get')
|
|
|
|
->with('cache-files-maxsize')
|
|
|
|
->will($this->returnValue('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, $configMock, null, $cacheMock, null, null);
|
|
|
|
}
|
|
|
|
|
2012-02-28 10:59:18 +00:00
|
|
|
public function testDownloadFileWithInvalidChecksum()
|
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
|
2012-02-28 10:59:18 +00:00
|
|
|
$packageMock->expects($this->any())
|
|
|
|
->method('getDistUrl')
|
2013-07-01 23:45:43 +00:00
|
|
|
->will($this->returnValue($distUrl = 'http://example.com/script.js'))
|
2012-02-28 10:59:18 +00:00
|
|
|
;
|
2013-08-19 07:38:25 +00:00
|
|
|
$packageMock->expects($this->atLeastOnce())
|
2014-05-07 16:25:28 +00:00
|
|
|
->method('getTransportOptions')
|
2013-08-19 07:38:25 +00:00
|
|
|
->will($this->returnValue(array()))
|
|
|
|
;
|
2012-02-28 10:59:18 +00:00
|
|
|
$packageMock->expects($this->any())
|
|
|
|
->method('getDistSha1Checksum')
|
|
|
|
->will($this->returnValue('invalid'))
|
|
|
|
;
|
2013-07-01 23:45:43 +00:00
|
|
|
$packageMock->expects($this->once())
|
|
|
|
->method('getDistUrls')
|
|
|
|
->will($this->returnValue(array($distUrl)))
|
|
|
|
;
|
2018-04-12 08:24:56 +00:00
|
|
|
$filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
|
2012-02-28 10:59:18 +00:00
|
|
|
|
2016-01-21 12:01:55 +00:00
|
|
|
$path = $this->getUniqueTmpDirectory();
|
2013-11-24 09:55:25 +00:00
|
|
|
$downloader = $this->getDownloader(null, null, null, null, null, $filesystem);
|
2020-03-28 19:38:50 +00:00
|
|
|
|
2012-03-15 00:28:10 +00:00
|
|
|
// make sure the file expected to be downloaded is on disk already
|
2020-03-28 19:38:50 +00:00
|
|
|
$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
|
|
|
|
2012-02-28 10:59:18 +00:00
|
|
|
try {
|
2019-01-17 16:12:33 +00:00
|
|
|
$loop = new Loop($this->httpDownloader);
|
2020-06-05 06:33:44 +00:00
|
|
|
$promise = $downloader->download($packageMock, $path);
|
2019-01-17 16:12:33 +00:00
|
|
|
$loop->wait(array($promise));
|
|
|
|
|
|
|
|
$this->fail('Download was expected to throw');
|
2012-02-28 10:59:18 +00:00
|
|
|
} 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);
|
2012-02-28 10:59:18 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 16:12:33 +00:00
|
|
|
$this->assertInstanceOf('UnexpectedValueException', $e, $e->getMessage());
|
2012-02-28 10:59:18 +00:00
|
|
|
$this->assertContains('checksum verification', $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
2018-02-15 22:38:41 +00:00
|
|
|
|
|
|
|
public function testDowngradeShowsAppropriateMessage()
|
|
|
|
{
|
2019-02-18 13:03:23 +00:00
|
|
|
$oldPackage = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
|
2018-02-15 22:38:41 +00:00
|
|
|
$oldPackage->expects($this->once())
|
2019-01-29 10:14:22 +00:00
|
|
|
->method('getFullPrettyVersion')
|
2018-04-13 11:48:30 +00:00
|
|
|
->will($this->returnValue('1.2.0'));
|
2018-02-15 22:38:41 +00:00
|
|
|
$oldPackage->expects($this->once())
|
2018-04-13 11:48:30 +00:00
|
|
|
->method('getVersion')
|
|
|
|
->will($this->returnValue('1.2.0.0'));
|
2018-02-15 22:38:41 +00:00
|
|
|
|
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())
|
2019-01-29 10:14:22 +00:00
|
|
|
->method('getFullPrettyVersion')
|
2018-04-13 11:48:30 +00:00
|
|
|
->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)));
|
2018-02-15 22:38:41 +00:00
|
|
|
|
2019-02-18 13:03:23 +00:00
|
|
|
$ioMock = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
|
2018-09-08 01:29:49 +00:00
|
|
|
$ioMock->expects($this->at(0))
|
2019-01-17 16:12:33 +00:00
|
|
|
->method('writeError')
|
|
|
|
->with($this->stringContains('Downloading'));
|
|
|
|
|
|
|
|
$ioMock->expects($this->at(1))
|
2018-02-15 22:38:41 +00:00
|
|
|
->method('writeError')
|
|
|
|
->with($this->stringContains('Downgrading'));
|
|
|
|
|
|
|
|
$path = $this->getUniqueTmpDirectory();
|
2019-02-18 13:03:23 +00:00
|
|
|
$filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
|
2018-02-15 22:38:41 +00:00
|
|
|
$filesystem->expects($this->once())
|
|
|
|
->method('removeDirectory')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$downloader = $this->getDownloader($ioMock, null, null, null, null, $filesystem);
|
2020-03-28 19:38:50 +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, $newPackage, $path);
|
|
|
|
mkdir(dirname($dlFile), 0777, true);
|
|
|
|
touch($dlFile);
|
|
|
|
|
2019-01-17 16:12:33 +00:00
|
|
|
$loop = new Loop($this->httpDownloader);
|
2020-06-05 06:33:44 +00:00
|
|
|
$promise = $downloader->download($newPackage, $path, $oldPackage);
|
2019-01-17 16:12:33 +00:00
|
|
|
$loop->wait(array($promise));
|
|
|
|
|
2018-04-13 11:48:30 +00:00
|
|
|
$downloader->update($oldPackage, $newPackage, $path);
|
2018-02-15 22:38:41 +00:00
|
|
|
}
|
2012-02-28 10:59:18 +00:00
|
|
|
}
|