ArchiveManagerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. $target = $this->targetDir.'/'.$packageName.'.'.$format;
  68. return $target;
  69. }
  70. /**
  71. * Create local git repository to run tests against!
  72. */
  73. protected function setupGitRepo()
  74. {
  75. $currentWorkDir = getcwd();
  76. chdir($this->testDir);
  77. $output = null;
  78. $result = $this->process->execute('git init -q', $output, $this->testDir);
  79. if ($result > 0) {
  80. chdir($currentWorkDir);
  81. throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput());
  82. }
  83. $result = $this->process->execute('git config user.email "you@example.com"', $output, $this->testDir);
  84. if ($result > 0) {
  85. chdir($currentWorkDir);
  86. throw new \RuntimeException('Could not config: '.$this->process->getErrorOutput());
  87. }
  88. $result = $this->process->execute('git config user.name "Your Name"', $output, $this->testDir);
  89. if ($result > 0) {
  90. chdir($currentWorkDir);
  91. throw new \RuntimeException('Could not config: '.$this->process->getErrorOutput());
  92. }
  93. $result = file_put_contents('composer.json', '{"name":"faker/faker", "description": "description", "license": "MIT"}');
  94. if (false === $result) {
  95. chdir($currentWorkDir);
  96. throw new \RuntimeException('Could not save file.');
  97. }
  98. $result = $this->process->execute('git add composer.json && git commit -m "commit composer.json" -q', $output, $this->testDir);
  99. if ($result > 0) {
  100. chdir($currentWorkDir);
  101. throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput());
  102. }
  103. chdir($currentWorkDir);
  104. }
  105. }