ArchiveManagerTest.php 2.7 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\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 = file_put_contents('composer.json', '{"name":"faker/faker", "description": "description", "license": "MIT"}');
  62. if (false === $result) {
  63. chdir($currentWorkDir);
  64. throw new \RuntimeException('Could not save file.');
  65. }
  66. $result = $this->process->execute('git add composer.json && git commit -m "commit composer.json" -q', $output, $this->testDir);
  67. if ($result > 0) {
  68. chdir($currentWorkDir);
  69. throw new \RuntimeException('Could not commit: '.$this->process->getErrorOutput());
  70. }
  71. chdir($currentWorkDir);
  72. }
  73. }