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:20:44 +00:00
|
|
|
use Composer\Util\Platform;
|
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
|
|
|
|
{
|
2020-11-12 11:04:48 +00:00
|
|
|
/**
|
|
|
|
* @param string $include
|
|
|
|
*
|
|
|
|
* @dataProvider provideGitignoreExcludeNegationTestCases
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGitignoreExcludeNegation($include): void
|
2020-11-12 11:04:48 +00:00
|
|
|
{
|
|
|
|
$this->testZipArchive(array(
|
|
|
|
'docs/README.md' => '# The doc',
|
|
|
|
'.gitignore' => "/*\n.*\n!.git*\n$include",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2022-02-21 12:42:28 +00:00
|
|
|
public function provideGitignoreExcludeNegationTestCases(): array
|
2020-11-12 11:04:48 +00:00
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('!/docs'),
|
|
|
|
array('!/docs/'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-01 20:44:12 +00:00
|
|
|
/**
|
|
|
|
* @param array<string, string> $files
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testZipArchive(array $files = array()): void
|
2014-01-13 16:14:12 +00:00
|
|
|
{
|
2016-02-17 09:06:10 +00:00
|
|
|
if (!class_exists('ZipArchive')) {
|
|
|
|
$this->markTestSkipped('Cannot run ZipArchiverTest, missing class "ZipArchive".');
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:04:48 +00:00
|
|
|
if (empty($files)) {
|
|
|
|
$files = array(
|
2021-02-25 12:46:52 +00:00
|
|
|
'file.txt' => null,
|
|
|
|
'foo/bar/baz' => null,
|
|
|
|
'x/baz' => null,
|
|
|
|
'x/includeme' => null,
|
2020-11-12 11:04:48 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!Platform::isWindows()) {
|
2021-02-25 12:46:52 +00:00
|
|
|
$files['foo' . getcwd() . '/file.txt'] = null;
|
2020-11-12 11:04:48 +00:00
|
|
|
}
|
2020-11-03 10:20:44 +00:00
|
|
|
}
|
2014-01-13 16:14:12 +00:00
|
|
|
// Set up repository
|
2020-11-03 10:04:02 +00:00
|
|
|
$this->setupDummyRepo($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');
|
2020-11-12 11:04:48 +00:00
|
|
|
static::assertFileExists($target);
|
2020-11-03 10:01:48 +00:00
|
|
|
$zip = new ZipArchive();
|
|
|
|
$res = $zip->open($target);
|
2020-11-12 11:04:48 +00:00
|
|
|
static::assertTrue($res, 'Failed asserting that Zip file can be opened');
|
|
|
|
foreach ($files as $path => $content) {
|
|
|
|
static::assertSame($content, $zip->getFromName($path), 'Failed asserting that Zip contains ' . $path);
|
2020-11-03 10:01:48 +00:00
|
|
|
}
|
2020-11-03 10:27:15 +00:00
|
|
|
$zip->close();
|
2014-01-13 16:14:12 +00:00
|
|
|
|
|
|
|
unlink($target);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a local dummy repository to run tests against!
|
2021-11-01 20:44:12 +00:00
|
|
|
*
|
|
|
|
* @param array<string, string|null> $files
|
|
|
|
*
|
|
|
|
* @return void
|
2014-01-13 16:14:12 +00:00
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
protected function setupDummyRepo(array &$files): void
|
2014-01-13 16:14:12 +00:00
|
|
|
{
|
|
|
|
$currentWorkDir = getcwd();
|
|
|
|
chdir($this->testDir);
|
2020-11-12 11:04:48 +00:00
|
|
|
foreach ($files as $path => $content) {
|
2021-02-25 12:46:52 +00:00
|
|
|
if ($files[$path] === null) {
|
2020-11-12 11:04:48 +00:00
|
|
|
$files[$path] = 'content';
|
|
|
|
}
|
|
|
|
$this->writeFile($path, $files[$path], $currentWorkDir);
|
2020-11-03 10:01:48 +00:00
|
|
|
}
|
2014-01-13 16:14:12 +00:00
|
|
|
|
|
|
|
chdir($currentWorkDir);
|
|
|
|
}
|
|
|
|
|
2021-11-01 20:44:12 +00:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param string $content
|
|
|
|
* @param string $currentWorkDir
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
protected function writeFile($path, $content, $currentWorkDir): void
|
2014-01-13 16:14:12 +00:00
|
|
|
{
|
|
|
|
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.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|