2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2012-08-23 19:35:17 +00:00
|
|
|
|
2012-08-24 08:57:38 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-08-27 07:33:04 +00:00
|
|
|
namespace Composer\Test\Package\Archiver;
|
2012-08-24 08:57:38 +00:00
|
|
|
|
2012-08-28 20:32:26 +00:00
|
|
|
use Composer\Package\Archiver\PharArchiver;
|
2022-02-22 15:47:09 +00:00
|
|
|
use Composer\Util\Platform;
|
2012-08-24 08:57:38 +00:00
|
|
|
|
2023-07-21 08:39:20 +00:00
|
|
|
class PharArchiverTest extends ArchiverTestCase
|
2012-08-24 08:57:38 +00:00
|
|
|
{
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testTarArchive(): void
|
2012-08-24 08:57:38 +00:00
|
|
|
{
|
2012-10-21 16:23:35 +00:00
|
|
|
// Set up repository
|
|
|
|
$this->setupDummyRepo();
|
2012-08-24 08:57:38 +00:00
|
|
|
$package = $this->setupPackage();
|
2022-05-11 14:05:35 +00:00
|
|
|
$target = self::getUniqueTmpDirectory().'/composer_archiver_test.tar';
|
2012-08-24 08:57:38 +00:00
|
|
|
|
2012-08-23 19:35:17 +00:00
|
|
|
// Test archive
|
2012-08-28 20:32:26 +00:00
|
|
|
$archiver = new PharArchiver();
|
2022-08-17 12:20:07 +00:00
|
|
|
$archiver->archive($package->getSourceUrl(), $target, 'tar', ['foo/bar', 'baz', '!/foo/bar/baz']);
|
2012-08-23 19:35:17 +00:00
|
|
|
$this->assertFileExists($target);
|
2012-08-24 08:57:38 +00:00
|
|
|
|
2016-01-21 12:01:55 +00:00
|
|
|
$this->filesystem->removeDirectory(dirname($target));
|
2012-08-24 08:57:38 +00:00
|
|
|
}
|
2012-08-28 20:32:26 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testZipArchive(): void
|
2012-08-28 20:32:26 +00:00
|
|
|
{
|
2012-10-21 16:23:35 +00:00
|
|
|
// Set up repository
|
|
|
|
$this->setupDummyRepo();
|
2012-08-28 20:32:26 +00:00
|
|
|
$package = $this->setupPackage();
|
2022-05-11 14:05:35 +00:00
|
|
|
$target = self::getUniqueTmpDirectory().'/composer_archiver_test.zip';
|
2012-08-28 20:32:26 +00:00
|
|
|
|
|
|
|
// Test archive
|
|
|
|
$archiver = new PharArchiver();
|
|
|
|
$archiver->archive($package->getSourceUrl(), $target, 'zip');
|
|
|
|
$this->assertFileExists($target);
|
|
|
|
|
2016-01-21 12:01:55 +00:00
|
|
|
$this->filesystem->removeDirectory(dirname($target));
|
2012-10-21 16:23:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a local dummy repository to run tests against!
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
protected function setupDummyRepo(): void
|
2012-10-21 16:23:35 +00:00
|
|
|
{
|
2022-02-22 15:47:09 +00:00
|
|
|
$currentWorkDir = Platform::getCwd();
|
2012-10-21 16:23:35 +00:00
|
|
|
chdir($this->testDir);
|
|
|
|
|
2013-02-07 14:45:58 +00:00
|
|
|
$this->writeFile('file.txt', 'content', $currentWorkDir);
|
|
|
|
$this->writeFile('foo/bar/baz', 'content', $currentWorkDir);
|
|
|
|
$this->writeFile('foo/bar/ignoreme', 'content', $currentWorkDir);
|
|
|
|
$this->writeFile('x/baz', 'content', $currentWorkDir);
|
|
|
|
$this->writeFile('x/includeme', 'content', $currentWorkDir);
|
|
|
|
|
|
|
|
chdir($currentWorkDir);
|
|
|
|
}
|
|
|
|
|
2022-02-22 15:47:09 +00:00
|
|
|
protected function writeFile(string $path, string $content, string $currentWorkDir): void
|
2013-02-07 14:45:58 +00:00
|
|
|
{
|
|
|
|
if (!file_exists(dirname($path))) {
|
|
|
|
mkdir(dirname($path), 0777, true);
|
|
|
|
}
|
|
|
|
|
2021-11-01 20:44:12 +00:00
|
|
|
$result = file_put_contents($path, $content);
|
2012-10-21 16:23:35 +00:00
|
|
|
if (false === $result) {
|
|
|
|
chdir($currentWorkDir);
|
|
|
|
throw new \RuntimeException('Could not save file.');
|
|
|
|
}
|
2012-08-28 20:32:26 +00:00
|
|
|
}
|
2012-08-24 08:57:38 +00:00
|
|
|
}
|