ArchiveManagerTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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->setupGitRepo();
  38. $package = $this->setupPackage();
  39. $this->manager->archive($package, 'tar', $this->targetDir);
  40. $target = $this->getTargetName($package, 'tar');
  41. $this->assertFileExists($target);
  42. $tmppath = sys_get_temp_dir().'/composer_archiver/'.$this->manager->getPackageFilename($package);
  43. $this->assertFileNotExists($tmppath);
  44. unlink($target);
  45. }
  46. public function testArchiveCustomFileName()
  47. {
  48. $this->setupGitRepo();
  49. $package = $this->setupPackage();
  50. $fileName = 'testArchiveName';
  51. $this->manager->archive($package, 'tar', $this->targetDir, $fileName);
  52. $target = $this->targetDir . '/' . $fileName . '.tar';
  53. $this->assertFileExists($target);
  54. $tmppath = sys_get_temp_dir().'/composer_archiver/'.$this->manager->getPackageFilename($package);
  55. $this->assertFileNotExists($tmppath);
  56. unlink($target);
  57. }
  58. protected function getTargetName(PackageInterface $package, $format, $fileName = null)
  59. {
  60. if (null === $fileName) {
  61. $packageName = $this->manager->getPackageFilename($package);
  62. } else {
  63. $packageName = $fileName;
  64. }
  65. $target = $this->targetDir.'/'.$packageName.'.'.$format;
  66. return $target;
  67. }
  68. /**
  69. * Create local git repository to run tests against!
  70. */
  71. protected function setupGitRepo()
  72. {
  73. $currentWorkDir = getcwd();
  74. chdir($this->testDir);
  75. $output = null;
  76. $result = $this->process->execute('git init -q', $output, $this->testDir);
  77. if ($result > 0) {
  78. chdir($currentWorkDir);
  79. throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput());
  80. }
  81. $result = $this->process->execute('git config user.email "you@example.com"', $output, $this->testDir);
  82. if ($result > 0) {
  83. chdir($currentWorkDir);
  84. throw new \RuntimeException('Could not config: '.$this->process->getErrorOutput());
  85. }
  86. $result = $this->process->execute('git config user.name "Your Name"', $output, $this->testDir);
  87. if ($result > 0) {
  88. chdir($currentWorkDir);
  89. throw new \RuntimeException('Could not config: '.$this->process->getErrorOutput());
  90. }
  91. $result = file_put_contents('composer.json', '{"name":"faker/faker", "description": "description", "license": "MIT"}');
  92. if (false === $result) {
  93. chdir($currentWorkDir);
  94. throw new \RuntimeException('Could not save file.');
  95. }
  96. $result = $this->process->execute('git add composer.json && git commit -m "commit composer.json" -q', $output, $this->testDir);
  97. if ($result > 0) {
  98. chdir($currentWorkDir);
  99. throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput());
  100. }
  101. chdir($currentWorkDir);
  102. }
  103. }