RootPackageLoaderTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. public function testNoVersionIsVisibleInPrettyVersion()
  91. {
  92. $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager')
  93. ->disableOriginalConstructor()
  94. ->getMock();
  95. $self = $this;
  96. /* Can do away with this mock object when https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81 is fixed */
  97. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')
  98. ->disableOriginalConstructor()
  99. ->getMock();
  100. $processExecutor->expects($this->any())
  101. ->method('execute')
  102. ->willReturn(null);
  103. $config = new Config;
  104. $config->merge(array('repositories' => array('packagist' => false)));
  105. $loader = new RootPackageLoader($manager, $config, null, $processExecutor);
  106. $package = $loader->load(array());
  107. $this->assertEquals("1.0.0.0", $package->getVersion());
  108. $this->assertEquals("No version set (parsed as 1.0.0)", $package->getPrettyVersion());
  109. }
  110. protected function loadPackage($data)
  111. {
  112. $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager')
  113. ->disableOriginalConstructor()
  114. ->getMock();
  115. $processExecutor = new ProcessExecutorMock(function ($command, &$output = null, $cwd = null) {
  116. return 1;
  117. });
  118. $config = new Config;
  119. $config->merge(array('repositories' => array('packagist' => false)));
  120. $loader = new RootPackageLoader($manager, $config);
  121. return $loader->load($data);
  122. }
  123. public function testStabilityFlagsParsing()
  124. {
  125. $package = $this->loadPackage(array(
  126. 'require' => array(
  127. 'foo/bar' => '~2.1.0-beta2',
  128. 'bar/baz' => '1.0.x-dev as 1.2.0',
  129. 'qux/quux' => '1.0.*@rc',
  130. 'zux/complex' => '~1.0,>=1.0.2@dev'
  131. ),
  132. 'minimum-stability' => 'alpha',
  133. ));
  134. $this->assertEquals('alpha', $package->getMinimumStability());
  135. $this->assertEquals(array(
  136. 'bar/baz' => BasePackage::STABILITY_DEV,
  137. 'qux/quux' => BasePackage::STABILITY_RC,
  138. 'zux/complex' => BasePackage::STABILITY_DEV,
  139. ), $package->getStabilityFlags());
  140. }
  141. public function testFeatureBranchPrettyVersion()
  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. $self = $this;
  150. /* Can do away with this mock object when https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81 is fixed */
  151. $processExecutor = new ProcessExecutorMock(function ($command, &$output = null, $cwd = null) use ($self) {
  152. if (0 === strpos($command, 'git rev-list')) {
  153. $output = "";
  154. return 0;
  155. }
  156. if ('git branch --no-color --no-abbrev -v' !== $command) {
  157. return 1; //0;
  158. }
  159. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  160. $output = "* latest-production 38137d2f6c70e775e137b2d8a7a7d3eaebf7c7e5 Commit message\n master 4f6ed96b0bc363d2aa4404c3412de1c011f67c66 Commit message\n";
  161. return 0;
  162. });
  163. $config = new Config;
  164. $config->merge(array('repositories' => array('packagist' => false)));
  165. $loader = new RootPackageLoader($manager, $config, null, $processExecutor);
  166. $package = $loader->load(array('require' => array('foo/bar' => 'self.version')));
  167. $this->assertEquals("dev-master", $package->getPrettyVersion());
  168. }
  169. public function testNonFeatureBranchPrettyVersion()
  170. {
  171. if (!function_exists('proc_open')) {
  172. $this->markTestSkipped('proc_open() is not available');
  173. }
  174. $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager')
  175. ->disableOriginalConstructor()
  176. ->getMock();
  177. $self = $this;
  178. /* Can do away with this mock object when https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81 is fixed */
  179. $processExecutor = new ProcessExecutorMock(function ($command, &$output = null, $cwd = null) use ($self) {
  180. if (0 === strpos($command, 'git rev-list')) {
  181. $output = "";
  182. return 0;
  183. }
  184. if ('git branch --no-color --no-abbrev -v' !== $command) {
  185. return 1; //0;
  186. }
  187. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  188. $output = "* latest-production 38137d2f6c70e775e137b2d8a7a7d3eaebf7c7e5 Commit message\n master 4f6ed96b0bc363d2aa4404c3412de1c011f67c66 Commit message\n";
  189. return 0;
  190. });
  191. $config = new Config;
  192. $config->merge(array('repositories' => array('packagist' => false)));
  193. $loader = new RootPackageLoader($manager, $config, null, $processExecutor);
  194. $package = $loader->load(array('require' => array('foo/bar' => 'self.version'), "non-feature-branches" => array("latest-.*")));
  195. $this->assertEquals("dev-latest-production", $package->getPrettyVersion());
  196. }
  197. }