2014-01-13 16:14:12 +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\Package\Archiver;
|
|
|
|
|
2020-11-03 10:01:48 +00:00
|
|
|
use ZipArchive;
|
2014-01-13 16:14:12 +00:00
|
|
|
use Composer\Package\Archiver\ZipArchiver;
|
|
|
|
|
|
|
|
class ZipArchiverTest extends ArchiverTest
|
|
|
|
{
|
|
|
|
public function testZipArchive()
|
|
|
|
{
|
2016-02-17 09:06:10 +00:00
|
|
|
if (!class_exists('ZipArchive')) {
|
|
|
|
$this->markTestSkipped('Cannot run ZipArchiverTest, missing class "ZipArchive".');
|
|
|
|
}
|
|
|
|
|
2020-11-03 10:01:48 +00:00
|
|
|
$files = array(
|
|
|
|
'file.txt',
|
|
|
|
'foo/bar/baz',
|
|
|
|
'x/baz',
|
|
|
|
'x/includeme',
|
|
|
|
'foo' . getcwd() . '/file.txt',
|
|
|
|
);
|
2014-01-13 16:14:12 +00:00
|
|
|
// Set up repository
|
2020-11-03 10:01:48 +00:00
|
|
|
$this->setupDummyRepo(array_merge($files));
|
2014-01-13 16:14:12 +00:00
|
|
|
$package = $this->setupPackage();
|
2017-03-08 14:07:29 +00:00
|
|
|
$target = sys_get_temp_dir().'/composer_archiver_test.zip';
|
2014-01-13 16:14:12 +00:00
|
|
|
|
|
|
|
// Test archive
|
|
|
|
$archiver = new ZipArchiver();
|
|
|
|
$archiver->archive($package->getSourceUrl(), $target, 'zip');
|
|
|
|
$this->assertFileExists($target);
|
2020-11-03 10:01:48 +00:00
|
|
|
$zip = new ZipArchive();
|
|
|
|
$res = $zip->open($target);
|
|
|
|
self::assertTrue($res, 'Failed asserting that Zip file can be opened');
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$this->assertSame('content', $zip->getFromName($file), 'Failed asserting that Zip contains ' . $file);
|
|
|
|
}
|
2014-01-13 16:14:12 +00:00
|
|
|
|
|
|
|
unlink($target);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a local dummy repository to run tests against!
|
2020-11-03 10:01:48 +00:00
|
|
|
* @param array $files
|
2014-01-13 16:14:12 +00:00
|
|
|
*/
|
2020-11-03 10:01:48 +00:00
|
|
|
protected function setupDummyRepo($files)
|
2014-01-13 16:14:12 +00:00
|
|
|
{
|
|
|
|
$currentWorkDir = getcwd();
|
|
|
|
chdir($this->testDir);
|
2020-11-03 10:01:48 +00:00
|
|
|
foreach ($files as $file) {
|
|
|
|
$this->writeFile($file, 'content', $currentWorkDir);
|
|
|
|
}
|
2014-01-13 16:14:12 +00:00
|
|
|
|
|
|
|
chdir($currentWorkDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function writeFile($path, $content, $currentWorkDir)
|
|
|
|
{
|
|
|
|
if (!file_exists(dirname($path))) {
|
|
|
|
mkdir(dirname($path), 0777, true);
|
|
|
|
}
|
|
|
|
|
2020-11-03 10:01:48 +00:00
|
|
|
$result = file_put_contents($path, $content);
|
2014-01-13 16:14:12 +00:00
|
|
|
if (false === $result) {
|
|
|
|
chdir($currentWorkDir);
|
|
|
|
throw new \RuntimeException('Could not save file.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|