ArchiveManagerTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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\ArchiveManager;
  14. use Composer\Package\PackageInterface;
  15. class ArchiveManagerTest extends ArchiverTest
  16. {
  17. /**
  18. * @var ArchiveManager
  19. */
  20. protected $manager;
  21. protected $targetDir;
  22. public function setUp()
  23. {
  24. parent::setUp();
  25. $factory = new Factory();
  26. $this->manager = $factory->createArchiveManager($factory->createConfig());
  27. $this->targetDir = $this->testDir.'/composer_archiver_tests';
  28. }
  29. public function testUnknownFormat()
  30. {
  31. $this->setExpectedException('RuntimeException');
  32. $package = $this->setupPackage();
  33. $this->manager->archive($package, '__unknown_format__', $this->targetDir);
  34. }
  35. public function testArchiveTar()
  36. {
  37. $this->skipIfNotExecutable('git');
  38. $this->setupGitRepo();
  39. $package = $this->setupPackage();
  40. $this->manager->archive($package, 'tar', $this->targetDir);
  41. $target = $this->getTargetName($package, 'tar');
  42. $this->assertFileExists($target);
  43. $tmppath = sys_get_temp_dir().'/composer_archiver/'.$this->manager->getPackageFilename($package);
  44. $this->assertFileNotExists($tmppath);
  45. unlink($target);
  46. }
  47. public function testArchiveCustomFileName()
  48. {
  49. $this->skipIfNotExecutable('git');
  50. $this->setupGitRepo();
  51. $package = $this->setupPackage();
  52. $fileName = 'testArchiveName';
  53. $this->manager->archive($package, 'tar', $this->targetDir, $fileName);
  54. $target = $this->targetDir . '/' . $fileName . '.tar';
  55. $this->assertFileExists($target);
  56. $tmppath = sys_get_temp_dir().'/composer_archiver/'.$this->manager->getPackageFilename($package);
  57. $this->assertFileNotExists($tmppath);
  58. unlink($target);
  59. }
  60. protected function getTargetName(PackageInterface $package, $format, $fileName = null)
  61. {
  62. if (null === $fileName) {
  63. $packageName = $this->manager->getPackageFilename($package);
  64. } else {
  65. $packageName = $fileName;
  66. }
  67. return $this->targetDir.'/'.$packageName.'.'.$format;
  68. }
  69. /**
  70. * Create local git repository to run tests against!
  71. */
  72. protected function setupGitRepo()
  73. {
  74. $currentWorkDir = getcwd();
  75. chdir($this->testDir);
  76. $output = null;
  77. $result = $this->process->execute('git init -q', $output, $this->testDir);
  78. if ($result > 0) {
  79. chdir($currentWorkDir);
  80. throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput());
  81. }
  82. $result = $this->process->execute('git config user.email "you@example.com"', $output, $this->testDir);
  83. if ($result > 0) {
  84. chdir($currentWorkDir);
  85. throw new \RuntimeException('Could not config: '.$this->process->getErrorOutput());
  86. }
  87. $result = $this->process->execute('git config user.name "Your Name"', $output, $this->testDir);
  88. if ($result > 0) {
  89. chdir($currentWorkDir);
  90. throw new \RuntimeException('Could not config: '.$this->process->getErrorOutput());
  91. }
  92. $result = file_put_contents('composer.json', '{"name":"faker/faker", "description": "description", "license": "MIT"}');
  93. if (false === $result) {
  94. chdir($currentWorkDir);
  95. throw new \RuntimeException('Could not save file.');
  96. }
  97. $result = $this->process->execute('git add composer.json && git commit -m "commit composer.json" -q', $output, $this->testDir);
  98. if ($result > 0) {
  99. chdir($currentWorkDir);
  100. throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput());
  101. }
  102. chdir($currentWorkDir);
  103. }
  104. }