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;
|
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;
|
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();
|
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);
|
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');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
;
|
|
|
|
|
|
|
|
$io = $this->getMock('Composer\IO\IOInterface');
|
2012-10-21 15:56:57 +00:00
|
|
|
$config = $this->getMock('Composer\Config');
|
2014-03-01 17:01:44 +00:00
|
|
|
$config->expects($this->at(0))
|
|
|
|
->method('get')
|
|
|
|
->with('disable-tls')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
$config->expects($this->at(1))
|
|
|
|
->method('get')
|
|
|
|
->with('cafile')
|
|
|
|
->will($this->returnValue(null));
|
|
|
|
$config->expects($this->at(2))
|
2016-01-21 09:22:12 +00:00
|
|
|
->method('get')
|
|
|
|
->with('capath')
|
|
|
|
->will($this->returnValue(null));
|
|
|
|
$config->expects($this->at(3))
|
2013-06-19 08:05:21 +00:00
|
|
|
->method('get')
|
|
|
|
->with('vendor-dir')
|
2015-12-14 15:50:04 +00:00
|
|
|
->will($this->returnValue($this->testDir));
|
2012-10-21 15:56:57 +00:00
|
|
|
$downloader = new ZipDownloader($io, $config);
|
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');
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
|
|
$this->assertContains('is not a zip archive', $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
2017-02-13 14:54:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Exception
|
|
|
|
* @expectedExceptionMessage ZipArchive Failed
|
|
|
|
*/
|
|
|
|
function testZipArchiveOnlyFailed() {
|
|
|
|
$downloader = new TestDownloader($this->getMock('Composer\IO\IOInterface'));
|
|
|
|
$e = new \Exception("ZipArchive Failed");
|
|
|
|
$downloader->setUp(TRUE, FALSE, $e, NULL);
|
|
|
|
$downloader->extract('testfile.zip', 'vendor/dir');
|
|
|
|
}
|
|
|
|
|
|
|
|
function testZipArchiveOnlyGood() {
|
|
|
|
$downloader = new TestDownloader($this->getMock('Composer\IO\IOInterface'));
|
|
|
|
$downloader->setUp(TRUE, FALSE, TRUE, NULL);
|
|
|
|
$downloader->extract('testfile.zip', 'vendor/dir');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Exception
|
|
|
|
* @expectedExceptionMessage SystemUnzip Failed
|
|
|
|
*/
|
|
|
|
function testSystemUnzipOnlyFailed() {
|
|
|
|
$downloader = new TestDownloader($this->getMock('Composer\IO\IOInterface'));
|
|
|
|
$e = new \Exception("SystemUnzip Failed");
|
|
|
|
$downloader->setUp(FALSE, TRUE, NULL, $e);
|
|
|
|
$downloader->extract('testfile.zip', 'vendor/dir');
|
|
|
|
}
|
|
|
|
|
|
|
|
function testSystemUnzipOnlyGood() {
|
|
|
|
$downloader = new TestDownloader($this->getMock('Composer\IO\IOInterface'));
|
|
|
|
$downloader->setUp(FALSE, TRUE, NULL, TRUE);
|
|
|
|
$downloader->extract('testfile.zip', 'vendor/dir');
|
|
|
|
}
|
|
|
|
|
|
|
|
function testSystemUnzipFallbackGood() {
|
|
|
|
$downloader = new TestDownloader($this->getMock('Composer\IO\IOInterface'));
|
|
|
|
$e = new \Exception("test");
|
|
|
|
$downloader->setUp(TRUE, TRUE, $e, TRUE);
|
|
|
|
$downloader->extract('testfile.zip', 'vendor/dir');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Exception
|
|
|
|
* @expectedExceptionMessage ZipArchive Failed
|
|
|
|
*/
|
|
|
|
function testSystemUnzipFallbackFailed() {
|
|
|
|
$downloader = new TestDownloader($this->getMock('Composer\IO\IOInterface'));
|
|
|
|
$e1 = new \Exception("ZipArchive Failed");
|
|
|
|
$e2 = new \Exception("SystemUnzip Failed");
|
|
|
|
$downloader->setUp(TRUE, TRUE, $e1, $e2);
|
|
|
|
$downloader->extract('testfile.zip', 'vendor/dir');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class TestDownloader extends ZipDownloader {
|
|
|
|
public function __construct($io)
|
|
|
|
{
|
|
|
|
$this->io = $io;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function extract($file, $path) {
|
|
|
|
parent::extract($file, $path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUp($zipArchive, $systemUnzip, $zipArchiveResponse, $systemUnzipResponse) {
|
|
|
|
self::$hasZipArchive = $zipArchive;
|
|
|
|
self::$hasSystemUnzip = $systemUnzip;
|
|
|
|
$this->zipArchiveResponse = $zipArchiveResponse;
|
|
|
|
$this->systemUnzipResponse = $systemUnzipResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function extractWithZipArchive($file, $path) {
|
|
|
|
return $this->zipArchiveResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function extractWithSystemUnzip($file, $path, $fallback) {
|
|
|
|
return $this->systemUnzipResponse;
|
|
|
|
}
|
2012-03-29 13:34:57 +00:00
|
|
|
}
|