Merge remote-tracking branch 'hason/filedownloader'
commit
9bcea6f485
@ -0,0 +1,99 @@
|
||||
<?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\Downloader;
|
||||
|
||||
use Composer\IO\IOInterface;
|
||||
use Composer\Package\PackageInterface;
|
||||
use Composer\Util\Filesystem;
|
||||
use Composer\Util\RemoteFilesystem;
|
||||
|
||||
/**
|
||||
* Base downloader for archives
|
||||
*
|
||||
* @author Kirill chEbba Chebunin <iam@chebba.org>
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
* @author François Pluchino <francois.pluchino@opendisplay.com>
|
||||
*/
|
||||
abstract class ArchiveDownloader extends FileDownloader
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function download(PackageInterface $package, $path)
|
||||
{
|
||||
parent::download($package, $path);
|
||||
|
||||
$fileName = $this->getFileName($package, $path);
|
||||
$this->io->write(' Unpacking archive');
|
||||
$this->extract($fileName, $path);
|
||||
|
||||
$this->io->write(' Cleaning up');
|
||||
unlink($fileName);
|
||||
|
||||
// If we have only a one dir inside it suppose to be a package itself
|
||||
$contentDir = glob($path . '/*');
|
||||
if (1 === count($contentDir)) {
|
||||
$contentDir = $contentDir[0];
|
||||
|
||||
// Rename the content directory to avoid error when moving up
|
||||
// a child folder with the same name
|
||||
$temporaryName = md5(time().rand());
|
||||
rename($contentDir, $temporaryName);
|
||||
$contentDir = $temporaryName;
|
||||
|
||||
foreach (array_merge(glob($contentDir . '/.*'), glob($contentDir . '/*')) as $file) {
|
||||
if (trim(basename($file), '.')) {
|
||||
rename($file, $path . '/' . basename($file));
|
||||
}
|
||||
}
|
||||
rmdir($contentDir);
|
||||
}
|
||||
|
||||
$this->io->write('');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getFileName(PackageInterface $package, $path)
|
||||
{
|
||||
return rtrim($path.'/'.md5($path.spl_object_hash($package)).'.'.pathinfo($package->getDistUrl(), PATHINFO_EXTENSION), '.');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function processUrl($url)
|
||||
{
|
||||
if (!extension_loaded('openssl') && (0 === strpos($url, 'https:') || 0 === strpos($url, 'http://github.com'))) {
|
||||
// bypass https for github if openssl is disabled
|
||||
if (preg_match('{^https?://(github.com/[^/]+/[^/]+/(zip|tar)ball/[^/]+)$}i', $url, $match)) {
|
||||
$url = 'http://nodeload.'.$match[1];
|
||||
} else {
|
||||
throw new \RuntimeException('You must enable the openssl extension to download files via https');
|
||||
}
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract file to directory
|
||||
*
|
||||
* @param string $file Extracted file
|
||||
* @param string $path Directory
|
||||
*
|
||||
* @throws \UnexpectedValueException If can not extract downloaded file to path
|
||||
*/
|
||||
abstract protected function extract($file, $path);
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
<?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\Util\Filesystem;
|
||||
|
||||
class ArchiveDownloaderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testGetFileName()
|
||||
{
|
||||
$packageMock = $this->getMock('Composer\Package\PackageInterface');
|
||||
$packageMock->expects($this->any())
|
||||
->method('getDistUrl')
|
||||
->will($this->returnValue('http://example.com/script.js'))
|
||||
;
|
||||
|
||||
$downloader = $this->getMockForAbstractClass('Composer\Downloader\ArchiveDownloader', array($this->getMock('Composer\IO\IOInterface')));
|
||||
$method = new \ReflectionMethod($downloader, 'getFileName');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$first = $method->invoke($downloader, $packageMock, '/path');
|
||||
$this->assertRegExp('#/path/[a-z0-9]+\.js#', $first);
|
||||
$this->assertSame($first, $method->invoke($downloader, $packageMock, '/path'));
|
||||
}
|
||||
}
|
@ -0,0 +1,145 @@
|
||||
<?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;
|
||||
use Composer\Util\Filesystem;
|
||||
|
||||
class FileDownloaderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testDownloadForPackageWithoutDistReference()
|
||||
{
|
||||
$packageMock = $this->getMock('Composer\Package\PackageInterface');
|
||||
$packageMock->expects($this->once())
|
||||
->method('getDistUrl')
|
||||
->will($this->returnValue(null))
|
||||
;
|
||||
|
||||
$downloader = new FileDownloader($this->getMock('Composer\IO\IOInterface'));
|
||||
$downloader->download($packageMock, '/path');
|
||||
}
|
||||
|
||||
public function testDownloadToExistFile()
|
||||
{
|
||||
$packageMock = $this->getMock('Composer\Package\PackageInterface');
|
||||
$packageMock->expects($this->once())
|
||||
->method('getDistUrl')
|
||||
->will($this->returnValue('url'))
|
||||
;
|
||||
|
||||
$path = tempnam(sys_get_temp_dir(), 'c');
|
||||
|
||||
$downloader = new FileDownloader($this->getMock('Composer\IO\IOInterface'));
|
||||
try {
|
||||
$downloader->download($packageMock, $path);
|
||||
$this->fail();
|
||||
} catch (\Exception $e) {
|
||||
if (file_exists($path)) {
|
||||
unset($path);
|
||||
}
|
||||
$this->assertInstanceOf('UnexpectedValueException', $e);
|
||||
$this->assertContains('exists and is not a directory', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testGetFileName()
|
||||
{
|
||||
$packageMock = $this->getMock('Composer\Package\PackageInterface');
|
||||
$packageMock->expects($this->once())
|
||||
->method('getDistUrl')
|
||||
->will($this->returnValue('http://example.com/script.js'))
|
||||
;
|
||||
|
||||
$downloader = new FileDownloader($this->getMock('Composer\IO\IOInterface'));
|
||||
$method = new \ReflectionMethod($downloader, 'getFileName');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$this->assertEquals('/path/script.js', $method->invoke($downloader, $packageMock, '/path'));
|
||||
}
|
||||
|
||||
public function testDownloadButFileIsUnsaved()
|
||||
{
|
||||
$packageMock = $this->getMock('Composer\Package\PackageInterface');
|
||||
$packageMock->expects($this->any())
|
||||
->method('getDistUrl')
|
||||
->will($this->returnValue('http://example.com/script.js'))
|
||||
;
|
||||
|
||||
do {
|
||||
$path = sys_get_temp_dir().'/'.md5(time().rand());
|
||||
} while (file_exists($path));
|
||||
|
||||
$ioMock = $this->getMock('Composer\IO\IOInterface');
|
||||
$ioMock->expects($this->any())
|
||||
->method('write')
|
||||
->will($this->returnCallback(function($messages, $newline = true) use ($path) {
|
||||
if (is_file($path.'/script.js')) {
|
||||
unlink($path.'/script.js');
|
||||
}
|
||||
return $messages;
|
||||
}))
|
||||
;
|
||||
|
||||
$downloader = new FileDownloader($ioMock);
|
||||
try {
|
||||
$downloader->download($packageMock, $path);
|
||||
$this->fail();
|
||||
} catch (\Exception $e) {
|
||||
if (is_dir($path)) {
|
||||
$fs = new Filesystem();
|
||||
$fs->removeDirectory($path);
|
||||
} else if (is_file($path)) {
|
||||
unset($path);
|
||||
}
|
||||
|
||||
$this->assertInstanceOf('UnexpectedValueException', $e);
|
||||
$this->assertContains('could not be saved to', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testDownloadFileWithInvalidChecksum()
|
||||
{
|
||||
$packageMock = $this->getMock('Composer\Package\PackageInterface');
|
||||
$packageMock->expects($this->any())
|
||||
->method('getDistUrl')
|
||||
->will($this->returnValue('http://example.com/script.js'))
|
||||
;
|
||||
$packageMock->expects($this->any())
|
||||
->method('getDistSha1Checksum')
|
||||
->will($this->returnValue('invalid'))
|
||||
;
|
||||
|
||||
do {
|
||||
$path = sys_get_temp_dir().'/'.md5(time().rand());
|
||||
} while (file_exists($path));
|
||||
|
||||
$downloader = new FileDownloader($this->getMock('Composer\IO\IOInterface'));
|
||||
try {
|
||||
$downloader->download($packageMock, $path);
|
||||
$this->fail();
|
||||
} catch (\Exception $e) {
|
||||
if (is_dir($path)) {
|
||||
$fs = new Filesystem();
|
||||
$fs->removeDirectory($path);
|
||||
} else if (is_file($path)) {
|
||||
unset($path);
|
||||
}
|
||||
|
||||
$this->assertInstanceOf('UnexpectedValueException', $e);
|
||||
$this->assertContains('checksum verification', $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,186 @@
|
||||
<?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\Util;
|
||||
|
||||
use Composer\Util\RemoteFilesystem;
|
||||
use Composer\Test\TestCase;
|
||||
|
||||
class RemoteFilesystemTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testGetOptionsForUrl()
|
||||
{
|
||||
$io = $this->getMock('Composer\IO\IOInterface');
|
||||
$io
|
||||
->expects($this->once())
|
||||
->method('hasAuthorization')
|
||||
->will($this->returnValue(false))
|
||||
;
|
||||
$io
|
||||
->expects($this->once())
|
||||
->method('getLastUsername')
|
||||
->will($this->returnValue(null))
|
||||
;
|
||||
|
||||
$this->assertEquals(array(), $this->callGetOptionsForUrl($io, array('http://example.org')));
|
||||
}
|
||||
|
||||
public function testGetOptionsForUrlWithAuthorization()
|
||||
{
|
||||
$io = $this->getMock('Composer\IO\IOInterface');
|
||||
$io
|
||||
->expects($this->once())
|
||||
->method('hasAuthorization')
|
||||
->will($this->returnValue(true))
|
||||
;
|
||||
$io
|
||||
->expects($this->once())
|
||||
->method('getAuthorization')
|
||||
->will($this->returnValue(array('username' => 'login', 'password' => 'password')))
|
||||
;
|
||||
|
||||
$options = $this->callGetOptionsForUrl($io, array('http://example.org'));
|
||||
$this->assertContains('Authorization: Basic', $options['http']['header']);
|
||||
}
|
||||
|
||||
public function testGetOptionsForUrlWithLastUsername()
|
||||
{
|
||||
$io = $this->getMock('Composer\IO\IOInterface');
|
||||
$io
|
||||
->expects($this->once())
|
||||
->method('hasAuthorization')
|
||||
->will($this->returnValue(false))
|
||||
;
|
||||
$io
|
||||
->expects($this->any())
|
||||
->method('getLastUsername')
|
||||
->will($this->returnValue('login'))
|
||||
;
|
||||
$io
|
||||
->expects($this->any())
|
||||
->method('getLastPassword')
|
||||
->will($this->returnValue('password'))
|
||||
;
|
||||
$io
|
||||
->expects($this->once())
|
||||
->method('setAuthorization')
|
||||
;
|
||||
|
||||
$options = $this->callGetOptionsForUrl($io, array('http://example.org'));
|
||||
$this->assertContains('Authorization: Basic', $options['http']['header']);
|
||||
}
|
||||
|
||||
public function testCallbackGetFileSize()
|
||||
{
|
||||
$fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
|
||||
$this->callCallbackGet($fs, STREAM_NOTIFY_FILE_SIZE_IS, 0, '', 0, 0, 20);
|
||||
$this->assertAttributeEquals(20, 'bytesMax', $fs);
|
||||
}
|
||||
|
||||
public function testCallbackGetNotifyProgress()
|
||||
{
|
||||
$io = $this->getMock('Composer\IO\IOInterface');
|
||||
$io
|
||||
->expects($this->once())
|
||||
->method('overwrite')
|
||||
;
|
||||
|
||||
$fs = new RemoteFilesystem($io);
|
||||
$this->setAttribute($fs, 'bytesMax', 20);
|
||||
$this->setAttribute($fs, 'progress', true);
|
||||
|
||||
$this->callCallbackGet($fs, STREAM_NOTIFY_PROGRESS, 0, '', 0, 10, 20);
|
||||
$this->assertAttributeEquals(50, 'lastProgress', $fs);
|
||||
}
|
||||
|
||||
public function testCallbackGetNotifyFailure404()
|
||||
{
|
||||
$fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
|
||||
$this->setAttribute($fs, 'firstCall', false);
|
||||
|
||||
try {
|
||||
$this->callCallbackGet($fs, STREAM_NOTIFY_FAILURE, 0, '', 404, 0, 0);
|
||||
$this->fail();
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInstanceOf('RuntimeException', $e);
|
||||
$this->assertContains('URL not found', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function testCallbackGetNotifyFailure404FirstCall()
|
||||
{
|
||||
$io = $this->getMock('Composer\IO\IOInterface');
|
||||
$io
|
||||
->expects($this->once())
|
||||
->method('getAuthorization')
|
||||
->will($this->returnValue(array('username' => null)))
|
||||
;
|
||||
$io
|
||||
->expects($this->once())
|
||||
->method('isInteractive')
|
||||
->will($this->returnValue(false))
|
||||
;
|
||||
|
||||
$fs = new RemoteFilesystem($io);
|
||||
$this->setAttribute($fs, 'firstCall', true);
|
||||
|
||||
try {
|
||||
$this->callCallbackGet($fs, STREAM_NOTIFY_FAILURE, 0, '', 404, 0, 0);
|
||||
$this->fail();
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInstanceOf('RuntimeException', $e);
|
||||
$this->assertContains('URL required authentication', $e->getMessage());
|
||||
$this->assertAttributeEquals(false, 'firstCall', $fs);
|
||||
}
|
||||
}
|
||||
|
||||
public function testGetContents()
|
||||
{
|
||||
$fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
|
||||
|
||||
$this->assertContains('RFC 2606', $fs->getContents('http://example.org', 'http://example.org'));
|
||||
}
|
||||
|
||||
public function testCopy()
|
||||
{
|
||||
$fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
|
||||
|
||||
$file = tempnam(sys_get_temp_dir(), 'c');
|
||||
$this->assertTrue($fs->copy('http://example.org', 'http://example.org', $file));
|
||||
$this->assertFileExists($file);
|
||||
$this->assertContains('RFC 2606', file_get_contents($file));
|
||||
unlink($file);
|
||||
}
|
||||
|
||||
protected function callGetOptionsForUrl($io, array $args = array())
|
||||
{
|
||||
$fs = new RemoteFilesystem($io);
|
||||
$ref = new \ReflectionMethod($fs, 'getOptionsForUrl');
|
||||
$ref->setAccessible(true);
|
||||
|
||||
return $ref->invokeArgs($fs, $args);
|
||||
}
|
||||
|
||||
protected function callCallbackGet(RemoteFilesystem $fs, $notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
|
||||
{
|
||||
$ref = new \ReflectionMethod($fs, 'callbackGet');
|
||||
$ref->setAccessible(true);
|
||||
$ref->invoke($fs, $notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax);
|
||||
}
|
||||
|
||||
protected function setAttribute($object, $attribute, $value)
|
||||
{
|
||||
$attr = new \ReflectionProperty($object, $attribute);
|
||||
$attr->setAccessible(true);
|
||||
$attr->setValue($object, $value);
|
||||
}
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
<?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\Util;
|
||||
|
||||
use Composer\Util\StreamContextFactory;
|
||||
|
||||
class StreamContextFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected function setUp()
|
||||
{
|
||||
unset($_SERVER['HTTP_PROXY']);
|
||||
unset($_SERVER['http_proxy']);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
unset($_SERVER['HTTP_PROXY']);
|
||||
unset($_SERVER['http_proxy']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataGetContext
|
||||
*/
|
||||
public function testGetContext($expectedOptions, $defaultOptions, $expectedParams, $defaultParams)
|
||||
{
|
||||
$context = StreamContextFactory::getContext($defaultOptions, $defaultParams);
|
||||
$options = stream_context_get_options($context);
|
||||
$params = stream_context_get_params($context);
|
||||
|
||||
$this->assertEquals($expectedOptions, $options);
|
||||
$this->assertEquals($expectedParams, $params);
|
||||
}
|
||||
|
||||
public function dataGetContext()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
array(), array(),
|
||||
array('options' => array()), array()
|
||||
),
|
||||
array(
|
||||
$a = array('http' => array('method' => 'GET')), $a,
|
||||
array('options' => $a, 'notification' => $f = function() {}), array('notification' => $f)
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function testHttpProxy()
|
||||
{
|
||||
$_SERVER['HTTP_PROXY'] = 'http://username:password@proxyserver.net:port/';
|
||||
$_SERVER['http_proxy'] = 'http://proxyserver/';
|
||||
|
||||
$context = StreamContextFactory::getContext(array('http' => array('method' => 'GET')));
|
||||
$options = stream_context_get_options($context);
|
||||
|
||||
$this->assertSame('http://proxyserver/', $_SERVER['http_proxy']);
|
||||
|
||||
$this->assertEquals(array('http' => array(
|
||||
'proxy' => 'tcp://username:password@proxyserver.net:port/',
|
||||
'request_fulluri' => true,
|
||||
'method' => 'GET',
|
||||
)), $options);
|
||||
}
|
||||
|
||||
public function testSSLProxy()
|
||||
{
|
||||
$_SERVER['http_proxy'] = 'https://proxyserver/';
|
||||
|
||||
if (extension_loaded('openssl')) {
|
||||
$context = StreamContextFactory::getContext();
|
||||
$options = stream_context_get_options($context);
|
||||
|
||||
$this->assertSame(array('http' => array(
|
||||
'proxy' => 'ssl://proxyserver/',
|
||||
'request_fulluri' => true,
|
||||
)), $options);
|
||||
} else {
|
||||
try {
|
||||
StreamContextFactory::getContext();
|
||||
$this->fail();
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInstanceOf('RuntimeException', $e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue