RootPackageLoaderTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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\Package\Version\VersionGuesser;
  16. use Composer\Semver\VersionParser;
  17. use Composer\Test\TestCase;
  18. use Prophecy\Argument;
  19. class RootPackageLoaderTest extends TestCase
  20. {
  21. protected function loadPackage($data)
  22. {
  23. $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager')
  24. ->disableOriginalConstructor()
  25. ->getMock();
  26. $config = new Config;
  27. $config->merge(array('repositories' => array('packagist' => false)));
  28. $loader = new RootPackageLoader($manager, $config);
  29. return $loader->load($data);
  30. }
  31. public function testStabilityFlagsParsing()
  32. {
  33. $package = $this->loadPackage(array(
  34. 'require' => array(
  35. 'foo/bar' => '~2.1.0-beta2',
  36. 'bar/baz' => '1.0.x-dev as 1.2.0',
  37. 'qux/quux' => '1.0.*@rc',
  38. 'zux/complex' => '~1.0,>=1.0.2@dev',
  39. 'or/op' => '^2.0@dev || ^2.0@dev',
  40. 'multi/lowest-wins' => '^2.0@rc || >=3.0@dev , ~3.5@alpha',
  41. 'or/op-without-flags' => 'dev-master || 2.0 , ~3.5-alpha',
  42. 'or/op-without-flags2' => '3.0-beta || 2.0 , ~3.5-alpha',
  43. ),
  44. 'minimum-stability' => 'alpha',
  45. ));
  46. $this->assertEquals('alpha', $package->getMinimumStability());
  47. $this->assertEquals(array(
  48. 'bar/baz' => BasePackage::STABILITY_DEV,
  49. 'qux/quux' => BasePackage::STABILITY_RC,
  50. 'zux/complex' => BasePackage::STABILITY_DEV,
  51. 'or/op' => BasePackage::STABILITY_DEV,
  52. 'multi/lowest-wins' => BasePackage::STABILITY_DEV,
  53. 'or/op-without-flags' => BasePackage::STABILITY_DEV,
  54. 'or/op-without-flags2' => BasePackage::STABILITY_ALPHA,
  55. ), $package->getStabilityFlags());
  56. }
  57. public function testNoVersionIsVisibleInPrettyVersion()
  58. {
  59. $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager')
  60. ->disableOriginalConstructor()
  61. ->getMock()
  62. ;
  63. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  64. ->setMethods(array('execute'))
  65. ->disableArgumentCloning()
  66. ->disableOriginalConstructor()
  67. ->getMock()
  68. ;
  69. $executor
  70. ->expects($this->any())
  71. ->method('execute')
  72. ->willReturn(null)
  73. ;
  74. $config = new Config;
  75. $config->merge(array('repositories' => array('packagist' => false)));
  76. $loader = new RootPackageLoader($manager, $config, null, new VersionGuesser($config, $executor, new VersionParser()));
  77. $package = $loader->load(array());
  78. $this->assertEquals("1.0.0.0", $package->getVersion());
  79. $this->assertEquals("No version set (parsed as 1.0.0)", $package->getPrettyVersion());
  80. }
  81. public function testPrettyVersionForRootPackageInVersionBranch()
  82. {
  83. // see #6845
  84. $manager = $this->prophesize('\\Composer\\Repository\\RepositoryManager');
  85. $versionGuesser = $this->prophesize('\\Composer\\Package\\Version\\VersionGuesser');
  86. $versionGuesser->guessVersion(Argument::cetera())
  87. ->willReturn(array(
  88. 'name' => 'A',
  89. 'version' => '3.0.9999999.9999999-dev',
  90. 'pretty_version' => '3.0-dev',
  91. 'commit' => 'aabbccddee',
  92. ));
  93. $config = new Config;
  94. $config->merge(array('repositories' => array('packagist' => false)));
  95. $loader = new RootPackageLoader($manager->reveal(), $config, null, $versionGuesser->reveal());
  96. $package = $loader->load(array());
  97. $this->assertEquals('3.0-dev', $package->getPrettyVersion());
  98. }
  99. public function testFeatureBranchPrettyVersion()
  100. {
  101. if (!function_exists('proc_open')) {
  102. $this->markTestSkipped('proc_open() is not available');
  103. }
  104. $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager')
  105. ->disableOriginalConstructor()
  106. ->getMock()
  107. ;
  108. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  109. ->setMethods(array('execute'))
  110. ->disableArgumentCloning()
  111. ->disableOriginalConstructor()
  112. ->getMock()
  113. ;
  114. $self = $this;
  115. $executor
  116. ->expects($this->at(0))
  117. ->method('execute')
  118. ->willReturnCallback(function ($command, &$output) use ($self) {
  119. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  120. $output = "* latest-production 38137d2f6c70e775e137b2d8a7a7d3eaebf7c7e5 Commit message\n master 4f6ed96b0bc363d2aa4404c3412de1c011f67c66 Commit message\n";
  121. return 0;
  122. })
  123. ;
  124. $executor
  125. ->expects($this->at(1))
  126. ->method('execute')
  127. ->willReturnCallback(function ($command, &$output) use ($self) {
  128. $self->assertEquals('git rev-list master..latest-production', $command);
  129. $output = "";
  130. return 0;
  131. })
  132. ;
  133. $config = new Config;
  134. $config->merge(array('repositories' => array('packagist' => false)));
  135. $loader = new RootPackageLoader($manager, $config, null, new VersionGuesser($config, $executor, new VersionParser()));
  136. $package = $loader->load(array('require' => array('foo/bar' => 'self.version')));
  137. $this->assertEquals("9999999-dev", $package->getPrettyVersion());
  138. $package = $package->getAliasOf();
  139. $this->assertEquals("dev-master", $package->getPrettyVersion());
  140. }
  141. public function testNonFeatureBranchPrettyVersion()
  142. {
  143. if (!function_exists('proc_open')) {
  144. $this->markTestSkipped('proc_open() is not available');
  145. }
  146. $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager')
  147. ->disableOriginalConstructor()
  148. ->getMock()
  149. ;
  150. $executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
  151. ->setMethods(array('execute'))
  152. ->disableArgumentCloning()
  153. ->disableOriginalConstructor()
  154. ->getMock()
  155. ;
  156. $self = $this;
  157. $executor
  158. ->expects($this->at(0))
  159. ->method('execute')
  160. ->willReturnCallback(function ($command, &$output) use ($self) {
  161. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  162. $output = "* latest-production 38137d2f6c70e775e137b2d8a7a7d3eaebf7c7e5 Commit message\n master 4f6ed96b0bc363d2aa4404c3412de1c011f67c66 Commit message\n";
  163. return 0;
  164. })
  165. ;
  166. $config = new Config;
  167. $config->merge(array('repositories' => array('packagist' => false)));
  168. $loader = new RootPackageLoader($manager, $config, null, new VersionGuesser($config, $executor, new VersionParser()));
  169. $package = $loader->load(array('require' => array('foo/bar' => 'self.version'), "non-feature-branches" => array("latest-.*")));
  170. $this->assertEquals("dev-latest-production", $package->getPrettyVersion());
  171. }
  172. }