ArchiveManagerTest.php 3.2 KB

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