VersionGuesserTest.php 4.1 KB

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