diff --git a/src/Composer/Package/Archiver/ArchiveManager.php b/src/Composer/Package/Archiver/ArchiveManager.php new file mode 100644 index 000000000..4bd0b615c --- /dev/null +++ b/src/Composer/Package/Archiver/ArchiveManager.php @@ -0,0 +1,113 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Package\Archiver; + +use Composer\Downloader\DownloadManager; +use Composer\Factory; +use Composer\IO\NullIO; +use Composer\Package\PackageInterface; +use Composer\Util\Filesystem; + +/** + * @author Matthieu Moquet + * @author Till Klampaeckel + */ +class ArchiveManager +{ + protected $buildDir; + + protected $downloadManager; + + protected $archivers = array(); + + protected $vcsArchivers = array(); + + /** + * @param string $buildDir The directory used to build the archive + * @param DownloadManager $downloadManager A manager used to download package sources + */ + public function __construct($buildDir, DownloadManager $downloadManager = null) + { + $this->buildDir = $buildDir; + + if (null !== $downloadManager) { + $this->downloadManager = $downloadManager; + } else { + $factory = new Factory(); + $this->downloadManager = $factory->createDownloadManager(new NullIO()); + } + } + + /** + * @param ArchiverInterface $archiver + */ + public function addArchiver(ArchiverInterface $archiver) + { + if ($archiver instanceof VcsArchiver) { + $this->vcsArchivers[$archiver->getSourceType()] = $archiver; + } else { + $this->archivers[] = $archiver; + } + } + + /** + * Create an archive of the specified package. + * + * @param PackageInterface $package The package to archive + * @param string $format The format of the archive (zip, tar, ...) + * + * @return string The path of the created archive + */ + public function archive(PackageInterface $package, $format) + { + if (empty($format)) { + throw new \InvalidArgumentException('Format must be specified'); + } + + $filesystem = new Filesystem(); + $packageName = str_replace('/', DIRECTORY_SEPARATOR, $package->getUniqueName()); + + // Directory used to download the sources + $sources = sys_get_temp_dir().DIRECTORY_SEPARATOR.$packageName; + $filesystem->ensureDirectoryExists($sources); + + // Archive filename + $target = $this->buildDir.DIRECTORY_SEPARATOR.$packageName.'.'.$format; + $filesystem->ensureDirectoryExists(dirname($this->buildDir.$target)); + + // Download sources + $this->downloadManager->download($package, $sources, true); + + // Try VCS archivers first + $sourceType = $package->getSourceType(); + if (isset($this->archivers[$sourceType]) && $this->archivers[$sourceType]->supports($format)) { + $archiver = $this->archivers[$sourceType]; + $archiver->setSourceRef($sourceRef); + $archiver->setFormat($format); + $archiver->archive($sources, $target); + + return $target; + } + + // Fallback on default archivers + foreach ($this->archivers as $archiver) { + if ($archiver->supports($format)) { + $archiver->archive($sources, $target); + + return $target; + } + } + + throw new \RuntimeException(sprintf('No archiver found to support %s format', $format)); + } +} diff --git a/src/Composer/Package/Archiver/ArchiverInterface.php b/src/Composer/Package/Archiver/ArchiverInterface.php index 20566140a..f1d597787 100644 --- a/src/Composer/Package/Archiver/ArchiverInterface.php +++ b/src/Composer/Package/Archiver/ArchiverInterface.php @@ -1,4 +1,5 @@ + * @author Matthieu Moquet */ interface ArchiverInterface { /** - * Return value depends on implementation - e.g. generating a tar or zip the - * method currently returns void, the ArrayArchiver returns an array. + * Create an archive from the sources. * - * @param PackageInterface $package - * - * @return void + * @param string $source The sources directory + * @param string $target The target file */ - public function dump(PackageInterface $package); + public function archive($sources, $target); + + /** + * Format supported by the archiver. + * + * @param string $format The format to support + * + * @return boolean true if the format is supported by the archiver + */ + public function supports($format); } diff --git a/src/Composer/Package/Archiver/BaseArchiver.php b/src/Composer/Package/Archiver/BaseArchiver.php index 7d9c26d80..8fadd7056 100644 --- a/src/Composer/Package/Archiver/BaseArchiver.php +++ b/src/Composer/Package/Archiver/BaseArchiver.php @@ -1,4 +1,5 @@ + * @author Matthieu Moquet */ abstract class BaseArchiver implements ArchiverInterface { /** - * Format: zip or tar. - * @var string - */ - protected $format = ''; - - /** - * Path to where to dump the export to. - * @var mixed|null - */ - protected $path; - - /** - * @var ProcessExecutor - */ - protected $process; - - /** - * Working directory. - * @var string - */ - protected $temp; - - /** - * @param mixed $path - * @param ProcessExecutor|null $process + * Create a PHAR archive. * - * @throws \InvalidArgumentException + * @param string $sources Path of the directory to archive + * @param string $target Path of the file archive to create + * @param int $format Format of the archive */ - public function __construct($path = null, ProcessExecutor $process = null) - { - if (!empty($path)) { - if (!is_writable($path)) { - throw new \InvalidArgumentException("Not authorized to write to '{$path}'"); - } - $this->path = $path; - } - $this->process = $process ?: new ProcessExecutor(); - $this->temp = sys_get_temp_dir(); - } - - /** - * @return \Composer\Downloader\DownloadManager - */ - public function getDownloadManager() - { - $factory = new Factory; - $dm = $factory->createDownloadManager(new NullIO()); - return $dm; - } - - /** - * @param PackageInterface $package - * @param string $extension - * - * @return string - * @throws \InvalidArgumentException When unknown 'format' is encountered. - */ - public function getFilename(PackageInterface $package, $extension) - { - $name = $package->getPrettyVersion(); - $fileName = sprintf('%s.%s', $name, $extension); - return $fileName; - } - - /** - * @param PackageInterface $package - * - * @return string - * @throws \RuntimeException - */ - protected function getAndEnsureWorkDirectory(PackageInterface $package) - { - $workDir = sprintf('%s/%s/%s', $this->temp, $this->format, $package->getName()); - if (!file_exists($workDir)) { - mkdir($workDir, 0777, true); - } - if (!file_exists($workDir)) { - throw new \RuntimeException("Could not find '{$workDir}' directory."); - } - return $workDir; - } - - /** - * Package the given directory into an archive. - * - * The format is most likely \Phar::TAR or \Phar::ZIP. - * - * @param string $filename - * @param string $workDir - * @param int $format - * - * @throws \RuntimeException - */ - protected function package($filename, $workDir, $format) + protected function createPharArchive($sources, $target, $format) { try { - $phar = new \PharData($filename, null, null, $format); - $phar->buildFromDirectory($workDir); + $phar = new \PharData($target, null, null, $format); + $phar->buildFromDirectory($sources); } catch (\UnexpectedValueException $e) { - $message = "Original PHAR exception: " . (string) $e; - $message .= PHP_EOL . PHP_EOL; - $message .= sprintf("Could not create archive '%s' from '%s'.", $filename, $workDir); - throw new \RuntimeException($message); + throw new \RuntimeException( + sprintf("Could not create archive '%s' from '%s': %s", + $target, + $sources, + $e->getMessage() + )); } } - - /** - * @param string $fileName - * @param string $sourceRef - * @param string $workDir - */ - protected function packageGit($fileName, $sourceRef, $workDir) - { - $command = sprintf( - 'git archive --format %s --output %s %s', - $this->format, - escapeshellarg(sprintf('%s/%s', $this->path, $fileName)), - $sourceRef - ); - $this->process->execute($command, $output, $workDir); - } - - /** - * @param string $fileName - * @param string $sourceRef - * @param string $workDir - */ - protected function packageHg($fileName, $sourceRef, $workDir) - { - $format = ($this->format == 'tarball')?'tar':$this->format; - $command = sprintf( - 'hg archive --rev %s --type %s %s', - $sourceRef, - $format, - escapeshellarg(sprintf('%s/%s', $this->path, $fileName)) - ); - $this->process->execute($command, $output, $workDir); - } } diff --git a/src/Composer/Package/Archiver/GitArchiver.php b/src/Composer/Package/Archiver/GitArchiver.php new file mode 100644 index 000000000..92cb50839 --- /dev/null +++ b/src/Composer/Package/Archiver/GitArchiver.php @@ -0,0 +1,58 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Package\Archiver; + +/** + * @author Till Klampaeckel + * @author Matthieu Moquet + */ +class GitArchiver extends VcsArchiver +{ + /** + * {@inheritdoc} + */ + public function archive($source, $target) + { + $format = $this->format ?: 'zip'; + $sourceRef = $this->sourceRef ?: 'HEAD'; + + $command = sprintf( + 'git archive --format %s --output %s %s', + $format, + escapeshellarg($target), + $sourceRef + ); + + $this->process->execute($command, $output, $source); + } + + /** + * {@inheritdoc} + */ + public function getSourceType() + { + return 'git'; + } + + /** + * {@inheritdoc} + */ + public function supports($format) + { + return in_array($format, array( + 'zip', + 'tar', + 'tgz', + )); + } +} \ No newline at end of file diff --git a/src/Composer/Package/Archiver/MercurialArchiver.php b/src/Composer/Package/Archiver/MercurialArchiver.php new file mode 100644 index 000000000..92b033e60 --- /dev/null +++ b/src/Composer/Package/Archiver/MercurialArchiver.php @@ -0,0 +1,60 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Package\Archiver; + +/** + * @author Till Klampaeckel + * @author Matthieu Moquet + */ +class MercurialArchiver extends VcsArchiver +{ + /** + * {@inheritdoc} + */ + public function archive($source, $target) + { + $format = $this->format ?: 'zip'; + $sourceRef = $this->sourceRef ?: 'default'; + + $command = sprintf( + 'hg archive --rev %s --type %s %s', + $sourceRef, + $format, + escapeshellarg($target) + ); + + $this->process->execute($command, $output, $source); + } + + /** + * {@inheritdoc} + */ + public function getSourceType() + { + return 'hg'; + } + + /** + * {@inheritdoc} + */ + public function supports($format) + { + return in_array($format, array( + 'tar', + 'tbz2', + 'tgz', + 'uzip', + 'zip', + )); + } +} \ No newline at end of file diff --git a/src/Composer/Package/Archiver/TarArchiver.php b/src/Composer/Package/Archiver/TarArchiver.php index 72b134e8e..538f3dd5c 100644 --- a/src/Composer/Package/Archiver/TarArchiver.php +++ b/src/Composer/Package/Archiver/TarArchiver.php @@ -1,4 +1,5 @@ + * @author Till Klampaeckel + * @author Matthieu Moquet */ class TarArchiver extends BaseArchiver { - protected $format = 'tar'; + /** + * {@inheritdoc} + */ + public function archive($sources, $target) + { + $this->createPharArchive($sources, $target, \Phar::TAR); + } /** - * @param PackageInterface $package - * @throws \InvalidArgumentException + * {@inheritdoc} */ - public function dump(PackageInterface $package) + public function supports($format) { - $workDir = $this->getAndEnsureWorkDirectory($package); - - $fileName = $this->getFilename($package, 'tar'); - $sourceType = $package->getSourceType(); - $sourceRef = $package->getSourceReference(); - - $dm = $this->getDownloadManager(); - $dm->download($package, $workDir, true); - - switch ($sourceType) { - case 'git': - $this->packageGit($fileName, $sourceRef, $workDir); - break; - case 'hg': - $this->packageHg($fileName, $sourceRef, $workDir); - break; - case 'svn': - $dir = $workDir . (substr($sourceRef, 0, 1) !== '/')?'/':'' . $sourceRef; - $this->package($fileName, $dir, \Phar::TAR); - break; - default: - throw new \InvalidArgumentException( - "Unable to handle repositories of type '{$sourceType}'."); - } + return 'tar' === $format; } } diff --git a/src/Composer/Package/Archiver/VcsArchiver.php b/src/Composer/Package/Archiver/VcsArchiver.php new file mode 100644 index 000000000..ae83029a4 --- /dev/null +++ b/src/Composer/Package/Archiver/VcsArchiver.php @@ -0,0 +1,60 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Package\Archiver; + +use Composer\Util\ProcessExecutor; + +/** + * VCS archivers are optimized for a specific source type. + * + * @author Till Klampaeckel + * @author Matthieu Moquet + */ +abstract class VcsArchiver implements ArchiverInterface +{ + protected $process; + protected $sourceRef; + protected $format; + + public function __construct($process = null) + { + $this->process = $process ?: new ProcessExecutor(); + } + + public function getSourceRef() + { + return $this->sourceRef; + } + + public function setSourceRef($sourceRef) + { + $this->sourceRef = $sourceRef; + } + + public function getFormat() + { + return $this->format; + } + + public function setFormat($format) + { + $this->format = $format; + } + + /** + * Get the source type supported by the archiver. + * + * @return string The source type of the archiver + */ + abstract public function getSourceType(); +} \ No newline at end of file diff --git a/src/Composer/Package/Archiver/ZipArchiver.php b/src/Composer/Package/Archiver/ZipArchiver.php index 9a6dcf7d3..f1032d096 100644 --- a/src/Composer/Package/Archiver/ZipArchiver.php +++ b/src/Composer/Package/Archiver/ZipArchiver.php @@ -1,4 +1,5 @@ + * @author Matthieu Moquet */ class ZipArchiver extends BaseArchiver { - protected $format = 'zip'; + /** + * {@inheritdoc} + */ + public function archive($sources, $target) + { + $this->createPharArchive($sources, $target, \Phar::ZIP); + } /** - * @param PackageInterface $package - * @throws \InvalidArgumentException + * {@inheritdoc} */ - public function dump(PackageInterface $package) + public function supports($format) { - $workDir = $this->getAndEnsureWorkDirectory($package); - - $fileName = $this->getFilename($package, 'zip'); - $sourceType = $package->getSourceType(); - $sourceRef = $package->getSourceReference(); - - $dm = $this->getDownloadManager(); - $dm->download($package, $workDir, true); - - switch ($sourceType) { - case 'git': - $this->packageGit($fileName, $sourceRef, $workDir); - break; - case 'hg': - $this->packageHg($fileName, $sourceRef, $workDir); - break; - case 'svn': - $dir = $workDir . (substr($sourceRef, 0, 1) !== '/')?'/':'' . $sourceRef; - $this->package($fileName, $dir, \Phar::ZIP); - break; - default: - throw new \InvalidArgumentException("Unable to handle repositories of type '{$sourceType}'."); - } + return 'zip' === $format; } -} \ No newline at end of file +} diff --git a/tests/Composer/Test/Package/Archiver/ArchiveManagerTest.php b/tests/Composer/Test/Package/Archiver/ArchiveManagerTest.php new file mode 100644 index 000000000..75f1259c5 --- /dev/null +++ b/tests/Composer/Test/Package/Archiver/ArchiveManagerTest.php @@ -0,0 +1,92 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Package\Archiver; + +use Composer\Package\Archiver; +use Composer\Package\Archiver\ArchiveManager; +use Composer\Package\PackageInterface; + +/** + * @author Till Klampaeckel + * @author Matthieu Moquet + */ +class ArchiveManagerTest extends ArchiverTest +{ + protected $manager; + + protected $workDir; + + public function setUp() + { + parent::setUp(); + + $this->workDir = sys_get_temp_dir(); + + $this->manager = new ArchiveManager($this->workDir); + $this->manager->addArchiver(new Archiver\GitArchiver); + $this->manager->addArchiver(new Archiver\MercurialArchiver); + $this->manager->addArchiver(new Archiver\TarArchiver); + $this->manager->addArchiver(new Archiver\ZipArchiver); + } + + public function testUnknownFormat() + { + $this->setExpectedException('RuntimeException'); + + $package = $this->setupPackage(); + + $this->manager->archive($package, '__unknown_format__'); + } + + public function testArchiveTarWithVcs() + { + $this->setupGitRepo(); + + $package = $this->setupPackage(); + + // The package is source from git, + // so it should `git archive --format tar` + $this->manager->archive($package, 'tar'); + + $target = $this->getTargetName($package, 'tar'); + $this->assertFileExists($target); + + unlink($target); + $this->removeGitRepo(); + } + + public function testArchiveTarWithoutVcs() + { + $this->setupGitRepo(); + + $package = $this->setupPackage(); + + // This should use the TarArchiver + $this->manager->archive($package, 'tar'); + + $package->setSourceType('__unknown_type__'); // disable VCS recognition + $target = $this->getTargetName($package, 'tar'); + $this->assertFileExists($target); + + unlink($target); + $this->removeGitRepo(); + } + + protected function getTargetName(PackageInterface $package, $format) + { + $packageName = str_replace('/', DIRECTORY_SEPARATOR, $package->getUniqueName()); + $target = $this->workDir.DIRECTORY_SEPARATOR.$packageName.'.'.$format; + + return $target; + } +} diff --git a/tests/Composer/Test/Package/Archiver/ArchiverTest.php b/tests/Composer/Test/Package/Archiver/ArchiverTest.php index e8f64cc86..f1cc3d43a 100644 --- a/tests/Composer/Test/Package/Archiver/ArchiverTest.php +++ b/tests/Composer/Test/Package/Archiver/ArchiverTest.php @@ -1,4 +1,5 @@ + * @author Matthieu Moquet + */ abstract class ArchiverTest extends \PHPUnit_Framework_TestCase { /** * @var \Composer\Util\Filesystem */ - protected $fs; + protected $filesystem; /** * @var \Composer\Util\ProcessExecutor @@ -30,18 +35,13 @@ abstract class ArchiverTest extends \PHPUnit_Framework_TestCase /** * @var string */ - protected $testdir = ''; + protected $testDir; public function setUp() { - $this->fs = new Filesystem; - $this->process = new ProcessExecutor; - $this->testdir = sys_get_temp_dir() . '/composer_archivertest_git_repository' . mt_rand(); - } - - protected function getTestDir() - { - return $this->testdir; + $this->filesystem = new Filesystem(); + $this->process = new ProcessExecutor(); + $this->testDir = sys_get_temp_dir().'/composer_archivertest_git_repository'.mt_rand(); } /** @@ -49,49 +49,42 @@ abstract class ArchiverTest extends \PHPUnit_Framework_TestCase */ protected function setupGitRepo() { - $td = $this->getTestDir(); - - $this->fs->removeDirectory($td); - $this->fs->ensureDirectoryExists($td); + $this->filesystem->removeDirectory($this->testDir); + $this->filesystem->ensureDirectoryExists($this->testDir); $currentWorkDir = getcwd(); - chdir($td); + chdir($this->testDir); - $result = $this->process->execute("git init -q"); + $result = $this->process->execute('git init -q'); if ($result > 0) { - throw new \RuntimeException( - "Could not init: " . $this->process->getErrorOutput()); + throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput()); } + $result = file_put_contents('b', 'a'); if (false === $result) { - throw new \RuntimeException("Could not save file."); + throw new \RuntimeException('Could not save file.'); } - $result = $this->process->execute("git add b && git commit -m 'commit b' -q"); + + $result = $this->process->execute('git add b && git commit -m "commit b" -q'); if ($result > 0) { - throw new \RuntimeException( - "Could not init: " . $this->process->getErrorOutput()); + throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput()); } + chdir($currentWorkDir); } protected function removeGitRepo() { - $td = $this->getTestDir(); - $this->fs->removeDirectory($td); + $this->filesystem->removeDirectory($this->testDir); } protected function setupPackage() { - $td = $this->getTestDir(); $package = new MemoryPackage('archivertest/archivertest', 'master', 'master'); - $package->setSourceUrl("file://$td"); + $package->setSourceUrl(realpath($this->testDir)); $package->setSourceReference('master'); $package->setSourceType('git'); + return $package; } - - protected function getPackageFileName(MemoryPackage $package) - { - return $package->getVersion(); - } } diff --git a/tests/Composer/Test/Package/Archiver/GitArchiverTest.php b/tests/Composer/Test/Package/Archiver/GitArchiverTest.php new file mode 100644 index 000000000..3ac47e22c --- /dev/null +++ b/tests/Composer/Test/Package/Archiver/GitArchiverTest.php @@ -0,0 +1,58 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Package\Archiver; + +use Composer\Package\Archiver\GitArchiver; + +/** + * @author Till Klampaeckel + * @author Matthieu Moquet + */ +class GitArchiverTest extends ArchiverTest +{ + public function testZipArchive() + { + $this->setupGitRepo(); + + $package = $this->setupPackage(); + $target = sys_get_temp_dir().'/composer_archiver_test.zip'; + + // Test archive + $archiver = new GitArchiver(); + $archiver->setFormat('zip'); + $archiver->setSourceRef('master'); + $archiver->archive($package->getSourceUrl(), $target); + $this->assertFileExists($target); + + unlink($target); + $this->removeGitRepo(); + } + + public function testTarArchive() + { + $this->setupGitRepo(); + + $package = $this->setupPackage(); + $target = sys_get_temp_dir().'/composer_archiver_test.tar'; + + // Test archive + $archiver = new GitArchiver(); + $archiver->setFormat('tar'); + $archiver->setSourceRef('master'); + $archiver->archive($package->getSourceUrl(), $target); + $this->assertFileExists($target); + + unlink($target); + $this->removeGitRepo(); + } +} diff --git a/tests/Composer/Test/Package/Archiver/MercurialArchiverTest.php b/tests/Composer/Test/Package/Archiver/MercurialArchiverTest.php new file mode 100644 index 000000000..b7b0eca64 --- /dev/null +++ b/tests/Composer/Test/Package/Archiver/MercurialArchiverTest.php @@ -0,0 +1,103 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Package\Archiver; + +use Composer\Package\Archiver\MercurialArchiver; +use Composer\Package\MemoryPackage; + +/** + * @author Matthieu Moquet + * @author Till Klampaeckel + */ +class MercurialArchiverTest extends ArchiverTest +{ + public function testZipArchive() + { + $this->setupMercurialRepo(); + + $package = $this->setupMercurialPackage(); + $target = sys_get_temp_dir().'/composer_archiver_test.zip'; + + // Test archive + $archiver = new MercurialArchiver(); + $archiver->setFormat('zip'); + $archiver->setSourceRef('default'); + $archiver->archive($package->getSourceUrl(), $target); + $this->assertFileExists($target); + + unlink($target); + $this->removeMercurialRepo(); + } + + public function testTarArchive() + { + $this->setupMercurialRepo(); + + $package = $this->setupMercurialPackage(); + $target = sys_get_temp_dir().'/composer_archiver_test.tar'; + + // Test archive + $archiver = new MercurialArchiver(); + $archiver->setFormat('tar'); + $archiver->setSourceRef('default'); + $archiver->archive($package->getSourceUrl(), $target); + $this->assertFileExists($target); + + unlink($target); + $this->removeMercurialRepo(); + } + + /** + * Create local git repository to run tests against! + */ + protected function setupMercurialRepo() + { + $this->filesystem->removeDirectory($this->testDir); + $this->filesystem->ensureDirectoryExists($this->testDir); + + $currentWorkDir = getcwd(); + chdir($this->testDir); + + $result = $this->process->execute('hg init -q'); + if ($result > 0) { + throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput()); + } + + $result = file_put_contents('b', 'a'); + if (false === $result) { + throw new \RuntimeException('Could not save file.'); + } + + $result = $this->process->execute('hg add b && hg commit -m "commit b" --config ui.username=test -q'); + if ($result > 0) { + throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput()); + } + + chdir($currentWorkDir); + } + + protected function removeMercurialRepo() + { + $this->filesystem->removeDirectory($this->testDir); + } + + protected function setupMercurialPackage() + { + $package = new MemoryPackage('archivertest/archivertest', 'master', 'master'); + $package->setSourceUrl(realpath($this->testDir)); + $package->setSourceReference('default'); + $package->setSourceType('hg'); + + return $package; + } +} diff --git a/tests/Composer/Test/Package/Archiver/TarArchiverTest.php b/tests/Composer/Test/Package/Archiver/TarArchiverTest.php index 23cd09e64..caf8d5e98 100644 --- a/tests/Composer/Test/Package/Archiver/TarArchiverTest.php +++ b/tests/Composer/Test/Package/Archiver/TarArchiverTest.php @@ -1,4 +1,5 @@ + * @author Matthieu Moquet + */ class TarArchiverTest extends ArchiverTest { - public function testThis() + public function testArchive() { $this->setupGitRepo(); + $package = $this->setupPackage(); - $name = $this->getPackageFileName($package); + $target = sys_get_temp_dir().'/composer_archiver_test.tar'; - $temp = sys_get_temp_dir(); - $tar = new TarArchiver($temp); - $tar->dump($package); + // Test archive + $archiver = new TarArchiver(); + $archiver->archive($package->getSourceUrl(), $target); + $this->assertFileExists($target); - $dist = sprintf('%s/%s.tar', - $temp, $name - ); - $this->assertFileExists($dist); - unlink($dist); + unlink($target); $this->removeGitRepo(); } - - /** - * @expectedException \InvalidArgumentException - */ - public function testException() - { - new TarArchiver("/totally-random-" . time()); - } } diff --git a/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php b/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php index 5d2fa40af..0e7403f59 100644 --- a/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php +++ b/tests/Composer/Test/Package/Archiver/ZipArchiverTest.php @@ -1,4 +1,5 @@ + * @author Matthieu Moquet + */ class ZipArchiverTest extends ArchiverTest { - public function testThis() + public function testArchive() { $this->setupGitRepo(); + $package = $this->setupPackage(); - $name = $this->getPackageFileName($package); + $target = sys_get_temp_dir().'/composer_archiver_test.zip'; - $temp = sys_get_temp_dir(); - $zip = new ZipArchiver($temp); - $zip->dump($package); + // Test archive + $archiver = new ZipArchiver(); + $archiver->archive($package->getSourceUrl(), $target); + $this->assertFileExists($target); - $dist = sprintf('%s/%s.zip', - $temp, $name - ); - $this->assertFileExists($dist); - unlink($dist); + unlink($target); $this->removeGitRepo(); } - - /** - * @expectedException \InvalidArgumentException - */ - public function testException() - { - new ZipArchiver("/totally-random-" . time()); - } }