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;
|
|
|
|
|
2020-02-07 03:18:45 +00:00
|
|
|
use Composer\Test\TestCase;
|
2017-11-04 14:52:13 +00:00
|
|
|
|
|
|
|
class ArchiveDownloaderTest extends TestCase
|
2012-02-28 10:59:18 +00:00
|
|
|
{
|
2021-10-30 08:21:50 +00:00
|
|
|
/** @var \Composer\Config&\PHPUnit\Framework\MockObject\MockObject */
|
2020-03-28 19:38:50 +00:00
|
|
|
protected $config;
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetFileName(): void
|
2012-02-28 10:59:18 +00:00
|
|
|
{
|
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')
|
|
|
|
->will($this->returnValue('http://example.com/script.js'))
|
|
|
|
;
|
|
|
|
|
2018-04-12 08:24:56 +00:00
|
|
|
$downloader = $this->getArchiveDownloaderMock();
|
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->any())
|
|
|
|
->method('get')
|
|
|
|
->with('vendor-dir')
|
|
|
|
->will($this->returnValue('/vendor'));
|
|
|
|
|
2012-02-28 10:59:18 +00:00
|
|
|
$first = $method->invoke($downloader, $packageMock, '/path');
|
2020-09-11 09:27:26 +00:00
|
|
|
$this->assertMatchesRegularExpression('#/vendor/composer/tmp-[a-z0-9]+\.js#', $first);
|
2012-02-28 10:59:18 +00:00
|
|
|
$this->assertSame($first, $method->invoke($downloader, $packageMock, '/path'));
|
|
|
|
}
|
2012-03-09 18:30:37 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testProcessUrl(): void
|
2012-03-09 18:30:37 +00:00
|
|
|
{
|
2013-07-04 22:47:18 +00:00
|
|
|
if (!extension_loaded('openssl')) {
|
2013-07-04 22:44:20 +00:00
|
|
|
$this->markTestSkipped('Requires openssl');
|
|
|
|
}
|
|
|
|
|
2018-04-12 08:24:56 +00:00
|
|
|
$downloader = $this->getArchiveDownloaderMock();
|
2012-03-09 18:30:37 +00:00
|
|
|
$method = new \ReflectionMethod($downloader, 'processUrl');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
$expected = 'https://github.com/composer/composer/zipball/master';
|
2018-04-12 08:24:56 +00:00
|
|
|
$url = $method->invoke($downloader, $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(), $expected);
|
2012-03-09 18:30:37 +00:00
|
|
|
|
2013-07-04 22:44:20 +00:00
|
|
|
$this->assertEquals($expected, $url);
|
2012-11-04 13:01:22 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testProcessUrl2(): void
|
2012-11-04 13:01:22 +00:00
|
|
|
{
|
2013-07-04 22:47:18 +00:00
|
|
|
if (!extension_loaded('openssl')) {
|
2013-07-04 22:44:20 +00:00
|
|
|
$this->markTestSkipped('Requires openssl');
|
|
|
|
}
|
|
|
|
|
2018-04-12 08:24:56 +00:00
|
|
|
$downloader = $this->getArchiveDownloaderMock();
|
2012-11-04 13:01:22 +00:00
|
|
|
$method = new \ReflectionMethod($downloader, 'processUrl');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
$expected = 'https://github.com/composer/composer/archive/master.tar.gz';
|
2018-04-12 08:24:56 +00:00
|
|
|
$url = $method->invoke($downloader, $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(), $expected);
|
2012-11-04 13:01:22 +00:00
|
|
|
|
2013-07-04 22:44:20 +00:00
|
|
|
$this->assertEquals($expected, $url);
|
2012-03-09 18:30:37 +00:00
|
|
|
}
|
2012-12-05 18:20:36 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testProcessUrl3(): void
|
2012-12-05 18:20:36 +00:00
|
|
|
{
|
2013-07-04 22:47:18 +00:00
|
|
|
if (!extension_loaded('openssl')) {
|
2013-07-04 22:44:20 +00:00
|
|
|
$this->markTestSkipped('Requires openssl');
|
|
|
|
}
|
|
|
|
|
2018-04-12 08:24:56 +00:00
|
|
|
$downloader = $this->getArchiveDownloaderMock();
|
2012-12-05 18:20:36 +00:00
|
|
|
$method = new \ReflectionMethod($downloader, 'processUrl');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
$expected = 'https://api.github.com/repos/composer/composer/zipball/master';
|
2018-04-12 08:24:56 +00:00
|
|
|
$url = $method->invoke($downloader, $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(), $expected);
|
2012-12-05 18:20:36 +00:00
|
|
|
|
2013-07-04 22:44:20 +00:00
|
|
|
$this->assertEquals($expected, $url);
|
2012-12-05 18:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideUrls
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testProcessUrlRewriteDist(string $url): void
|
2012-12-05 18:20:36 +00:00
|
|
|
{
|
2013-07-04 22:47:18 +00:00
|
|
|
if (!extension_loaded('openssl')) {
|
2013-07-04 22:44:20 +00:00
|
|
|
$this->markTestSkipped('Requires openssl');
|
|
|
|
}
|
|
|
|
|
2018-04-12 08:24:56 +00:00
|
|
|
$downloader = $this->getArchiveDownloaderMock();
|
2012-12-05 18:20:36 +00:00
|
|
|
$method = new \ReflectionMethod($downloader, 'processUrl');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
$type = strpos($url, 'tar') ? 'tar' : 'zip';
|
|
|
|
$expected = 'https://api.github.com/repos/composer/composer/'.$type.'ball/ref';
|
|
|
|
|
2018-04-12 08:24:56 +00:00
|
|
|
$package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
|
2012-12-05 18:20:36 +00:00
|
|
|
$package->expects($this->any())
|
|
|
|
->method('getDistReference')
|
|
|
|
->will($this->returnValue('ref'));
|
|
|
|
$url = $method->invoke($downloader, $package, $url);
|
|
|
|
|
2013-07-04 22:44:20 +00:00
|
|
|
$this->assertEquals($expected, $url);
|
2012-12-05 18:20:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
public static function provideUrls(): array
|
2012-12-05 18:20:36 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
['https://api.github.com/repos/composer/composer/zipball/master'],
|
|
|
|
['https://api.github.com/repos/composer/composer/tarball/master'],
|
|
|
|
['https://github.com/composer/composer/zipball/master'],
|
|
|
|
['https://www.github.com/composer/composer/tarball/master'],
|
|
|
|
['https://github.com/composer/composer/archive/master.zip'],
|
|
|
|
['https://github.com/composer/composer/archive/master.tar.gz'],
|
|
|
|
];
|
2012-12-05 18:20:36 +00:00
|
|
|
}
|
2015-06-19 13:07:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideBitbucketUrls
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testProcessUrlRewriteBitbucketDist(string $url, string $extension): void
|
2015-06-19 13:07:16 +00:00
|
|
|
{
|
|
|
|
if (!extension_loaded('openssl')) {
|
|
|
|
$this->markTestSkipped('Requires openssl');
|
|
|
|
}
|
|
|
|
|
2018-04-12 08:24:56 +00:00
|
|
|
$downloader = $this->getArchiveDownloaderMock();
|
2015-06-19 13:07:16 +00:00
|
|
|
$method = new \ReflectionMethod($downloader, 'processUrl');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
2018-09-08 01:35:35 +00:00
|
|
|
$url .= '.' . $extension;
|
2015-06-19 13:07:16 +00:00
|
|
|
$expected = 'https://bitbucket.org/davereid/drush-virtualhost/get/ref.' . $extension;
|
|
|
|
|
2018-04-12 08:24:56 +00:00
|
|
|
$package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
|
2015-06-19 13:07:16 +00:00
|
|
|
$package->expects($this->any())
|
|
|
|
->method('getDistReference')
|
|
|
|
->will($this->returnValue('ref'));
|
|
|
|
$url = $method->invoke($downloader, $package, $url);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $url);
|
|
|
|
}
|
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
public static function provideBitbucketUrls(): array
|
2015-06-19 13:07:16 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
['https://bitbucket.org/davereid/drush-virtualhost/get/77ca490c26ac818e024d1138aa8bd3677d1ef21f', 'zip'],
|
|
|
|
['https://bitbucket.org/davereid/drush-virtualhost/get/master', 'tar.gz'],
|
|
|
|
['https://bitbucket.org/davereid/drush-virtualhost/get/v1.0', 'tar.bz2'],
|
|
|
|
];
|
2015-06-19 13:07:16 +00:00
|
|
|
}
|
2018-04-12 08:24:56 +00:00
|
|
|
|
2021-11-02 13:32:09 +00:00
|
|
|
/**
|
|
|
|
* @return \Composer\Downloader\ArchiveDownloader&\PHPUnit\Framework\MockObject\MockObject
|
|
|
|
*/
|
2018-04-12 08:24:56 +00:00
|
|
|
private function getArchiveDownloaderMock()
|
|
|
|
{
|
|
|
|
return $this->getMockForAbstractClass(
|
|
|
|
'Composer\Downloader\ArchiveDownloader',
|
2022-08-17 12:20:07 +00:00
|
|
|
[
|
2018-10-31 11:44:54 +00:00
|
|
|
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
|
2020-03-28 19:38:50 +00:00
|
|
|
$this->config = $this->getMockBuilder('Composer\Config')->getMock(),
|
|
|
|
new \Composer\Util\HttpDownloader($io, $this->config),
|
2022-08-17 12:20:07 +00:00
|
|
|
]
|
2018-04-12 08:24:56 +00:00
|
|
|
);
|
|
|
|
}
|
2012-02-28 10:59:18 +00:00
|
|
|
}
|