1
0
Fork 0
composer/tests/Composer/Test/Downloader/ZipDownloaderTest.php

352 lines
13 KiB
PHP
Raw Normal View History

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;
use Composer\Test\TestCase;
use Composer\Util\Filesystem;
use Composer\Util\HttpDownloader;
use Composer\Util\Loop;
2012-03-29 13:34:57 +00:00
class ZipDownloaderTest extends TestCase
2012-03-29 13:34:57 +00:00
{
/** @var string */
2015-12-14 15:50:04 +00:00
private $testDir;
/** @var \Composer\Util\HttpDownloader */
2019-01-07 15:22:41 +00:00
private $httpDownloader;
/** @var \Composer\IO\IOInterface&\PHPUnit\Framework\MockObject\MockObject */
private $io;
/** @var \Composer\Config&\PHPUnit\Framework\MockObject\MockObject */
private $config;
/** @var \Composer\Package\PackageInterface&\PHPUnit\Framework\MockObject\MockObject */
private $package;
2021-12-08 16:03:05 +00:00
public function setUp(): void
2012-03-29 13:34:57 +00:00
{
$this->testDir = $this->getUniqueTmpDirectory();
$this->io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$this->config = $this->getMockBuilder('Composer\Config')->getMock();
$dlConfig = $this->getMockBuilder('Composer\Config')->getMock();
$this->httpDownloader = new HttpDownloader($this->io, $dlConfig);
$this->package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
}
protected function tearDown(): void
{
parent::tearDown();
2015-12-14 15:50:04 +00:00
$fs = new Filesystem;
$fs->removeDirectory($this->testDir);
2017-03-30 07:24:48 +00:00
$this->setPrivateProperty('hasZipArchive', null);
2017-03-14 22:43:48 +00:00
}
/**
* @param string $name
* @param mixed $value
* @param ?\Composer\Test\Downloader\MockedZipDownloader $obj
* @return void
*/
2017-03-14 22:43:48 +00:00
public function setPrivateProperty($name, $value, $obj = null)
{
$reflectionClass = new \ReflectionClass('Composer\Downloader\ZipDownloader');
$reflectedProperty = $reflectionClass->getProperty($name);
$reflectedProperty->setAccessible(true);
if ($obj === null) {
$reflectedProperty->setValue($value);
2017-03-14 22:43:48 +00:00
} else {
$reflectedProperty->setValue($obj, $value);
2017-03-14 22:43:48 +00:00
}
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');
}
$this->config->expects($this->any())
2017-03-14 22:43:48 +00:00
->method('get')
->with('vendor-dir')
->will($this->returnValue($this->testDir));
$this->package->expects($this->any())
2012-03-29 13:34:57 +00:00
->method('getDistUrl')
->will($this->returnValue($distUrl = 'file://'.__FILE__))
;
$this->package->expects($this->any())
->method('getDistUrls')
->will($this->returnValue(array($distUrl)))
2012-03-29 13:34:57 +00:00
;
$this->package->expects($this->atLeastOnce())
->method('getTransportOptions')
->will($this->returnValue(array()))
2012-03-29 13:34:57 +00:00
;
$downloader = new ZipDownloader($this->io, $this->config, $this->httpDownloader);
2017-03-14 22:43:48 +00:00
2012-03-29 13:34:57 +00:00
try {
$loop = new Loop($this->httpDownloader);
$promise = $downloader->download($this->package, $path = sys_get_temp_dir().'/composer-zip-test');
$loop->wait(array($promise));
$downloader->install($this->package, $path);
2012-03-29 13:34:57 +00:00
$this->fail('Download of invalid zip files should throw an exception');
2017-03-14 22:43:48 +00:00
} catch (\Exception $e) {
$this->assertStringContainsString('is not a zip archive', $e->getMessage());
2012-03-29 13:34:57 +00:00
}
}
2017-02-13 14:54:55 +00:00
2017-03-14 22:43:48 +00:00
public function testZipArchiveOnlyFailed()
{
2021-12-09 19:55:26 +00:00
self::expectException('RuntimeException');
self::expectExceptionMessage('There was an error extracting the ZIP file');
if (!class_exists('ZipArchive')) {
$this->markTestSkipped('zip extension missing');
}
2017-03-30 07:24:48 +00:00
$this->setPrivateProperty('hasZipArchive', true);
$downloader = new MockedZipDownloader($this->io, $this->config, $this->httpDownloader);
2021-12-09 12:02:20 +00:00
$zipArchive = $this->getMockBuilder('ZipArchive')->getMock();
2021-12-08 21:06:17 +00:00
$zipArchive->expects($this->once())
2017-03-14 22:43:48 +00:00
->method('open')
->will($this->returnValue(true));
2021-12-08 21:06:17 +00:00
$zipArchive->expects($this->once())
2017-03-14 22:43:48 +00:00
->method('extractTo')
->will($this->returnValue(false));
$this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
2020-06-05 12:05:19 +00:00
$promise = $downloader->extract($this->package, 'testfile.zip', 'vendor/dir');
$this->wait($promise);
2017-02-13 14:54:55 +00:00
}
public function testZipArchiveExtractOnlyFailed()
{
2021-12-09 19:55:26 +00:00
self::expectException('RuntimeException');
self::expectExceptionMessage('The archive may contain identical file names with different capitalization (which fails on case insensitive filesystems): Not a directory');
if (!class_exists('ZipArchive')) {
$this->markTestSkipped('zip extension missing');
}
$this->setPrivateProperty('hasZipArchive', true);
$downloader = new MockedZipDownloader($this->io, $this->config, $this->httpDownloader);
$zipArchive = $this->getMockBuilder('ZipArchive')->getMock();
2021-12-09 12:02:20 +00:00
$zipArchive->expects($this->once())
->method('open')
->will($this->returnValue(true));
2021-12-09 12:02:20 +00:00
$zipArchive->expects($this->once())
->method('extractTo')
->will($this->throwException(new \ErrorException('Not a directory')));
$this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
2020-06-05 12:05:19 +00:00
$promise = $downloader->extract($this->package, 'testfile.zip', 'vendor/dir');
$this->wait($promise);
}
2017-03-14 22:43:48 +00:00
public function testZipArchiveOnlyGood()
{
if (!class_exists('ZipArchive')) {
$this->markTestSkipped('zip extension missing');
}
2017-03-30 07:24:48 +00:00
$this->setPrivateProperty('hasZipArchive', true);
$downloader = new MockedZipDownloader($this->io, $this->config, $this->httpDownloader);
$zipArchive = $this->getMockBuilder('ZipArchive')->getMock();
2021-12-09 12:02:20 +00:00
$zipArchive->expects($this->once())
2017-03-14 22:43:48 +00:00
->method('open')
->will($this->returnValue(true));
2021-12-09 12:02:20 +00:00
$zipArchive->expects($this->once())
2017-03-14 22:43:48 +00:00
->method('extractTo')
->will($this->returnValue(true));
$this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
2020-06-05 12:05:19 +00:00
$promise = $downloader->extract($this->package, 'testfile.zip', 'vendor/dir');
$this->wait($promise);
2017-02-13 14:54:55 +00:00
}
2017-03-14 22:43:48 +00:00
public function testSystemUnzipOnlyFailed()
{
2021-12-09 19:55:26 +00:00
self::expectException('Exception');
self::expectExceptionMessage('Failed to extract : (1) unzip');
$this->setPrivateProperty('isWindows', false);
2017-03-30 07:24:48 +00:00
$this->setPrivateProperty('hasZipArchive', false);
$this->setPrivateProperty('unzipCommands', array(array('unzip', 'unzip -qq %s -d %s')));
$procMock = $this->getMockBuilder('Symfony\Component\Process\Process')->disableOriginalConstructor()->getMock();
$procMock->expects($this->any())
->method('getExitCode')
->will($this->returnValue(1));
$procMock->expects($this->any())
->method('isSuccessful')
->will($this->returnValue(false));
$procMock->expects($this->any())
->method('getErrorOutput')
->will($this->returnValue('output'));
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
2021-12-09 12:02:20 +00:00
$processExecutor->expects($this->once())
->method('executeAsync')
->will($this->returnValue(\React\Promise\resolve($procMock)));
2017-03-14 22:43:48 +00:00
$downloader = new MockedZipDownloader($this->io, $this->config, $this->httpDownloader, null, null, null, $processExecutor);
$promise = $downloader->extract($this->package, 'testfile.zip', 'vendor/dir');
2020-06-05 12:05:19 +00:00
$this->wait($promise);
2017-02-13 14:54:55 +00:00
}
2017-03-14 22:43:48 +00:00
public function testSystemUnzipOnlyGood()
{
$this->setPrivateProperty('isWindows', false);
2017-03-30 07:24:48 +00:00
$this->setPrivateProperty('hasZipArchive', false);
$this->setPrivateProperty('unzipCommands', array(array('unzip', 'unzip -qq %s -d %s')));
$procMock = $this->getMockBuilder('Symfony\Component\Process\Process')->disableOriginalConstructor()->getMock();
$procMock->expects($this->any())
->method('getExitCode')
->will($this->returnValue(0));
$procMock->expects($this->any())
->method('isSuccessful')
->will($this->returnValue(true));
$procMock->expects($this->any())
->method('getErrorOutput')
->will($this->returnValue('output'));
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
2021-12-09 12:02:20 +00:00
$processExecutor->expects($this->once())
->method('executeAsync')
->will($this->returnValue(\React\Promise\resolve($procMock)));
2017-03-14 22:43:48 +00:00
$downloader = new MockedZipDownloader($this->io, $this->config, $this->httpDownloader, null, null, null, $processExecutor);
2020-06-05 12:05:19 +00:00
$promise = $downloader->extract($this->package, 'testfile.zip', 'vendor/dir');
$this->wait($promise);
2017-02-13 14:54:55 +00:00
}
2017-03-14 22:43:48 +00:00
public function testNonWindowsFallbackGood()
{
if (!class_exists('ZipArchive')) {
$this->markTestSkipped('zip extension missing');
}
2017-03-30 07:24:48 +00:00
$this->setPrivateProperty('isWindows', false);
$this->setPrivateProperty('hasZipArchive', true);
2017-03-14 22:43:48 +00:00
$procMock = $this->getMockBuilder('Symfony\Component\Process\Process')->disableOriginalConstructor()->getMock();
$procMock->expects($this->any())
->method('getExitCode')
->will($this->returnValue(1));
$procMock->expects($this->any())
->method('isSuccessful')
->will($this->returnValue(false));
$procMock->expects($this->any())
->method('getErrorOutput')
->will($this->returnValue('output'));
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
2021-12-09 12:02:20 +00:00
$processExecutor->expects($this->once())
->method('executeAsync')
->will($this->returnValue(\React\Promise\resolve($procMock)));
2017-03-14 22:43:48 +00:00
$zipArchive = $this->getMockBuilder('ZipArchive')->getMock();
2021-12-09 12:02:20 +00:00
$zipArchive->expects($this->once())
2017-03-14 22:43:48 +00:00
->method('open')
->will($this->returnValue(true));
2021-12-09 12:02:20 +00:00
$zipArchive->expects($this->once())
2017-03-14 22:43:48 +00:00
->method('extractTo')
->will($this->returnValue(true));
$downloader = new MockedZipDownloader($this->io, $this->config, $this->httpDownloader, null, null, null, $processExecutor);
2017-03-14 22:43:48 +00:00
$this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
2020-06-05 12:05:19 +00:00
$promise = $downloader->extract($this->package, 'testfile.zip', 'vendor/dir');
$this->wait($promise);
2017-02-13 14:54:55 +00:00
}
2017-03-14 22:43:48 +00:00
public function testNonWindowsFallbackFailed()
{
2021-12-09 19:55:26 +00:00
self::expectException('Exception');
self::expectExceptionMessage('There was an error extracting the ZIP file');
if (!class_exists('ZipArchive')) {
$this->markTestSkipped('zip extension missing');
}
2017-03-30 07:24:48 +00:00
$this->setPrivateProperty('isWindows', false);
$this->setPrivateProperty('hasZipArchive', true);
2017-03-14 22:43:48 +00:00
2020-06-05 12:05:19 +00:00
$procMock = $this->getMockBuilder('Symfony\Component\Process\Process')->disableOriginalConstructor()->getMock();
$procMock->expects($this->any())
->method('getExitCode')
->will($this->returnValue(1));
$procMock->expects($this->any())
->method('isSuccessful')
->will($this->returnValue(false));
$procMock->expects($this->any())
->method('getErrorOutput')
->will($this->returnValue('output'));
$processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
2021-12-09 12:02:20 +00:00
$processExecutor->expects($this->once())
2020-06-05 12:05:19 +00:00
->method('executeAsync')
->will($this->returnValue(\React\Promise\resolve($procMock)));
2017-03-14 22:43:48 +00:00
$zipArchive = $this->getMockBuilder('ZipArchive')->getMock();
2021-12-09 12:02:20 +00:00
$zipArchive->expects($this->once())
2017-03-14 22:43:48 +00:00
->method('open')
->will($this->returnValue(true));
2021-12-09 12:02:20 +00:00
$zipArchive->expects($this->once())
2017-03-14 22:43:48 +00:00
->method('extractTo')
->will($this->returnValue(false));
$downloader = new MockedZipDownloader($this->io, $this->config, $this->httpDownloader, null, null, null, $processExecutor);
2017-03-14 22:43:48 +00:00
$this->setPrivateProperty('zipArchiveObject', $zipArchive, $downloader);
2020-06-05 12:05:19 +00:00
$promise = $downloader->extract($this->package, 'testfile.zip', 'vendor/dir');
$this->wait($promise);
}
/**
* @param ?\React\Promise\PromiseInterface $promise
* @return void
*/
2020-06-05 12:05:19 +00:00
private function wait($promise)
{
if (null === $promise) {
return;
}
$e = null;
$promise->then(function () {
// noop
}, function ($ex) use (&$e) {
$e = $ex;
});
if ($e) {
throw $e;
}
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, PackageInterface $prevPackage = null, $output = true)
2017-03-14 22:43:48 +00:00
{
return \React\Promise\resolve();
2017-02-13 14:54:55 +00:00
}
public function install(PackageInterface $package, $path, $output = true)
{
return \React\Promise\resolve();
}
public function extract(PackageInterface $package, $file, $path)
2017-03-14 22:43:48 +00:00
{
return parent::extract($package, $file, $path);
2017-02-13 14:54:55 +00:00
}
2012-03-29 13:34:57 +00:00
}