123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?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;
- use Composer\TestCase;
- use Composer\Util\Filesystem;
- class ZipDownloaderTest extends TestCase
- {
- /**
- * @var string
- */
- private $testDir;
- public function setUp()
- {
- $this->testDir = $this->getUniqueTmpDirectory();
- }
- public function tearDown()
- {
- $fs = new Filesystem;
- $fs->removeDirectory($this->testDir);
- }
- public function testErrorMessages()
- {
- if (!class_exists('ZipArchive')) {
- $this->markTestSkipped('zip extension missing');
- }
- $packageMock = $this->getMock('Composer\Package\PackageInterface');
- $packageMock->expects($this->any())
- ->method('getDistUrl')
- ->will($this->returnValue($distUrl = 'file://'.__FILE__))
- ;
- $packageMock->expects($this->any())
- ->method('getDistUrls')
- ->will($this->returnValue(array($distUrl)))
- ;
- $packageMock->expects($this->atLeastOnce())
- ->method('getTransportOptions')
- ->will($this->returnValue(array()))
- ;
- $io = $this->getMock('Composer\IO\IOInterface');
- $config = $this->getMock('Composer\Config');
- $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))
- ->method('get')
- ->with('capath')
- ->will($this->returnValue(null));
- $config->expects($this->at(3))
- ->method('get')
- ->with('vendor-dir')
- ->will($this->returnValue($this->testDir));
- $downloader = new ZipDownloader($io, $config);
- 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());
- }
- }
- /**
- * @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() {
- $this->setExpectedException(\Exception::class);
- $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() {
- $this->setExpectedException(\Exception::class);
- $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;
- }
- }
|