VcsRepositoryTest.php 4.7 KB

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