ArchiveManagerTest.php 4.3 KB

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