diff --git a/tests/Composer/Test/Package/Archiver/ArchiveManagerTest.php b/tests/Composer/Test/Package/Archiver/ArchiveManagerTest.php index e289d7f9a..d8f864bd2 100644 --- a/tests/Composer/Test/Package/Archiver/ArchiveManagerTest.php +++ b/tests/Composer/Test/Package/Archiver/ArchiveManagerTest.php @@ -33,7 +33,7 @@ class ArchiveManagerTest extends ArchiverTest $factory = new Factory(); $this->manager = $factory->createArchiveManager(null, $factory->createConfig()); - $this->targetDir = sys_get_temp_dir().'/composer_archiver_tests'; + $this->targetDir = $this->testDir.'/composer_archiver_tests'; } public function testUnknownFormat() @@ -45,7 +45,7 @@ class ArchiveManagerTest extends ArchiverTest $this->manager->archive($package, '__unknown_format__', $this->targetDir); } - public function testArchiveTarWithVcs() + public function testArchiveTar() { $this->setupGitRepo(); @@ -59,24 +59,6 @@ class ArchiveManagerTest extends ArchiverTest $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', $this->targetDir); - - $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) @@ -86,4 +68,33 @@ class ArchiveManagerTest extends ArchiverTest return $target; } + + /** + * Create local git repository to run tests against! + */ + protected function setupGitRepo() + { + $currentWorkDir = getcwd(); + chdir($this->testDir); + + $result = $this->process->execute('git init -q'); + if ($result > 0) { + chdir($currentWorkDir); + throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput()); + } + + $result = file_put_contents('b', 'a'); + if (false === $result) { + chdir($currentWorkDir); + throw new \RuntimeException('Could not save file.'); + } + + $result = $this->process->execute('git add b && git commit -m "commit b" -q'); + if ($result > 0) { + chdir($currentWorkDir); + throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput()); + } + + chdir($currentWorkDir); + } } diff --git a/tests/Composer/Test/Package/Archiver/ArchiverTest.php b/tests/Composer/Test/Package/Archiver/ArchiverTest.php index 62e4bb914..993244788 100644 --- a/tests/Composer/Test/Package/Archiver/ArchiverTest.php +++ b/tests/Composer/Test/Package/Archiver/ArchiverTest.php @@ -41,46 +41,20 @@ abstract class ArchiverTest extends \PHPUnit_Framework_TestCase { $this->filesystem = new Filesystem(); $this->process = new ProcessExecutor(); - $this->testDir = sys_get_temp_dir().'/composer_archivertest_git_repository'.mt_rand(); + $this->testDir = sys_get_temp_dir().'/composer_archiver_test_'.mt_rand(); + $this->filesystem->ensureDirectoryExists($this->testDir); + } + + public function tearDown() + { + $this->filesystem->removeDirectory($this->testDir); } /** - * Create local git repository to run tests against! + * Util method to quickly setup a package using the source path built. + * + * @return \Composer\Package\Package */ - protected function setupGitRepo() - { - $this->filesystem->removeDirectory($this->testDir); - $this->filesystem->ensureDirectoryExists($this->testDir); - - $currentWorkDir = getcwd(); - chdir($this->testDir); - - $result = $this->process->execute('git init -q'); - if ($result > 0) { - chdir($currentWorkDir); - throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput()); - } - - $result = file_put_contents('b', 'a'); - if (false === $result) { - chdir($currentWorkDir); - throw new \RuntimeException('Could not save file.'); - } - - $result = $this->process->execute('git add b && git commit -m "commit b" -q'); - if ($result > 0) { - chdir($currentWorkDir); - throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput()); - } - - chdir($currentWorkDir); - } - - protected function removeGitRepo() - { - $this->filesystem->removeDirectory($this->testDir); - } - protected function setupPackage() { $package = new Package('archivertest/archivertest', 'master', 'master'); diff --git a/tests/Composer/Test/Package/Archiver/GitArchiverTest.php b/tests/Composer/Test/Package/Archiver/GitArchiverTest.php index 9ddb9d4f4..6934bd2de 100644 --- a/tests/Composer/Test/Package/Archiver/GitArchiverTest.php +++ b/tests/Composer/Test/Package/Archiver/GitArchiverTest.php @@ -20,10 +20,12 @@ use Composer\Package\Archiver\GitArchiver; */ class GitArchiverTest extends ArchiverTest { + protected $targetFile; + public function testZipArchive() { + // Set up repository $this->setupGitRepo(); - $package = $this->setupPackage(); $target = sys_get_temp_dir().'/composer_archiver_test.zip'; @@ -33,13 +35,12 @@ class GitArchiverTest extends ArchiverTest $this->assertFileExists($target); unlink($target); - $this->removeGitRepo(); } public function testTarArchive() { + // Set up repository $this->setupGitRepo(); - $package = $this->setupPackage(); $target = sys_get_temp_dir().'/composer_archiver_test.tar'; @@ -49,6 +50,34 @@ class GitArchiverTest extends ArchiverTest $this->assertFileExists($target); unlink($target); - $this->removeGitRepo(); + } + + /** + * Create local git repository to run tests against! + */ + protected function setupGitRepo() + { + $currentWorkDir = getcwd(); + chdir($this->testDir); + + $result = $this->process->execute('git init -q'); + if ($result > 0) { + chdir($currentWorkDir); + throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput()); + } + + $result = file_put_contents('b', 'a'); + if (false === $result) { + chdir($currentWorkDir); + throw new \RuntimeException('Could not save file.'); + } + + $result = $this->process->execute('git add b && git commit -m "commit b" -q'); + if ($result > 0) { + chdir($currentWorkDir); + throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput()); + } + + chdir($currentWorkDir); } } diff --git a/tests/Composer/Test/Package/Archiver/MercurialArchiverTest.php b/tests/Composer/Test/Package/Archiver/MercurialArchiverTest.php index 4934572da..e3b316a6b 100644 --- a/tests/Composer/Test/Package/Archiver/MercurialArchiverTest.php +++ b/tests/Composer/Test/Package/Archiver/MercurialArchiverTest.php @@ -23,9 +23,9 @@ class MercurialArchiverTest extends ArchiverTest { public function testZipArchive() { + // Set up repository $this->setupMercurialRepo(); - - $package = $this->setupMercurialPackage(); + $package = $this->setupPackage(); $target = sys_get_temp_dir().'/composer_archiver_test.zip'; // Test archive @@ -34,14 +34,13 @@ class MercurialArchiverTest extends ArchiverTest $this->assertFileExists($target); unlink($target); - $this->removeMercurialRepo(); } public function testTarArchive() { + // Set up repository $this->setupMercurialRepo(); - - $package = $this->setupMercurialPackage(); + $package = $this->setupPackage(); $target = sys_get_temp_dir().'/composer_archiver_test.tar'; // Test archive @@ -50,47 +49,40 @@ class MercurialArchiverTest extends ArchiverTest $this->assertFileExists($target); unlink($target); - $this->removeMercurialRepo(); } /** - * Create local git repository to run tests against! + * Create local mercurial 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) { + chdir($currentWorkDir); throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput()); } $result = file_put_contents('b', 'a'); if (false === $result) { + chdir($currentWorkDir); 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) { + chdir($currentWorkDir); throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput()); } chdir($currentWorkDir); } - protected function removeMercurialRepo() + protected function setupPackage() { - $this->filesystem->removeDirectory($this->testDir); - } - - protected function setupMercurialPackage() - { - $package = new Package('archivertest/archivertest', 'master', 'master'); - $package->setSourceUrl(realpath($this->testDir)); + $package = parent::setupPackage(); $package->setSourceReference('default'); $package->setSourceType('hg'); diff --git a/tests/Composer/Test/Package/Archiver/PharArchiverTest.php b/tests/Composer/Test/Package/Archiver/PharArchiverTest.php index 625626b28..0e5099668 100644 --- a/tests/Composer/Test/Package/Archiver/PharArchiverTest.php +++ b/tests/Composer/Test/Package/Archiver/PharArchiverTest.php @@ -22,8 +22,8 @@ class PharArchiverTest extends ArchiverTest { public function testTarArchive() { - $this->setupGitRepo(); - + // Set up repository + $this->setupDummyRepo(); $package = $this->setupPackage(); $target = sys_get_temp_dir().'/composer_archiver_test.tar'; @@ -33,13 +33,12 @@ class PharArchiverTest extends ArchiverTest $this->assertFileExists($target); unlink($target); - $this->removeGitRepo(); } public function testZipArchive() { - $this->setupGitRepo(); - + // Set up repository + $this->setupDummyRepo(); $package = $this->setupPackage(); $target = sys_get_temp_dir().'/composer_archiver_test.zip'; @@ -49,6 +48,22 @@ class PharArchiverTest extends ArchiverTest $this->assertFileExists($target); unlink($target); - $this->removeGitRepo(); + } + + /** + * Create a local dummy repository to run tests against! + */ + protected function setupDummyRepo() + { + $currentWorkDir = getcwd(); + chdir($this->testDir); + + $result = file_put_contents('b', 'a'); + if (false === $result) { + chdir($currentWorkDir); + throw new \RuntimeException('Could not save file.'); + } + + chdir($currentWorkDir); } }