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

142 lines
4.1 KiB
PHP
Raw Normal View History

2012-08-23 19:35:17 +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;
2012-08-28 21:55:26 +00:00
use Composer\Factory;
use Composer\Package\Archiver\ArchiveManager;
2012-08-23 19:35:17 +00:00
use Composer\Package\PackageInterface;
class ArchiveManagerTest extends ArchiverTest
{
/**
* @var ArchiveManager
*/
2012-08-23 19:35:17 +00:00
protected $manager;
protected $targetDir;
2012-08-23 19:35:17 +00:00
public function setUp()
{
parent::setUp();
2012-08-28 21:55:26 +00:00
$factory = new Factory();
$this->manager = $factory->createArchiveManager($factory->createConfig());
2012-10-21 16:23:35 +00:00
$this->targetDir = $this->testDir.'/composer_archiver_tests';
2012-08-23 19:35:17 +00:00
}
public function testUnknownFormat()
{
$this->setExpectedException('RuntimeException');
$package = $this->setupPackage();
$this->manager->archive($package, '__unknown_format__', $this->targetDir);
2012-08-23 19:35:17 +00:00
}
2012-10-21 16:23:35 +00:00
public function testArchiveTar()
2012-08-23 19:35:17 +00:00
{
$this->skipIfNotExecutable('git');
2012-08-23 19:35:17 +00:00
$this->setupGitRepo();
$package = $this->setupPackage();
$this->manager->archive($package, 'tar', $this->targetDir);
2012-08-23 19:35:17 +00:00
$target = $this->getTargetName($package, 'tar');
$this->assertFileExists($target);
2013-10-11 23:12:02 +00:00
$tmppath = sys_get_temp_dir().'/composer_archiver/'.$this->manager->getPackageFilename($package);
$this->assertFileNotExists($tmppath);
2012-08-23 19:35:17 +00:00
unlink($target);
}
public function testArchiveCustomFileName()
2012-08-23 19:35:17 +00:00
{
$this->skipIfNotExecutable('git');
$this->setupGitRepo();
$package = $this->setupPackage();
2015-10-07 17:42:19 +00:00
$fileName = 'testArchiveName';
$this->manager->archive($package, 'tar', $this->targetDir, $fileName);
2015-10-07 17:42:19 +00:00
$target = $this->targetDir . '/' . $fileName . '.tar';
$this->assertFileExists($target);
$tmppath = sys_get_temp_dir().'/composer_archiver/'.$this->manager->getPackageFilename($package);
$this->assertFileNotExists($tmppath);
unlink($target);
}
2015-10-25 15:19:15 +00:00
protected function getTargetName(PackageInterface $package, $format, $fileName = null)
{
2015-10-25 15:19:15 +00:00
if (null === $fileName) {
$packageName = $this->manager->getPackageFilename($package);
2015-10-05 01:03:06 +00:00
} else {
$packageName = $fileName;
}
2015-10-05 01:03:06 +00:00
$target = $this->targetDir.'/'.$packageName.'.'.$format;
2012-08-23 19:35:17 +00:00
return $target;
}
2012-10-21 16:23:35 +00:00
/**
* Create local git repository to run tests against!
*/
protected function setupGitRepo()
{
$currentWorkDir = getcwd();
chdir($this->testDir);
$output = null;
$result = $this->process->execute('git init -q', $output, $this->testDir);
2012-10-21 16:23:35 +00:00
if ($result > 0) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput());
}
$result = $this->process->execute('git config user.email "you@example.com"', $output, $this->testDir);
if ($result > 0) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not config: '.$this->process->getErrorOutput());
}
$result = $this->process->execute('git config user.name "Your Name"', $output, $this->testDir);
if ($result > 0) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not config: '.$this->process->getErrorOutput());
}
$result = file_put_contents('composer.json', '{"name":"faker/faker", "description": "description", "license": "MIT"}');
2012-10-21 16:23:35 +00:00
if (false === $result) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not save file.');
}
$result = $this->process->execute('git add composer.json && git commit -m "commit composer.json" -q', $output, $this->testDir);
2012-10-21 16:23:35 +00:00
if ($result > 0) {
chdir($currentWorkDir);
throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput());
}
chdir($currentWorkDir);
}
2012-08-23 19:35:17 +00:00
}