RootPackageLoaderTest.php 9.3 KB

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