ArchiveManagerTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Package\Archiver;
  12. use Composer\Factory;
  13. use Composer\Package\Archiver;
  14. use Composer\Package\PackageInterface;
  15. class ArchiveManagerTest extends ArchiverTest
  16. {
  17. protected $manager;
  18. protected $targetDir;
  19. public function setUp()
  20. {
  21. parent::setUp();
  22. $factory = new Factory();
  23. $this->manager = $factory->createArchiveManager($factory->createConfig());
  24. $this->targetDir = $this->testDir.'/composer_archiver_tests';
  25. }
  26. public function testUnknownFormat()
  27. {
  28. $this->setExpectedException('RuntimeException');
  29. $package = $this->setupPackage();
  30. $this->manager->archive($package, '__unknown_format__', $this->targetDir);
  31. }
  32. public function testArchiveTar()
  33. {
  34. $this->setupGitRepo();
  35. $package = $this->setupPackage();
  36. $this->manager->archive($package, 'tar', $this->targetDir);
  37. $target = $this->getTargetName($package, 'tar');
  38. $this->assertFileExists($target);
  39. unlink($target);
  40. }
  41. protected function getTargetName(PackageInterface $package, $format)
  42. {
  43. $packageName = $this->manager->getPackageFilename($package);
  44. $target = $this->targetDir.'/'.$packageName.'.'.$format;
  45. return $target;
  46. }
  47. /**
  48. * Create local git repository to run tests against!
  49. */
  50. protected function setupGitRepo()
  51. {
  52. $currentWorkDir = getcwd();
  53. chdir($this->testDir);
  54. $output = null;
  55. $result = $this->process->execute('git init -q', $output, $this->testDir);
  56. if ($result > 0) {
  57. chdir($currentWorkDir);
  58. throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput());
  59. }
  60. $result = file_put_contents('b', 'a');
  61. if (false === $result) {
  62. chdir($currentWorkDir);
  63. throw new \RuntimeException('Could not save file.');
  64. }
  65. $result = $this->process->execute('git add b && git commit -m "commit b" -q', $output, $this->testDir);
  66. if ($result > 0) {
  67. chdir($currentWorkDir);
  68. throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput());
  69. }
  70. chdir($currentWorkDir);
  71. }
  72. }