2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2012-02-28 10:59:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2020-10-16 03:42:11 +00:00
|
|
|
use Composer\EventDispatcher\EventDispatcher;
|
|
|
|
use Composer\Plugin\PluginEvents;
|
|
|
|
use Composer\Plugin\PreFileDownloadEvent;
|
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;
|
2022-02-22 15:47:09 +00:00
|
|
|
use Composer\Config;
|
|
|
|
use Composer\Composer;
|
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
|
|
|
{
|
2021-10-30 08:21:50 +00:00
|
|
|
/** @var \Composer\Util\HttpDownloader&\PHPUnit\Framework\MockObject\MockObject */
|
2019-01-07 15:22:41 +00:00
|
|
|
private $httpDownloader;
|
|
|
|
|
2021-12-08 16:03:05 +00:00
|
|
|
public function setUp(): void
|
2021-10-30 08:21:50 +00:00
|
|
|
{
|
|
|
|
$this->httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Composer\EventDispatcher\EventDispatcher $eventDispatcher
|
|
|
|
* @param \Composer\Cache $cache
|
|
|
|
* @param \Composer\Util\HttpDownloader&\PHPUnit\Framework\MockObject\MockObject $httpDownloader
|
|
|
|
* @param \Composer\Util\Filesystem $filesystem
|
|
|
|
*/
|
2022-08-17 12:20:07 +00:00
|
|
|
protected function getDownloader(?\Composer\IO\IOInterface $io = null, ?Config $config = null, ?EventDispatcher $eventDispatcher = null, ?\Composer\Cache $cache = null, $httpDownloader = null, ?Filesystem $filesystem = null): FileDownloader
|
2012-03-15 00:28:10 +00:00
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
|
2022-02-22 15:47:09 +00:00
|
|
|
$config = $config ?: $this->getConfig();
|
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')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnValue(\React\Promise\resolve(new Response(['url' => 'http://example.org/'], 200, [], 'file~'))));
|
2019-01-17 16:12:33 +00:00
|
|
|
$this->httpDownloader = $httpDownloader;
|
2012-06-14 10:10:01 +00:00
|
|
|
|
2022-02-22 15:47:09 +00:00
|
|
|
return new FileDownloader($io, $config, $httpDownloader, $eventDispatcher, $cache, $filesystem);
|
2012-03-15 00:28:10 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testDownloadForPackageWithoutDistReference(): void
|
2012-02-28 10:59:18 +00:00
|
|
|
{
|
2022-11-24 13:39:08 +00:00
|
|
|
$package = self::getPackage();
|
2012-02-28 10:59:18 +00:00
|
|
|
|
2021-12-09 19:55:26 +00:00
|
|
|
self::expectException('InvalidArgumentException');
|
2020-09-10 15:21:11 +00:00
|
|
|
|
2012-03-15 00:28:10 +00:00
|
|
|
$downloader = $this->getDownloader();
|
2022-02-22 15:47:09 +00:00
|
|
|
$downloader->download($package, '/path');
|
2012-02-28 10:59:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testDownloadToExistingFile(): void
|
2012-02-28 10:59:18 +00:00
|
|
|
{
|
2022-11-24 13:39:08 +00:00
|
|
|
$package = self::getPackage();
|
2022-02-22 15:47:09 +00:00
|
|
|
$package->setDistUrl('url');
|
2012-02-28 10:59:18 +00:00
|
|
|
|
2022-05-11 14:05:35 +00:00
|
|
|
$path = $this->createTempFile(self::getUniqueTmpDirectory());
|
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 {
|
2022-02-22 15:47:09 +00:00
|
|
|
$downloader->download($package, $path);
|
2012-02-28 10:59:18 +00:00
|
|
|
$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);
|
2020-09-10 15:21:11 +00:00
|
|
|
$this->assertStringContainsString('exists and is not a directory', $e->getMessage());
|
2012-02-28 10:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetFileName(): void
|
2012-02-28 10:59:18 +00:00
|
|
|
{
|
2022-11-24 13:39:08 +00:00
|
|
|
$package = self::getPackage();
|
2022-02-22 15:47:09 +00:00
|
|
|
$package->setDistUrl('http://example.com/script.js');
|
2012-02-28 10:59:18 +00:00
|
|
|
|
2022-02-22 15:47:09 +00:00
|
|
|
$config = $this->getConfig(['vendor-dir' => '/vendor']);
|
|
|
|
$downloader = $this->getDownloader(null, $config);
|
2012-02-28 10:59:18 +00:00
|
|
|
$method = new \ReflectionMethod($downloader, 'getFileName');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
2022-02-22 15:47:09 +00:00
|
|
|
$this->assertMatchesRegularExpression('#/vendor/composer/tmp-[a-z0-9]+\.js#', $method->invoke($downloader, $package, '/path'));
|
2012-02-28 10:59:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testDownloadButFileIsUnsaved(): void
|
2012-02-28 10:59:18 +00:00
|
|
|
{
|
2022-11-24 13:39:08 +00:00
|
|
|
$package = self::getPackage();
|
2022-02-22 15:47:09 +00:00
|
|
|
$package->setDistUrl('http://example.com/script.js');
|
2012-02-28 10:59:18 +00:00
|
|
|
|
2022-05-11 14:05:35 +00:00
|
|
|
$path = self::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')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnCallback(static 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;
|
|
|
|
}))
|
|
|
|
;
|
|
|
|
|
2022-02-22 15:47:09 +00:00
|
|
|
$config = $this->getConfig(['vendor-dir' => $path.'/vendor']);
|
|
|
|
$downloader = $this->getDownloader($ioMock, $config);
|
2020-03-28 19:38:50 +00:00
|
|
|
|
2020-10-16 03:42:11 +00:00
|
|
|
try {
|
|
|
|
$loop = new Loop($this->httpDownloader);
|
2022-02-22 15:47:09 +00:00
|
|
|
$promise = $downloader->download($package, $path);
|
2022-08-17 12:20:07 +00:00
|
|
|
$loop->wait([$promise]);
|
2020-10-16 03:42:11 +00:00
|
|
|
|
|
|
|
$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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testDownloadWithCustomProcessedUrl(): void
|
2020-10-16 03:42:11 +00:00
|
|
|
{
|
2022-05-11 14:05:35 +00:00
|
|
|
$path = self::getUniqueTmpDirectory();
|
2020-10-16 03:42:11 +00:00
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
$package = self::getPackage();
|
2022-02-22 15:47:09 +00:00
|
|
|
$package->setDistUrl('url');
|
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
$rootPackage = self::getRootPackage();
|
2022-02-22 15:47:09 +00:00
|
|
|
|
|
|
|
$config = $this->getConfig([
|
|
|
|
'vendor-dir' => $path.'/vendor',
|
|
|
|
'bin-dir' => $path.'/vendor/bin',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$composer = new Composer;
|
|
|
|
$composer->setPackage($rootPackage);
|
|
|
|
$composer->setConfig($config);
|
2020-10-16 03:42:11 +00:00
|
|
|
|
2020-11-22 13:48:56 +00:00
|
|
|
$expectedUrl = 'foobar';
|
2022-02-22 15:47:09 +00:00
|
|
|
$expectedCacheKey = 'dummy/pkg/'.sha1($expectedUrl).'.';
|
2020-10-16 03:42:11 +00:00
|
|
|
|
|
|
|
$dispatcher = new EventDispatcher(
|
2022-02-22 15:47:09 +00:00
|
|
|
$composer,
|
2020-10-16 03:42:11 +00:00
|
|
|
$this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
|
2021-12-09 16:09:07 +00:00
|
|
|
$this->getProcessExecutorMock()
|
2020-10-16 03:42:11 +00:00
|
|
|
);
|
2022-08-17 12:20:07 +00:00
|
|
|
$dispatcher->addListener(PluginEvents::PRE_FILE_DOWNLOAD, static function (PreFileDownloadEvent $event) use ($expectedUrl): void {
|
2020-10-16 03:42:11 +00:00
|
|
|
$event->setProcessedUrl($expectedUrl);
|
|
|
|
});
|
|
|
|
|
|
|
|
$cacheMock = $this->getMockBuilder('Composer\Cache')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$cacheMock
|
|
|
|
->expects($this->any())
|
|
|
|
->method('copyTo')
|
2022-02-21 12:42:28 +00:00
|
|
|
->will($this->returnCallback(function ($cacheKey) use ($expectedCacheKey): bool {
|
2022-01-03 14:40:32 +00:00
|
|
|
$this->assertEquals($expectedCacheKey, $cacheKey, 'Failed assertion on $cacheKey argument of Cache::copyTo method:');
|
2020-10-16 03:42:11 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}));
|
|
|
|
$cacheMock
|
|
|
|
->expects($this->any())
|
|
|
|
->method('copyFrom')
|
2022-02-21 12:42:28 +00:00
|
|
|
->will($this->returnCallback(function ($cacheKey) use ($expectedCacheKey): bool {
|
2022-01-03 14:40:32 +00:00
|
|
|
$this->assertEquals($expectedCacheKey, $cacheKey, 'Failed assertion on $cacheKey argument of Cache::copyFrom method:');
|
2020-10-16 03:42:11 +00:00
|
|
|
|
|
|
|
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:');
|
2020-10-16 03:42:11 +00:00
|
|
|
|
|
|
|
return \React\Promise\resolve(
|
2022-08-17 12:20:07 +00:00
|
|
|
new Response(['url' => 'http://example.org/'], 200, [], 'file~')
|
2020-10-16 03:42:11 +00:00
|
|
|
);
|
|
|
|
}));
|
|
|
|
|
2022-02-22 15:47:09 +00:00
|
|
|
$downloader = $this->getDownloader(null, $config, $dispatcher, $cacheMock, $httpDownloaderMock);
|
2020-10-16 03:42:11 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$loop = new Loop($this->httpDownloader);
|
2022-02-22 15:47:09 +00:00
|
|
|
$promise = $downloader->download($package, $path);
|
2022-08-17 12:20:07 +00:00
|
|
|
$loop->wait([$promise]);
|
2020-10-16 03:42:11 +00:00
|
|
|
|
|
|
|
$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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testDownloadWithCustomCacheKey(): void
|
2020-10-16 03:42:11 +00:00
|
|
|
{
|
2022-05-11 14:05:35 +00:00
|
|
|
$path = self::getUniqueTmpDirectory();
|
2020-10-16 03:42:11 +00:00
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
$package = self::getPackage();
|
2022-02-22 15:47:09 +00:00
|
|
|
$package->setDistUrl('url');
|
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
$rootPackage = self::getRootPackage();
|
2022-02-22 15:47:09 +00:00
|
|
|
|
|
|
|
$config = $this->getConfig([
|
|
|
|
'vendor-dir' => $path.'/vendor',
|
|
|
|
'bin-dir' => $path.'/vendor/bin',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$composer = new Composer;
|
|
|
|
$composer->setPackage($rootPackage);
|
|
|
|
$composer->setConfig($config);
|
2020-10-16 03:42:11 +00:00
|
|
|
|
2020-11-22 13:48:56 +00:00
|
|
|
$expectedUrl = 'url';
|
|
|
|
$customCacheKey = 'xyzzy';
|
2022-02-22 15:47:09 +00:00
|
|
|
$expectedCacheKey = 'dummy/pkg/'.sha1($customCacheKey).'.';
|
2020-10-16 03:42:11 +00:00
|
|
|
|
|
|
|
$dispatcher = new EventDispatcher(
|
2022-02-22 15:47:09 +00:00
|
|
|
$composer,
|
2020-10-16 03:42:11 +00:00
|
|
|
$this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
|
2021-12-09 16:09:07 +00:00
|
|
|
$this->getProcessExecutorMock()
|
2020-10-16 03:42:11 +00:00
|
|
|
);
|
2022-08-17 12:20:07 +00:00
|
|
|
$dispatcher->addListener(PluginEvents::PRE_FILE_DOWNLOAD, static function (PreFileDownloadEvent $event) use ($customCacheKey): void {
|
2020-10-16 03:42:11 +00:00
|
|
|
$event->setCustomCacheKey($customCacheKey);
|
|
|
|
});
|
|
|
|
|
|
|
|
$cacheMock = $this->getMockBuilder('Composer\Cache')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$cacheMock
|
|
|
|
->expects($this->any())
|
|
|
|
->method('copyTo')
|
2022-02-21 12:42:28 +00:00
|
|
|
->will($this->returnCallback(function ($cacheKey) use ($expectedCacheKey): bool {
|
2022-01-03 14:40:32 +00:00
|
|
|
$this->assertEquals($expectedCacheKey, $cacheKey, 'Failed assertion on $cacheKey argument of Cache::copyTo method:');
|
2020-10-16 03:42:11 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}));
|
|
|
|
$cacheMock
|
|
|
|
->expects($this->any())
|
|
|
|
->method('copyFrom')
|
2022-02-21 12:42:28 +00:00
|
|
|
->will($this->returnCallback(function ($cacheKey) use ($expectedCacheKey): bool {
|
2022-01-03 14:40:32 +00:00
|
|
|
$this->assertEquals($expectedCacheKey, $cacheKey, 'Failed assertion on $cacheKey argument of Cache::copyFrom method:');
|
2020-10-16 03:42:11 +00:00
|
|
|
|
|
|
|
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:');
|
2020-10-16 03:42:11 +00:00
|
|
|
|
|
|
|
return \React\Promise\resolve(
|
2022-08-17 12:20:07 +00:00
|
|
|
new Response(['url' => 'http://example.org/'], 200, [], 'file~')
|
2020-10-16 03:42:11 +00:00
|
|
|
);
|
|
|
|
}));
|
|
|
|
|
2022-02-22 15:47:09 +00:00
|
|
|
$downloader = $this->getDownloader(null, $config, $dispatcher, $cacheMock, $httpDownloaderMock);
|
2020-10-16 03:42:11 +00:00
|
|
|
|
2012-02-28 10:59:18 +00:00
|
|
|
try {
|
2019-01-17 16:12:33 +00:00
|
|
|
$loop = new Loop($this->httpDownloader);
|
2022-02-22 15:47:09 +00:00
|
|
|
$promise = $downloader->download($package, $path);
|
2022-08-17 12:20:07 +00:00
|
|
|
$loop->wait([$promise]);
|
2019-01-17 16:12:33 +00:00
|
|
|
|
|
|
|
$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());
|
2020-09-10 15:21:11 +00:00
|
|
|
$this->assertStringContainsString('could not be saved to', $e->getMessage());
|
2012-02-28 10:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testCacheGarbageCollectionIsCalled(): void
|
2013-11-24 09:55:25 +00:00
|
|
|
{
|
|
|
|
$expectedTtl = '99999999';
|
|
|
|
|
2022-02-22 15:47:09 +00:00
|
|
|
$config = $this->getConfig([
|
|
|
|
'cache-files-ttl' => $expectedTtl,
|
|
|
|
'cache-files-maxsize' => '500M',
|
|
|
|
]);
|
2013-11-24 09:55:25 +00:00
|
|
|
|
|
|
|
$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());
|
|
|
|
|
2022-02-22 15:47:09 +00:00
|
|
|
$downloader = $this->getDownloader(null, $config, null, $cacheMock, null, null);
|
2013-11-24 09:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testDownloadFileWithInvalidChecksum(): void
|
2012-02-28 10:59:18 +00:00
|
|
|
{
|
2022-11-24 13:39:08 +00:00
|
|
|
$package = self::getPackage();
|
2022-02-22 15:47:09 +00:00
|
|
|
$package->setDistUrl($distUrl = 'http://example.com/script.js');
|
|
|
|
$package->setDistSha1Checksum('invalid');
|
|
|
|
|
2018-04-12 08:24:56 +00:00
|
|
|
$filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
|
2012-02-28 10:59:18 +00:00
|
|
|
|
2022-05-11 14:05:35 +00:00
|
|
|
$path = self::getUniqueTmpDirectory();
|
2022-02-22 15:47:09 +00:00
|
|
|
$config = $this->getConfig(['vendor-dir' => $path.'/vendor']);
|
2020-03-28 19:38:50 +00:00
|
|
|
|
2022-02-22 15:47:09 +00:00
|
|
|
$downloader = $this->getDownloader(null, $config, null, null, null, $filesystem);
|
2020-03-28 19:38:50 +00:00
|
|
|
|
2022-02-22 15:47:09 +00:00
|
|
|
// make sure the file expected to be downloaded is on disk already
|
2020-03-28 19:38:50 +00:00
|
|
|
$method = new \ReflectionMethod($downloader, 'getFileName');
|
|
|
|
$method->setAccessible(true);
|
2022-02-22 15:47:09 +00:00
|
|
|
$dlFile = $method->invoke($downloader, $package, $path);
|
2020-03-28 19:38:50 +00:00
|
|
|
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);
|
2022-02-22 15:47:09 +00:00
|
|
|
$promise = $downloader->download($package, $path);
|
2022-08-17 12:20:07 +00:00
|
|
|
$loop->wait([$promise]);
|
2019-01-17 16:12:33 +00:00
|
|
|
|
|
|
|
$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());
|
2020-09-10 15:21:11 +00:00
|
|
|
$this->assertStringContainsString('checksum verification', $e->getMessage());
|
2012-02-28 10:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-15 22:38:41 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testDowngradeShowsAppropriateMessage(): void
|
2018-02-15 22:38:41 +00:00
|
|
|
{
|
2022-11-24 13:39:08 +00:00
|
|
|
$oldPackage = self::getPackage('dummy/pkg', '1.2.0');
|
|
|
|
$newPackage = self::getPackage('dummy/pkg', '1.0.0');
|
2022-02-22 15:47:09 +00:00
|
|
|
$newPackage->setDistUrl($distUrl = 'http://example.com/script.js');
|
2018-02-15 22:38:41 +00:00
|
|
|
|
2023-06-07 12:35:16 +00:00
|
|
|
$ioMock = $this->getIOMock();
|
|
|
|
$ioMock->expects([
|
|
|
|
['text' => '{Downloading .*}', 'regex' => true],
|
|
|
|
['text' => '{Downgrading .*}', 'regex' => true],
|
|
|
|
]);
|
2018-02-15 22:38:41 +00:00
|
|
|
|
2022-05-11 14:05:35 +00:00
|
|
|
$path = self::getUniqueTmpDirectory();
|
2022-02-22 15:47:09 +00:00
|
|
|
$config = $this->getConfig(['vendor-dir' => $path.'/vendor']);
|
|
|
|
|
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())
|
2021-02-25 10:28:07 +00:00
|
|
|
->method('removeDirectoryAsync')
|
|
|
|
->will($this->returnValue(\React\Promise\resolve(true)));
|
2018-02-15 22:38:41 +00:00
|
|
|
|
2022-02-22 15:47:09 +00:00
|
|
|
$downloader = $this->getDownloader($ioMock, $config, null, null, null, $filesystem);
|
2020-03-28 19:38:50 +00:00
|
|
|
|
|
|
|
// make sure the file expected to be downloaded is on disk already
|
|
|
|
$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);
|
2022-08-17 12:20:07 +00:00
|
|
|
$loop->wait([$promise]);
|
2019-01-17 16:12:33 +00:00
|
|
|
|
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
|
|
|
}
|