ArchiveManagerTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\IO\NullIO;
  14. use Composer\Package\Archiver;
  15. use Composer\Package\Archiver\ArchiveManager;
  16. use Composer\Package\PackageInterface;
  17. /**
  18. * @author Till Klampaeckel <till@php.net>
  19. * @author Matthieu Moquet <matthieu@moquet.net>
  20. */
  21. class ArchiveManagerTest extends ArchiverTest
  22. {
  23. protected $manager;
  24. protected $targetDir;
  25. public function setUp()
  26. {
  27. parent::setUp();
  28. $factory = new Factory();
  29. $this->manager = $factory->createArchiveManager(null, $factory->createConfig());
  30. $this->targetDir = $this->testDir.'/composer_archiver_tests';
  31. }
  32. public function testUnknownFormat()
  33. {
  34. $this->setExpectedException('RuntimeException');
  35. $package = $this->setupPackage();
  36. $this->manager->archive($package, '__unknown_format__', $this->targetDir);
  37. }
  38. public function testArchiveTar()
  39. {
  40. $this->setupGitRepo();
  41. $package = $this->setupPackage();
  42. // The package is source from git,
  43. // so it should `git archive --format tar`
  44. $this->manager->archive($package, 'tar', $this->targetDir);
  45. $target = $this->getTargetName($package, 'tar');
  46. $this->assertFileExists($target);
  47. unlink($target);
  48. }
  49. protected function getTargetName(PackageInterface $package, $format)
  50. {
  51. $packageName = $package->getUniqueName();
  52. $target = $this->targetDir.'/'.$packageName.'.'.$format;
  53. return $target;
  54. }
  55. /**
  56. * Create local git repository to run tests against!
  57. */
  58. protected function setupGitRepo()
  59. {
  60. $currentWorkDir = getcwd();
  61. chdir($this->testDir);
  62. $result = $this->process->execute('git init -q');
  63. if ($result > 0) {
  64. chdir($currentWorkDir);
  65. throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput());
  66. }
  67. $result = file_put_contents('b', 'a');
  68. if (false === $result) {
  69. chdir($currentWorkDir);
  70. throw new \RuntimeException('Could not save file.');
  71. }
  72. $result = $this->process->execute('git add b && git commit -m "commit b" -q');
  73. if ($result > 0) {
  74. chdir($currentWorkDir);
  75. throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput());
  76. }
  77. chdir($currentWorkDir);
  78. }
  79. }