VcsRepositoryTest.php 4.5 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\Json;
  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. // update master to 2.0
  75. $composer['version'] = '2.0.0';
  76. file_put_contents('composer.json', json_encode($composer));
  77. $process->execute('git add composer.json', $null);
  78. $process->execute('git commit -m bump-version', $null);
  79. chdir($oldCwd);
  80. }
  81. public function setUp()
  82. {
  83. if (self::$skipped) {
  84. $this->markTestSkipped(self::$skipped);
  85. }
  86. }
  87. public static function tearDownAfterClass()
  88. {
  89. $fs = new Filesystem;
  90. $fs->removeDirectory(self::$gitRepo);
  91. }
  92. public function testLoadVersions()
  93. {
  94. $expected = array(
  95. '0.6.0' => true,
  96. '0.9.0' => true,
  97. '1.0.0' => true,
  98. 'dev-feature-b' => true,
  99. 'dev-feature-a' => true,
  100. 'dev-master' => true,
  101. );
  102. $repo = new VcsRepository(array('url' => self::$gitRepo), new NullIO);
  103. $packages = $repo->getPackages();
  104. $dumper = new ArrayDumper();
  105. foreach ($packages as $package) {
  106. if (isset($expected[$package->getPrettyVersion()])) {
  107. unset($expected[$package->getPrettyVersion()]);
  108. } else {
  109. $this->fail('Unexpected version '.$package->getPrettyVersion().' in '.json_encode($dumper->dump($package)));
  110. }
  111. }
  112. if ($expected) {
  113. $this->fail('Missing versions: '.implode(', ', $expected));
  114. }
  115. $this->pass();
  116. }
  117. }