diff --git a/src/Composer/Package/Archiver/ArchiveManager.php b/src/Composer/Package/Archiver/ArchiveManager.php index 527757a4e..656d2f7a2 100644 --- a/src/Composer/Package/Archiver/ArchiveManager.php +++ b/src/Composer/Package/Archiver/ArchiveManager.php @@ -108,6 +108,9 @@ class ArchiveManager if (empty($format)) { throw new \InvalidArgumentException('Format must be specified'); } + if(!is_null($fileName) && !is_string($fileName)) { + throw new \InvalidArgumentException('fileName must be a string'); + } // Search for the most appropriate archiver $usableArchiver = null; @@ -124,7 +127,11 @@ class ArchiveManager } $filesystem = new Filesystem(); - $packageName = $this->getPackageFilename($package); + if(null === $fileName) { + $packageName = $fileName; + } else { + $packageName = $this->getPackageFilename($package); + } // Archive filename $filesystem->ensureDirectoryExists($targetDir); diff --git a/tests/Composer/Test/Package/Archiver/ArchiveManagerTest.php b/tests/Composer/Test/Package/Archiver/ArchiveManagerTest.php index f4d343e63..d5f7300a9 100644 --- a/tests/Composer/Test/Package/Archiver/ArchiveManagerTest.php +++ b/tests/Composer/Test/Package/Archiver/ArchiveManagerTest.php @@ -13,11 +13,16 @@ namespace Composer\Test\Package\Archiver; use Composer\Factory; +use Composer\Package\Archiver\ArchiveManager; use Composer\Package\PackageInterface; class ArchiveManagerTest extends ArchiverTest { + /** + * @var ArchiveManager + */ protected $manager; + protected $targetDir; public function setUp() @@ -55,9 +60,41 @@ class ArchiveManagerTest extends ArchiverTest unlink($target); } - protected function getTargetName(PackageInterface $package, $format) + public function testArchiveCustomFileName() { - $packageName = $this->manager->getPackageFilename($package); + $this->setupGitRepo(); + + $package = $this->setupPackage(); + + $fileName = "testArchiveName"; + + $this->manager->archive($package, 'tar', $this->targetDir, $fileName); + + $target = $this->getTargetName($package, 'tar', $fileName); + + $this->assertFileExists($target); + + $tmppath = sys_get_temp_dir().'/composer_archiver/'.$this->manager->getPackageFilename($package); + $this->assertFileNotExists($tmppath); + + unlink($target); + } + + public function testNonStringFileName() { + $this->setExpectedException("InvalidArgumentException"); + + $package = $this->setupPackage(); + + $this->manager->archive($package, 'tar', $this->targetDir, array()); + } + + protected function getTargetName(PackageInterface $package, $format, $fileName = null) + { + if(!is_null($fileName)) { + $packageName = $fileName; + } else { + $packageName = $this->manager->getPackageFilename($package); + } $target = $this->targetDir.'/'.$packageName.'.'.$format; return $target;