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;
|
|
|
|
use Composer\Util\Filesystem;
|
|
|
|
|
|
|
|
class FileDownloaderTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2013-11-24 09:55:25 +00:00
|
|
|
protected function getDownloader($io = null, $config = null, $eventDispatcher = null, $cache = null, $rfs = null, $filesystem = null)
|
2012-03-15 00:28:10 +00:00
|
|
|
{
|
|
|
|
$io = $io ?: $this->getMock('Composer\IO\IOInterface');
|
2012-10-21 15:56:57 +00:00
|
|
|
$config = $config ?: $this->getMock('Composer\Config');
|
2012-03-15 00:28:10 +00:00
|
|
|
$rfs = $rfs ?: $this->getMockBuilder('Composer\Util\RemoteFilesystem')->disableOriginalConstructor()->getMock();
|
2012-06-14 10:10:01 +00:00
|
|
|
|
2013-11-24 09:55:25 +00:00
|
|
|
return new FileDownloader($io, $config, $eventDispatcher, $cache, $rfs, $filesystem);
|
2012-03-15 00:28:10 +00:00
|
|
|
}
|
|
|
|
|
2012-02-28 10:59:18 +00:00
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testDownloadForPackageWithoutDistReference()
|
|
|
|
{
|
|
|
|
$packageMock = $this->getMock('Composer\Package\PackageInterface');
|
|
|
|
$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
|
|
|
{
|
|
|
|
$packageMock = $this->getMock('Composer\Package\PackageInterface');
|
|
|
|
$packageMock->expects($this->once())
|
|
|
|
->method('getDistUrl')
|
|
|
|
->will($this->returnValue('url'))
|
|
|
|
;
|
|
|
|
|
|
|
|
$path = tempnam(sys_get_temp_dir(), 'c');
|
|
|
|
|
2012-03-15 00:28:10 +00:00
|
|
|
$downloader = $this->getDownloader();
|
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()
|
|
|
|
{
|
|
|
|
$packageMock = $this->getMock('Composer\Package\PackageInterface');
|
|
|
|
$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);
|
|
|
|
|
|
|
|
$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'))
|
|
|
|
;
|
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
|
|
|
|
|
|
|
do {
|
2013-01-30 09:44:07 +00:00
|
|
|
$path = sys_get_temp_dir().'/'.md5(time().mt_rand());
|
2012-02-28 10:59:18 +00:00
|
|
|
} 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');
|
|
|
|
}
|
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);
|
2012-02-28 10:59:18 +00:00
|
|
|
try {
|
|
|
|
$downloader->download($packageMock, $path);
|
|
|
|
$this->fail();
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertInstanceOf('UnexpectedValueException', $e);
|
|
|
|
$this->assertContains('could not be saved to', $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-24 09:55:25 +00:00
|
|
|
public function testCacheGarbageCollectionIsCalled()
|
|
|
|
{
|
|
|
|
$expectedTtl = '99999999';
|
|
|
|
|
|
|
|
$configMock = $this->getMock('Composer\Config');
|
|
|
|
$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()
|
|
|
|
{
|
|
|
|
$packageMock = $this->getMock('Composer\Package\PackageInterface');
|
|
|
|
$packageMock->expects($this->any())
|
|
|
|
->method('getDistUrl')
|
|
|
|
->will($this->returnValue('http://example.com/script.js'))
|
|
|
|
;
|
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-08-31 13:55:12 +00:00
|
|
|
$filesystem = $this->getMock('Composer\Util\Filesystem');
|
2012-02-28 10:59:18 +00:00
|
|
|
|
|
|
|
do {
|
2013-01-30 09:44:07 +00:00
|
|
|
$path = sys_get_temp_dir().'/'.md5(time().mt_rand());
|
2012-02-28 10:59:18 +00:00
|
|
|
} while (file_exists($path));
|
|
|
|
|
2013-11-24 09:55:25 +00:00
|
|
|
$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
|
|
|
|
mkdir($path, 0777, true);
|
|
|
|
touch($path.'/script.js');
|
|
|
|
|
2012-02-28 10:59:18 +00:00
|
|
|
try {
|
|
|
|
$downloader->download($packageMock, $path);
|
|
|
|
$this->fail();
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertInstanceOf('UnexpectedValueException', $e);
|
|
|
|
$this->assertContains('checksum verification', $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|