2012-03-29 13:34:57 +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\ZipDownloader;
|
2017-03-14 22:43:48 +00:00
|
|
|
use Composer\Package\PackageInterface;
|
2016-01-21 12:01:55 +00:00
|
|
|
use Composer\TestCase;
|
2015-12-14 14:35:27 +00:00
|
|
|
use Composer\Util\Filesystem;
|
2012-03-29 13:34:57 +00:00
|
|
|
|
2016-01-21 12:01:55 +00:00
|
|
|
class ZipDownloaderTest extends TestCase
|
2012-03-29 13:34:57 +00:00
|
|
|
{
|
2015-12-14 14:35:27 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-12-14 15:50:04 +00:00
|
|
|
private $testDir;
|
2017-03-14 22:43:48 +00:00
|
|
|
private $prophet;
|
2015-12-14 14:35:27 +00:00
|
|
|
|
2012-03-29 13:34:57 +00:00
|
|
|
public function setUp()
|
|
|
|
{
|
2016-01-21 12:01:55 +00:00
|
|
|
$this->testDir = $this->getUniqueTmpDirectory();
|
2017-03-14 22:43:48 +00:00
|
|
|
$this->io = $this->getMock('Composer\IO\IOInterface');
|
|
|
|
$this->config = $this->getMock('Composer\Config');
|
2015-12-14 14:35:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 22:43:48 +00:00
|
|
|
|
2015-12-14 14:35:27 +00:00
|
|
|
public function tearDown()
|
|
|
|
{
|
2015-12-14 15:50:04 +00:00
|
|
|
$fs = new Filesystem;
|
|
|
|
$fs->removeDirectory($this->testDir);
|
2017-03-14 22:43:48 +00:00
|
|
|
ZipDownloader::$hasSystemUnzip = null;
|
|
|
|
ZipDownloader::$hasZipArchive = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPrivateProperty($name, $value, $obj = null)
|
|
|
|
{
|
|
|
|
$reflectionClass = new \ReflectionClass('Composer\Downloader\ZipDownloader');
|
|
|
|
$reflectedProperty = $reflectionClass->getProperty($name);
|
|
|
|
$reflectedProperty->setAccessible(true);
|
|
|
|
if ($obj === null) {
|
|
|
|
$reflectedProperty = $reflectedProperty->setValue($value);
|
|
|
|
} else {
|
|
|
|
$reflectedProperty = $reflectedProperty->setValue($obj, $value);
|
|
|
|
}
|
2012-03-29 13:34:57 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 22:43:48 +00:00
|
|
|
/**
|
|
|
|
* @group only
|
|
|
|
*/
|
2012-03-29 13:34:57 +00:00
|
|
|
public function testErrorMessages()
|
|
|
|
{
|
2017-02-13 14:54:55 +00:00
|
|
|
if (!class_exists('ZipArchive')) {
|
|
|
|
$this->markTestSkipped('zip extension missing');
|
|
|
|
}
|
|
|
|
|
2017-03-14 22:43:48 +00:00
|
|
|
$this->config->expects($this->at(0))
|
|
|
|
->method('get')
|
|
|
|
->with('disable-tls')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
$this->config->expects($this->at(1))
|
|
|
|
->method('get')
|
|
|
|
->with('cafile')
|
|
|
|
->will($this->returnValue(null));
|
|
|
|
$this->config->expects($this->at(2))
|
|
|
|
->method('get')
|
|
|
|
->with('capath')
|
|
|
|
->will($this->returnValue(null));
|
|
|
|
$this->config->expects($this->at(3))
|
|
|
|
->method('get')
|
|
|
|
->with('vendor-dir')
|
|
|
|
->will($this->returnValue($this->testDir));
|
|
|
|
|
2012-03-29 13:34:57 +00:00
|
|
|
$packageMock = $this->getMock('Composer\Package\PackageInterface');
|
|
|
|
$packageMock->expects($this->any())
|
|
|
|
->method('getDistUrl')
|
2013-07-01 23:45:43 +00:00
|
|
|
->will($this->returnValue($distUrl = 'file://'.__FILE__))
|
|
|
|
;
|
|
|
|
$packageMock->expects($this->any())
|
|
|
|
->method('getDistUrls')
|
|
|
|
->will($this->returnValue(array($distUrl)))
|
2012-03-29 13:34:57 +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-03-29 13:34:57 +00:00
|
|
|
;
|
|
|
|
|
2017-03-14 22:43:48 +00:00
|
|
|
$downloader = new ZipDownloader($this->io, $this->config);
|
|
|
|
|
|
|
|
ZipDownloader::$hasSystemUnzip = false;
|
2012-03-29 13:34:57 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$downloader->download($packageMock, sys_get_temp_dir().'/composer-zip-test');
|
|
|
|
$this->fail('Download of invalid zip files should throw an exception');
|
2017-03-14 22:43:48 +00:00
|
|
|
} catch (\Exception $e) {
|
2012-03-29 13:34:57 +00:00
|
|
|
$this->assertContains('is not a zip archive', $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
2017-02-13 14:54:55 +00:00
|
|
|
|
|
|
|
/**
|
2017-03-14 22:43:48 +00:00
|
|
|
* @expectedException \RuntimeException
|
|
|
|
* @expectedExceptionMessage There was an error extracting the ZIP file
|
2017-02-13 14:54:55 +00:00
|
|
|
*/
|
2017-03-14 22:43:48 +00:00
|
|
|
public function testZipArchiveOnlyFailed()
|
|
|
|
{
|
|
|
|
MockedZipDownloader::$hasSystemUnzip = false;
|
|
|
|
MockedZipDownloader::$hasZipArchive = true;
|
|
|
|
$downloader = new MockedZipDownloader($this->io, $this->config);
|
|
|
|
|
|
|
|
$zipArchive = $this->getMock('ZipArchive');
|
|
|
|
$zipArchive->expects($this->at(0))
|
|
|
|
->method('open')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$zipArchive->expects($this->at(1))
|
|
|
|
->method('extractTo')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
|
2017-02-13 14:54:55 +00:00
|
|
|
$downloader->extract('testfile.zip', 'vendor/dir');
|
|
|
|
}
|
|
|
|
|
2017-03-14 22:43:48 +00:00
|
|
|
/**
|
|
|
|
* @group only
|
|
|
|
*/
|
|
|
|
public function testZipArchiveOnlyGood()
|
|
|
|
{
|
|
|
|
MockedZipDownloader::$hasSystemUnzip = false;
|
|
|
|
MockedZipDownloader::$hasZipArchive = true;
|
|
|
|
$downloader = new MockedZipDownloader($this->io, $this->config);
|
|
|
|
|
|
|
|
$zipArchive = $this->getMock('ZipArchive');
|
|
|
|
$zipArchive->expects($this->at(0))
|
|
|
|
->method('open')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$zipArchive->expects($this->at(1))
|
|
|
|
->method('extractTo')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
|
2017-02-13 14:54:55 +00:00
|
|
|
$downloader->extract('testfile.zip', 'vendor/dir');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Exception
|
2017-03-14 22:43:48 +00:00
|
|
|
* @expectedExceptionMessage Failed to execute unzip
|
2017-02-13 14:54:55 +00:00
|
|
|
*/
|
2017-03-14 22:43:48 +00:00
|
|
|
public function testSystemUnzipOnlyFailed()
|
|
|
|
{
|
|
|
|
MockedZipDownloader::$hasSystemUnzip = true;
|
|
|
|
MockedZipDownloader::$hasZipArchive = false;
|
|
|
|
$processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
|
|
|
|
$processExecutor->expects($this->at(0))
|
|
|
|
->method('execute')
|
|
|
|
->will($this->returnValue(1));
|
|
|
|
|
|
|
|
$downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
|
2017-02-13 14:54:55 +00:00
|
|
|
$downloader->extract('testfile.zip', 'vendor/dir');
|
|
|
|
}
|
|
|
|
|
2017-03-14 22:43:48 +00:00
|
|
|
public function testSystemUnzipOnlyGood()
|
|
|
|
{
|
|
|
|
MockedZipDownloader::$hasSystemUnzip = true;
|
|
|
|
MockedZipDownloader::$hasZipArchive = false;
|
|
|
|
$processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
|
|
|
|
$processExecutor->expects($this->at(0))
|
|
|
|
->method('execute')
|
|
|
|
->will($this->returnValue(0));
|
|
|
|
|
|
|
|
$downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
|
2017-02-13 14:54:55 +00:00
|
|
|
$downloader->extract('testfile.zip', 'vendor/dir');
|
|
|
|
}
|
|
|
|
|
2017-03-14 22:43:48 +00:00
|
|
|
public function testNonWindowsFallbackGood()
|
|
|
|
{
|
|
|
|
MockedZipDownloader::$isWindows = false;
|
|
|
|
MockedZipDownloader::$hasSystemUnzip = true;
|
|
|
|
MockedZipDownloader::$hasZipArchive = true;
|
|
|
|
|
|
|
|
$processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
|
|
|
|
$processExecutor->expects($this->at(0))
|
|
|
|
->method('execute')
|
|
|
|
->will($this->returnValue(1));
|
|
|
|
|
|
|
|
$zipArchive = $this->getMock('ZipArchive');
|
|
|
|
$zipArchive->expects($this->at(0))
|
|
|
|
->method('open')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$zipArchive->expects($this->at(1))
|
|
|
|
->method('extractTo')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
|
|
|
|
$this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
|
2017-02-13 14:54:55 +00:00
|
|
|
$downloader->extract('testfile.zip', 'vendor/dir');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Exception
|
2017-03-14 22:43:48 +00:00
|
|
|
* @expectedExceptionMessage There was an error extracting the ZIP file
|
2017-02-13 14:54:55 +00:00
|
|
|
*/
|
2017-03-14 22:43:48 +00:00
|
|
|
public function testNonWindowsFallbackFailed()
|
|
|
|
{
|
|
|
|
MockedZipDownloader::$isWindows = false;
|
|
|
|
MockedZipDownloader::$hasSystemUnzip = true;
|
|
|
|
MockedZipDownloader::$hasZipArchive = true;
|
|
|
|
|
|
|
|
$processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
|
|
|
|
$processExecutor->expects($this->at(0))
|
|
|
|
->method('execute')
|
|
|
|
->will($this->returnValue(1));
|
|
|
|
|
|
|
|
$zipArchive = $this->getMock('ZipArchive');
|
|
|
|
$zipArchive->expects($this->at(0))
|
|
|
|
->method('open')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$zipArchive->expects($this->at(1))
|
|
|
|
->method('extractTo')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
|
|
|
|
$this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
|
2017-02-13 14:54:55 +00:00
|
|
|
$downloader->extract('testfile.zip', 'vendor/dir');
|
|
|
|
}
|
|
|
|
|
2017-03-14 22:43:48 +00:00
|
|
|
public function testWindowsFallbackGood()
|
2017-02-13 14:54:55 +00:00
|
|
|
{
|
2017-03-14 22:43:48 +00:00
|
|
|
MockedZipDownloader::$isWindows = true;
|
|
|
|
MockedZipDownloader::$hasSystemUnzip = true;
|
|
|
|
MockedZipDownloader::$hasZipArchive = true;
|
2017-02-13 14:54:55 +00:00
|
|
|
|
2017-03-14 22:43:48 +00:00
|
|
|
$processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
|
|
|
|
$processExecutor->expects($this->atLeastOnce())
|
|
|
|
->method('execute')
|
|
|
|
->will($this->returnValue(0));
|
|
|
|
|
|
|
|
$zipArchive = $this->getMock('ZipArchive');
|
|
|
|
$zipArchive->expects($this->at(0))
|
|
|
|
->method('open')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$zipArchive->expects($this->at(1))
|
|
|
|
->method('extractTo')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
|
|
|
|
$this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
|
|
|
|
$downloader->extract('testfile.zip', 'vendor/dir');
|
2017-02-13 14:54:55 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 22:43:48 +00:00
|
|
|
/**
|
|
|
|
* @expectedException \Exception
|
|
|
|
* @expectedExceptionMessage Failed to execute unzip
|
|
|
|
*/
|
|
|
|
public function testWindowsFallbackFailed()
|
|
|
|
{
|
|
|
|
MockedZipDownloader::$isWindows = true;
|
|
|
|
MockedZipDownloader::$hasSystemUnzip = true;
|
|
|
|
MockedZipDownloader::$hasZipArchive = true;
|
|
|
|
|
|
|
|
$processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
|
|
|
|
$processExecutor->expects($this->atLeastOnce())
|
|
|
|
->method('execute')
|
|
|
|
->will($this->returnValue(1));
|
|
|
|
|
|
|
|
$zipArchive = $this->getMock('ZipArchive');
|
|
|
|
$zipArchive->expects($this->at(0))
|
|
|
|
->method('open')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$zipArchive->expects($this->at(1))
|
|
|
|
->method('extractTo')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$downloader = new MockedZipDownloader($this->io, $this->config, null, null, $processExecutor);
|
|
|
|
$this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
|
|
|
|
$downloader->extract('testfile.zip', 'vendor/dir');
|
2017-02-13 14:54:55 +00:00
|
|
|
}
|
2017-03-14 22:43:48 +00:00
|
|
|
}
|
2017-02-13 14:54:55 +00:00
|
|
|
|
2017-03-14 22:43:48 +00:00
|
|
|
class MockedZipDownloader extends ZipDownloader
|
|
|
|
{
|
|
|
|
public function download(PackageInterface $package, $path, $output = true)
|
|
|
|
{
|
|
|
|
return;
|
2017-02-13 14:54:55 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 22:43:48 +00:00
|
|
|
public function extract($file, $path)
|
|
|
|
{
|
|
|
|
parent::extract($file, $path);
|
2017-02-13 14:54:55 +00:00
|
|
|
}
|
2012-03-29 13:34:57 +00:00
|
|
|
}
|