RootPackageLoaderTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. ),
  111. 'minimum-stability' => 'alpha',
  112. ));
  113. $this->assertEquals('alpha', $package->getMinimumStability());
  114. $this->assertEquals(array(
  115. 'bar/baz' => BasePackage::STABILITY_DEV,
  116. 'qux/quux' => BasePackage::STABILITY_RC,
  117. ), $package->getStabilityFlags());
  118. }
  119. public function testFeatureBranchPrettyVersion()
  120. {
  121. if (!function_exists('proc_open')) {
  122. $this->markTestSkipped('proc_open() is not available');
  123. }
  124. $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager')
  125. ->disableOriginalConstructor()
  126. ->getMock();
  127. $self = $this;
  128. /* Can do away with this mock object when https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81 is fixed */
  129. $processExecutor = new ProcessExecutorMock(function($command, &$output = null, $cwd = null) use ($self) {
  130. if (0 === strpos($command, 'git rev-list')) {
  131. $output = "";
  132. return 0;
  133. }
  134. if ('git branch --no-color --no-abbrev -v' !== $command) {
  135. return 1; //0;
  136. }
  137. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  138. $output = "* latest-production 38137d2f6c70e775e137b2d8a7a7d3eaebf7c7e5 Commit message\n master 4f6ed96b0bc363d2aa4404c3412de1c011f67c66 Commit message\n";
  139. return 0;
  140. });
  141. $config = new Config;
  142. $config->merge(array('repositories' => array('packagist' => false)));
  143. $loader = new RootPackageLoader($manager, $config, null, $processExecutor);
  144. $package = $loader->load(array('require' => array('foo/bar' => 'self.version')));
  145. $this->assertEquals("dev-master", $package->getPrettyVersion());
  146. }
  147. public function testNonFeatureBranchPrettyVersion()
  148. {
  149. if (!function_exists('proc_open')) {
  150. $this->markTestSkipped('proc_open() is not available');
  151. }
  152. $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager')
  153. ->disableOriginalConstructor()
  154. ->getMock();
  155. $self = $this;
  156. /* Can do away with this mock object when https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81 is fixed */
  157. $processExecutor = new ProcessExecutorMock(function($command, &$output = null, $cwd = null) use ($self) {
  158. if (0 === strpos($command, 'git rev-list')) {
  159. $output = "";
  160. return 0;
  161. }
  162. if ('git branch --no-color --no-abbrev -v' !== $command) {
  163. return 1; //0;
  164. }
  165. $self->assertEquals('git branch --no-color --no-abbrev -v', $command);
  166. $output = "* latest-production 38137d2f6c70e775e137b2d8a7a7d3eaebf7c7e5 Commit message\n master 4f6ed96b0bc363d2aa4404c3412de1c011f67c66 Commit message\n";
  167. return 0;
  168. });
  169. $config = new Config;
  170. $config->merge(array('repositories' => array('packagist' => false)));
  171. $loader = new RootPackageLoader($manager, $config, null, $processExecutor);
  172. $package = $loader->load(array('require' => array('foo/bar' => 'self.version'), "non-feature-branches" => array("latest-.*")));
  173. $this->assertEquals("dev-latest-production", $package->getPrettyVersion());
  174. }
  175. }