RootPackageLoaderTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. }