RootPackageLoaderTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. protected function loadPackage($data)
  67. {
  68. $manager = $this->getMockBuilder('\\Composer\\Repository\\RepositoryManager')
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $processExecutor = new ProcessExecutorMock(function($command, &$output = null, $cwd = null) {
  72. return 1;
  73. });
  74. $config = new Config;
  75. $config->merge(array('repositories' => array('packagist' => false)));
  76. $loader = new RootPackageLoader($manager, $config);
  77. return $loader->load($data);
  78. }
  79. public function testStabilityFlagsParsing()
  80. {
  81. $package = $this->loadPackage(array(
  82. 'require' => array(
  83. 'foo/bar' => '~2.1.0-beta2',
  84. 'bar/baz' => '1.0.x-dev as 1.2.0',
  85. 'qux/quux' => '1.0.*@rc',
  86. ),
  87. 'minimum-stability' => 'alpha',
  88. ));
  89. $this->assertEquals('alpha', $package->getMinimumStability());
  90. $this->assertEquals(array(
  91. 'bar/baz' => BasePackage::STABILITY_DEV,
  92. 'qux/quux' => BasePackage::STABILITY_RC,
  93. ), $package->getStabilityFlags());
  94. }
  95. }