VcsRepositoryTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. touch('foo');
  49. $exec('git add foo');
  50. $exec('git commit -m init');
  51. // non-composed tag & branch
  52. $exec('git tag 0.5.0');
  53. $exec('git branch oldbranch');
  54. // add composed tag & master branch
  55. $composer = array('name' => 'a/b');
  56. file_put_contents('composer.json', json_encode($composer));
  57. $exec('git add composer.json');
  58. $exec('git commit -m addcomposer');
  59. $exec('git tag 0.6.0');
  60. // add feature-a branch
  61. $exec('git checkout -b feature/a-1.0-B');
  62. file_put_contents('foo', 'bar feature');
  63. $exec('git add foo');
  64. $exec('git commit -m change-a');
  65. // add version to composer.json
  66. $exec('git checkout master');
  67. $composer['version'] = '1.0.0';
  68. file_put_contents('composer.json', json_encode($composer));
  69. $exec('git add composer.json');
  70. $exec('git commit -m addversion');
  71. // create tag with wrong version in it
  72. $exec('git tag 0.9.0');
  73. // create tag with correct version in it
  74. $exec('git tag 1.0.0');
  75. // add feature-b branch
  76. $exec('git checkout -b feature-b');
  77. file_put_contents('foo', 'baz feature');
  78. $exec('git add foo');
  79. $exec('git commit -m change-b');
  80. // add 1.0 branch
  81. $exec('git checkout master');
  82. $exec('git branch 1.0');
  83. // add 1.0.x branch
  84. $exec('git branch 1.1.x');
  85. // update master to 2.0
  86. $composer['version'] = '2.0.0';
  87. file_put_contents('composer.json', json_encode($composer));
  88. $exec('git add composer.json');
  89. $exec('git commit -m bump-version');
  90. chdir($oldCwd);
  91. }
  92. public function setUp()
  93. {
  94. if (!self::$gitRepo) {
  95. $this->initialize();
  96. }
  97. if ($this->skipped) {
  98. $this->markTestSkipped($this->skipped);
  99. }
  100. }
  101. public static function tearDownAfterClass()
  102. {
  103. $fs = new Filesystem;
  104. $fs->removeDirectory(self::$gitRepo);
  105. }
  106. public function testLoadVersions()
  107. {
  108. $expected = array(
  109. '0.6.0' => true,
  110. '1.0.0' => true,
  111. '1.0.x-dev' => true,
  112. '1.1.x-dev' => true,
  113. 'dev-feature-b' => true,
  114. 'dev-feature/a-1.0-B' => true,
  115. 'dev-master' => true,
  116. );
  117. $repo = new VcsRepository(array('url' => self::$gitRepo, 'type' => 'vcs'), new NullIO, new Config());
  118. $packages = $repo->getPackages();
  119. $dumper = new ArrayDumper();
  120. foreach ($packages as $package) {
  121. if (isset($expected[$package->getPrettyVersion()])) {
  122. unset($expected[$package->getPrettyVersion()]);
  123. } else {
  124. $this->fail('Unexpected version '.$package->getPrettyVersion().' in '.json_encode($dumper->dump($package)));
  125. }
  126. }
  127. $this->assertEmpty($expected, 'Missing versions: '.implode(', ', array_keys($expected)));
  128. }
  129. }