ArchiveManagerTest.php 4.6 KB

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