ArchiveManagerTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. class ArchiveManagerTest extends ArchiverTest
  18. {
  19. protected $manager;
  20. protected $targetDir;
  21. public function setUp()
  22. {
  23. parent::setUp();
  24. $factory = new Factory();
  25. $this->manager = $factory->createArchiveManager($factory->createConfig());
  26. $this->targetDir = $this->testDir.'/composer_archiver_tests';
  27. }
  28. public function testUnknownFormat()
  29. {
  30. $this->setExpectedException('RuntimeException');
  31. $package = $this->setupPackage();
  32. $this->manager->archive($package, '__unknown_format__', $this->targetDir);
  33. }
  34. public function testArchiveTar()
  35. {
  36. $this->setupGitRepo();
  37. $package = $this->setupPackage();
  38. $this->manager->archive($package, 'tar', $this->targetDir);
  39. $target = $this->getTargetName($package, 'tar');
  40. $this->assertFileExists($target);
  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('b', 'a');
  63. if (false === $result) {
  64. chdir($currentWorkDir);
  65. throw new \RuntimeException('Could not save file.');
  66. }
  67. $result = $this->process->execute('git add b && git commit -m "commit b" -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. }