VcsRepositoryTest.php 4.7 KB

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