VersionGuesserTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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\Package\Version;
  12. use Composer\Config;
  13. use Composer\Package\Loader\RootPackageLoader;
  14. use Composer\Package\Version\VersionGuesser;
  15. use Composer\Package\Version\VersionParser;
  16. class VersionGuesserTest extends \PHPUnit_Framework_TestCase
  17. {
  18. public function testDetachedHeadBecomesDevHash()
  19. {
  20. if (!function_exists('proc_open')) {
  21. $this->markTestSkipped('proc_open() is not available');
  22. }
  23. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  24. $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager')
  25. ->disableOriginalConstructor()
  26. ->getMock()
  27. ;
  28. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  29. ->setMethods(array('execute'))
  30. ->disableArgumentCloning()
  31. ->disableOriginalConstructor()
  32. ->getMock()
  33. ;
  34. $executor
  35. ->expects($this->at(0))
  36. ->method('execute')
  37. ->with('git describe --exact-match --tags')
  38. ->willReturn(1)
  39. ;
  40. $self = $this;
  41. $executor
  42. ->expects($this->at(1))
  43. ->method('execute')
  44. ->willReturnCallback(function ($command, &$output) use ($self, $commitHash) {
  45. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  46. $output = "* (no branch) $commitHash Commit message\n";
  47. return 0;
  48. })
  49. ;
  50. $config = new Config;
  51. $config->merge(array('repositories' => array('packagist' => false)));
  52. $guesser = new VersionGuesser($executor, new VersionParser());
  53. $version = $guesser->guessVersion($config, []);
  54. $this->assertEquals("dev-$commitHash", $version);
  55. }
  56. public function testTagBecomesVersion()
  57. {
  58. if (!function_exists('proc_open')) {
  59. $this->markTestSkipped('proc_open() is not available');
  60. }
  61. $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager')
  62. ->disableOriginalConstructor()
  63. ->getMock()
  64. ;
  65. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  66. ->setMethods(array('execute'))
  67. ->disableArgumentCloning()
  68. ->disableOriginalConstructor()
  69. ->getMock()
  70. ;
  71. $self = $this;
  72. $executor
  73. ->expects($this->at(0))
  74. ->method('execute')
  75. ->willReturnCallback(function ($command, &$output) use ($self) {
  76. $self->assertEquals('git describe --exact-match --tags', $command);
  77. $output = "v2.0.5-alpha2";
  78. return 0;
  79. })
  80. ;
  81. $config = new Config;
  82. $config->merge(array('repositories' => array('packagist' => false)));
  83. $guesser = new VersionGuesser($executor, new VersionParser());
  84. $version = $guesser->guessVersion($config, []);
  85. $this->assertEquals("2.0.5.0-alpha2", $version);
  86. }
  87. public function testInvalidTagBecomesVersion()
  88. {
  89. if (!function_exists('proc_open')) {
  90. $this->markTestSkipped('proc_open() is not available');
  91. }
  92. $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager')
  93. ->disableOriginalConstructor()
  94. ->getMock()
  95. ;
  96. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  97. ->setMethods(array('execute'))
  98. ->disableArgumentCloning()
  99. ->disableOriginalConstructor()
  100. ->getMock()
  101. ;
  102. $self = $this;
  103. $executor
  104. ->expects($this->at(0))
  105. ->method('execute')
  106. ->willReturnCallback(function ($command, &$output) use ($self) {
  107. $self->assertEquals('git describe --exact-match --tags', $command);
  108. $output = "foo-bar";
  109. return 0;
  110. })
  111. ;
  112. $executor
  113. ->expects($this->at(1))
  114. ->method('execute')
  115. ->willReturnCallback(function ($command, &$output) use ($self) {
  116. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  117. $output = "* foo 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n";
  118. return 0;
  119. })
  120. ;
  121. $config = new Config;
  122. $config->merge(array('repositories' => array('packagist' => false)));
  123. $guesser = new VersionGuesser($executor, new VersionParser());
  124. $version = $guesser->guessVersion($config, []);
  125. $this->assertEquals("dev-foo", $version);
  126. }
  127. }