VcsRepositoryTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\Repository;
  12. use Symfony\Component\Process\ExecutableFinder;
  13. use Composer\Package\Dumper\ArrayDumper;
  14. use Composer\Repository\VcsRepository;
  15. use Composer\Util\Filesystem;
  16. use Composer\Util\ProcessExecutor;
  17. use Composer\IO\NullIO;
  18. use Composer\Config;
  19. /**
  20. * @group slow
  21. */
  22. class VcsRepositoryTest extends \PHPUnit_Framework_TestCase
  23. {
  24. private static $gitRepo;
  25. private $skipped;
  26. protected function initialize()
  27. {
  28. $oldCwd = getcwd();
  29. self::$gitRepo = sys_get_temp_dir() . '/composer-git-'.mt_rand().'/';
  30. $locator = new ExecutableFinder();
  31. if (!$locator->find('git')) {
  32. $this->skipped = 'This test needs a git binary in the PATH to be able to run';
  33. return;
  34. }
  35. if (!mkdir(self::$gitRepo) || !chdir(self::$gitRepo)) {
  36. $this->skipped = 'Could not create and move into the temp git repo '.self::$gitRepo;
  37. return;
  38. }
  39. // init
  40. $process = new ProcessExecutor;
  41. $exec = function ($command) use ($process) {
  42. $cwd = getcwd();
  43. if ($process->execute($command, $output, $cwd) !== 0) {
  44. throw new \RuntimeException('Failed to execute '.$command.': '.$process->getErrorOutput());
  45. }
  46. };
  47. $exec('git init');
  48. $exec('git config user.email composertest@example.org');
  49. $exec('git config user.name ComposerTest');
  50. touch('foo');
  51. $exec('git add foo');
  52. $exec('git commit -m init');
  53. // non-composed tag & branch
  54. $exec('git tag 0.5.0');
  55. $exec('git branch oldbranch');
  56. // add composed tag & master branch
  57. $composer = array('name' => 'a/b');
  58. file_put_contents('composer.json', json_encode($composer));
  59. $exec('git add composer.json');
  60. $exec('git commit -m addcomposer');
  61. $exec('git tag 0.6.0');
  62. // add feature-a branch
  63. $exec('git checkout -b feature/a-1.0-B');
  64. file_put_contents('foo', 'bar feature');
  65. $exec('git add foo');
  66. $exec('git commit -m change-a');
  67. // add version to composer.json
  68. $exec('git checkout master');
  69. $composer['version'] = '1.0.0';
  70. file_put_contents('composer.json', json_encode($composer));
  71. $exec('git add composer.json');
  72. $exec('git commit -m addversion');
  73. // create tag with wrong version in it
  74. $exec('git tag 0.9.0');
  75. // create tag with correct version in it
  76. $exec('git tag 1.0.0');
  77. // add feature-b branch
  78. $exec('git checkout -b feature-b');
  79. file_put_contents('foo', 'baz feature');
  80. $exec('git add foo');
  81. $exec('git commit -m change-b');
  82. // add 1.0 branch
  83. $exec('git checkout master');
  84. $exec('git branch 1.0');
  85. // add 1.0.x branch
  86. $exec('git branch 1.1.x');
  87. // update master to 2.0
  88. $composer['version'] = '2.0.0';
  89. file_put_contents('composer.json', json_encode($composer));
  90. $exec('git add composer.json');
  91. $exec('git commit -m bump-version');
  92. chdir($oldCwd);
  93. }
  94. public function setUp()
  95. {
  96. if (!self::$gitRepo) {
  97. $this->initialize();
  98. }
  99. if ($this->skipped) {
  100. $this->markTestSkipped($this->skipped);
  101. }
  102. }
  103. public static function tearDownAfterClass()
  104. {
  105. $fs = new Filesystem;
  106. $fs->removeDirectory(self::$gitRepo);
  107. }
  108. public function testLoadVersions()
  109. {
  110. $expected = array(
  111. '0.6.0' => true,
  112. '1.0.0' => true,
  113. '1.0.x-dev' => true,
  114. '1.1.x-dev' => true,
  115. 'dev-feature-b' => true,
  116. 'dev-feature/a-1.0-B' => true,
  117. 'dev-master' => true,
  118. );
  119. $repo = new VcsRepository(array('url' => self::$gitRepo, 'type' => 'vcs'), new NullIO, new Config());
  120. $packages = $repo->getPackages();
  121. $dumper = new ArrayDumper();
  122. foreach ($packages as $package) {
  123. if (isset($expected[$package->getPrettyVersion()])) {
  124. unset($expected[$package->getPrettyVersion()]);
  125. } else {
  126. $this->fail('Unexpected version '.$package->getPrettyVersion().' in '.json_encode($dumper->dump($package)));
  127. }
  128. }
  129. $this->assertEmpty($expected, 'Missing versions: '.implode(', ', array_keys($expected)));
  130. }
  131. }