1
0
Fork 0
composer/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php

147 lines
4.0 KiB
PHP
Raw Normal View History

2022-02-23 15:58:18 +00:00
<?php declare(strict_types=1);
/*
* 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;
use ZipArchive;
use Composer\Package\Archiver\ZipArchiver;
class ZipArchiverTest extends ArchiverTestCase
{
/** @var list<string> */
private $filesToCleanup = [];
public function testSimpleFiles(): void
{
$files = [
'file.txt' => null,
'foo/bar/baz' => null,
'x/baz' => null,
'x/includeme' => null,
];
if (!Platform::isWindows()) {
$files['zfoo' . Platform::getCwd() . '/file.txt'] = null;
}
$this->assertZipArchive($files);
}
/**
* @dataProvider provideGitignoreExcludeNegationTestCases
*/
2022-02-22 15:47:09 +00:00
public function testGitignoreExcludeNegation(string $include): void
{
$this->assertZipArchive([
'.gitignore' => "/*\n.*\n!.git*\n$include",
'docs/README.md' => '# The doc',
2022-08-17 12:20:07 +00:00
]);
}
public static function provideGitignoreExcludeNegationTestCases(): array
{
2022-08-17 12:20:07 +00:00
return [
['!/docs'],
['!/docs/'],
];
}
public function testFolderWithBackslashes(): void
{
if (Platform::isWindows()) {
$this->markTestSkipped('Folder names cannot contain backslashes on Windows.');
}
$this->testZipArchive([
'folder\with\backslashes/README.md' => '# doc',
]);
}
2021-11-01 20:44:12 +00:00
/**
* @param array<string, string|null> $files
2021-11-01 20:44:12 +00:00
*/
protected function assertZipArchive(array $files): void
{
2016-02-17 09:06:10 +00:00
if (!class_exists('ZipArchive')) {
$this->markTestSkipped('Cannot run ZipArchiverTest, missing class "ZipArchive".');
}
// Set up repository
$this->setupDummyRepo($files);
$package = $this->setupPackage();
$target = $this->filesToCleanup[] = sys_get_temp_dir().'/composer_archiver_test.zip';
// Test archive
$archiver = new ZipArchiver();
$archiver->archive($package->getSourceUrl(), $target, 'zip');
static::assertFileExists($target);
$zip = new ZipArchive();
$res = $zip->open($target);
static::assertTrue($res, 'Failed asserting that Zip file can be opened');
$zipContents = [];
for ($i = 0; $i < $zip->numFiles; $i++) {
$path = $zip->getNameIndex($i);
static::assertIsString($path);
$zipContents[$path] = $zip->getFromName($path);
}
2020-11-03 10:27:15 +00:00
$zip->close();
static::assertSame(
$files,
$zipContents,
'Failed asserting that Zip created with the ZipArchiver contains all files from the repository.'
);
}
/**
* Create a local dummy repository to run tests against!
2021-11-01 20:44:12 +00:00
*
* @param array<string, string|null> $files
*/
protected function setupDummyRepo(array &$files): void
{
2022-02-22 15:47:09 +00:00
$currentWorkDir = Platform::getCwd();
chdir($this->testDir);
foreach ($files as $path => $content) {
2021-02-25 12:46:52 +00:00
if ($files[$path] === null) {
$files[$path] = 'content';
}
$this->writeFile($path, $files[$path], $currentWorkDir);
}
chdir($currentWorkDir);
}
2022-02-22 15:47:09 +00:00
protected function writeFile(string $path, string $content, string $currentWorkDir): void
{
if (!file_exists(dirname($path))) {
mkdir(dirname($path), 0777, true);
}
$result = file_put_contents($path, $content);
if (false === $result) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not save file.');
}
}
protected function tearDown(): void
{
foreach ($this->filesToCleanup as $file) {
unlink($file);
}
parent::tearDown();
}
}