VcsRepositoryTest.php 4.6 KB

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