RootPackageLoaderTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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\Loader;
  12. use Composer\Config;
  13. use Composer\Package\Loader\RootPackageLoader;
  14. use Composer\Package\BasePackage;
  15. use Composer\Test\Mock\ProcessExecutorMock;
  16. use Composer\Repository\RepositoryManager;
  17. class RootPackageLoaderTest extends \PHPUnit_Framework_TestCase
  18. {
  19. public function testDetachedHeadBecomesDevHash()
  20. {
  21. if (!function_exists('proc_open')) {
  22. $this->markTestSkipped('proc_open() is not available');
  23. }
  24. $commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
  25. $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager')
  26. ->disableOriginalConstructor()
  27. ->getMock();
  28. $self = $this;
  29. /* Can do away with this mock object when https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81 is fixed */
  30. $processExecutor = new ProcessExecutorMock(function ($command, &$output = null, $cwd = null) use ($self, $commitHash) {
  31. if (0 === strpos($command, 'git describe')) {
  32. // simulate not being on a tag
  33. return 1;
  34. }
  35. $self->assertStringStartsWith('git branch', $command);
  36. $output = "* (no branch) $commitHash Commit message\n";
  37. return 0;
  38. });
  39. $config = new Config;
  40. $config->merge(array('repositories' => array('packagist' => false)));
  41. $loader = new RootPackageLoader($manager, $config, null, $processExecutor);
  42. $package = $loader->load(array());
  43. $this->assertEquals("dev-$commitHash", $package->getVersion());
  44. }
  45. public function testTagBecomesVersion()
  46. {
  47. if (!function_exists('proc_open')) {
  48. $this->markTestSkipped('proc_open() is not available');
  49. }
  50. $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager')
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $self = $this;
  54. /* Can do away with this mock object when https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81 is fixed */
  55. $processExecutor = new ProcessExecutorMock(function ($command, &$output = null, $cwd = null) use ($self) {
  56. $self->assertEquals('git describe --exact-match --tags', $command);
  57. $output = "v2.0.5-alpha2";
  58. return 0;
  59. });
  60. $config = new Config;
  61. $config->merge(array('repositories' => array('packagist' => false)));
  62. $loader = new RootPackageLoader($manager, $config, null, $processExecutor);
  63. $package = $loader->load(array());
  64. $this->assertEquals("2.0.5.0-alpha2", $package->getVersion());
  65. }
  66. public function testInvalidTagBecomesVersion()
  67. {
  68. if (!function_exists('proc_open')) {
  69. $this->markTestSkipped('proc_open() is not available');
  70. }
  71. $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager')
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $self = $this;
  75. /* Can do away with this mock object when https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81 is fixed */
  76. $processExecutor = new ProcessExecutorMock(function ($command, &$output = null, $cwd = null) use ($self) {
  77. if ('git describe --exact-match --tags' === $command) {
  78. $output = "foo-bar";
  79. return 0;
  80. }
  81. $output = "* foo 03a15d220da53c52eddd5f32ffca64a7b3801bea Commit message\n";
  82. return 0;
  83. });
  84. $config = new Config;
  85. $config->merge(array('repositories' => array('packagist' => false)));
  86. $loader = new RootPackageLoader($manager, $config, null, $processExecutor);
  87. $package = $loader->load(array());
  88. $this->assertEquals("dev-foo", $package->getVersion());
  89. }
  90. protected function loadPackage($data)
  91. {
  92. $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager')
  93. ->disableOriginalConstructor()
  94. ->getMock();
  95. $processExecutor = new ProcessExecutorMock(function ($command, &$output = null, $cwd = null) {
  96. return 1;
  97. });
  98. $config = new Config;
  99. $config->merge(array('repositories' => array('packagist' => false)));
  100. $loader = new RootPackageLoader($manager, $config);
  101. return $loader->load($data);
  102. }
  103. public function testStabilityFlagsParsing()
  104. {
  105. $package = $this->loadPackage(array(
  106. 'require' => array(
  107. 'foo/bar' => '~2.1.0-beta2',
  108. 'bar/baz' => '1.0.x-dev as 1.2.0',
  109. 'qux/quux' => '1.0.*@rc',
  110. 'zux/complex' => '~1.0,>=1.0.2@dev'
  111. ),
  112. 'minimum-stability' => 'alpha',
  113. ));
  114. $this->assertEquals('alpha', $package->getMinimumStability());
  115. $this->assertEquals(array(
  116. 'bar/baz' => BasePackage::STABILITY_DEV,
  117. 'qux/quux' => BasePackage::STABILITY_RC,
  118. 'zux/complex' => BasePackage::STABILITY_DEV,
  119. ), $package->getStabilityFlags());
  120. }
  121. public function testFeatureBranchPrettyVersion()
  122. {
  123. if (!function_exists('proc_open')) {
  124. $this->markTestSkipped('proc_open() is not available');
  125. }
  126. $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager')
  127. ->disableOriginalConstructor()
  128. ->getMock();
  129. $self = $this;
  130. /* Can do away with this mock object when https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81 is fixed */
  131. $processExecutor = new ProcessExecutorMock(function ($command, &$output = null, $cwd = null) use ($self) {
  132. if (0 === strpos($command, 'git rev-list')) {
  133. $output = "";
  134. return 0;
  135. }
  136. if ('git branch --no-color --no-abbrev -v' !== $command) {
  137. return 1; //0;
  138. }
  139. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  140. $output = "* latest-production 38137d2f6c70e775e137b2d8a7a7d3eaebf7c7e5 Commit message\n master 4f6ed96b0bc363d2aa4404c3412de1c011f67c66 Commit message\n";
  141. return 0;
  142. });
  143. $config = new Config;
  144. $config->merge(array('repositories' => array('packagist' => false)));
  145. $loader = new RootPackageLoader($manager, $config, null, $processExecutor);
  146. $package = $loader->load(array('require' => array('foo/bar' => 'self.version')));
  147. $this->assertEquals("dev-master", $package->getPrettyVersion());
  148. }
  149. public function testNonFeatureBranchPrettyVersion()
  150. {
  151. if (!function_exists('proc_open')) {
  152. $this->markTestSkipped('proc_open() is not available');
  153. }
  154. $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager')
  155. ->disableOriginalConstructor()
  156. ->getMock();
  157. $self = $this;
  158. /* Can do away with this mock object when https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81 is fixed */
  159. $processExecutor = new ProcessExecutorMock(function ($command, &$output = null, $cwd = null) use ($self) {
  160. if (0 === strpos($command, 'git rev-list')) {
  161. $output = "";
  162. return 0;
  163. }
  164. if ('git branch --no-color --no-abbrev -v' !== $command) {
  165. return 1; //0;
  166. }
  167. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  168. $output = "* latest-production 38137d2f6c70e775e137b2d8a7a7d3eaebf7c7e5 Commit message\n master 4f6ed96b0bc363d2aa4404c3412de1c011f67c66 Commit message\n";
  169. return 0;
  170. });
  171. $config = new Config;
  172. $config->merge(array('repositories' => array('packagist' => false)));
  173. $loader = new RootPackageLoader($manager, $config, null, $processExecutor);
  174. $package = $loader->load(array('require' => array('foo/bar' => 'self.version'), "non-feature-branches" => array("latest-.*")));
  175. $this->assertEquals("dev-latest-production", $package->getPrettyVersion());
  176. }
  177. }