VcsRepositoryTest.php 4.6 KB

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