ArchiveManagerTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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;
  14. use Composer\Package\PackageInterface;
  15. class ArchiveManagerTest extends ArchiverTest
  16. {
  17. protected $manager;
  18. protected $targetDir;
  19. public function setUp()
  20. {
  21. parent::setUp();
  22. $factory = new Factory();
  23. $this->manager = $factory->createArchiveManager($factory->createConfig());
  24. $this->targetDir = $this->testDir.'/composer_archiver_tests';
  25. }
  26. public function testUnknownFormat()
  27. {
  28. $this->setExpectedException('RuntimeException');
  29. $package = $this->setupPackage();
  30. $this->manager->archive($package, '__unknown_format__', $this->targetDir);
  31. }
  32. public function testArchiveTar()
  33. {
  34. $this->setupGitRepo();
  35. $package = $this->setupPackage();
  36. $this->manager->archive($package, 'tar', $this->targetDir);
  37. $target = $this->getTargetName($package, 'tar');
  38. $this->assertFileExists($target);
  39. $tmppath = sys_get_temp_dir().'/composer_archiver/'.$this->manager->getPackageFilename($package);
  40. $this->assertFileNotExists($tmppath);
  41. unlink($target);
  42. }
  43. protected function getTargetName(PackageInterface $package, $format)
  44. {
  45. $packageName = $this->manager->getPackageFilename($package);
  46. $target = $this->targetDir.'/'.$packageName.'.'.$format;
  47. return $target;
  48. }
  49. /**
  50. * Create local git repository to run tests against!
  51. */
  52. protected function setupGitRepo()
  53. {
  54. $currentWorkDir = getcwd();
  55. chdir($this->testDir);
  56. $output = null;
  57. $result = $this->process->execute('git init -q', $output, $this->testDir);
  58. if ($result > 0) {
  59. chdir($currentWorkDir);
  60. throw new \RuntimeException('Could not init: '.$this->process->getErrorOutput());
  61. }
  62. $result = file_put_contents('composer.json', '{"name":"faker/faker", "description": "description", "license": "MIT"}');
  63. if (false === $result) {
  64. chdir($currentWorkDir);
  65. throw new \RuntimeException('Could not save file.');
  66. }
  67. $result = $this->process->execute('git add composer.json && git commit -m "commit composer.json" -q', $output, $this->testDir);
  68. if ($result > 0) {
  69. chdir($currentWorkDir);
  70. throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput());
  71. }
  72. chdir($currentWorkDir);
  73. }
  74. }