ConfigTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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;
  12. use Composer\Config;
  13. class ConfigTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @dataProvider dataAddPackagistRepository
  17. */
  18. public function testAddPackagistRepository($expected, $localConfig, $systemConfig = null)
  19. {
  20. $config = new Config(false);
  21. if ($systemConfig) {
  22. $config->merge(array('repositories' => $systemConfig));
  23. }
  24. $config->merge(array('repositories' => $localConfig));
  25. $this->assertEquals($expected, $config->getRepositories());
  26. }
  27. public function dataAddPackagistRepository()
  28. {
  29. $data = array();
  30. $data['local config inherits system defaults'] = array(
  31. array(
  32. 'packagist' => array('type' => 'composer', 'url' => 'https?://packagist.org', 'allow_ssl_downgrade' => true)
  33. ),
  34. array(),
  35. );
  36. $data['local config can disable system config by name'] = array(
  37. array(),
  38. array(
  39. array('packagist' => false),
  40. )
  41. );
  42. $data['local config adds above defaults'] = array(
  43. array(
  44. 1 => array('type' => 'vcs', 'url' => 'git://github.com/composer/composer.git'),
  45. 0 => array('type' => 'pear', 'url' => 'http://pear.composer.org'),
  46. 'packagist' => array('type' => 'composer', 'url' => 'https?://packagist.org', 'allow_ssl_downgrade' => true),
  47. ),
  48. array(
  49. array('type' => 'vcs', 'url' => 'git://github.com/composer/composer.git'),
  50. array('type' => 'pear', 'url' => 'http://pear.composer.org'),
  51. ),
  52. );
  53. $data['system config adds above core defaults'] = array(
  54. array(
  55. 'example.com' => array('type' => 'composer', 'url' => 'http://example.com'),
  56. 'packagist' => array('type' => 'composer', 'url' => 'https?://packagist.org', 'allow_ssl_downgrade' => true)
  57. ),
  58. array(),
  59. array(
  60. 'example.com' => array('type' => 'composer', 'url' => 'http://example.com'),
  61. ),
  62. );
  63. $data['local config can disable repos by name and re-add them anonymously to bring them above system config'] = array(
  64. array(
  65. 0 => array('type' => 'composer', 'url' => 'http://packagist.org'),
  66. 'example.com' => array('type' => 'composer', 'url' => 'http://example.com')
  67. ),
  68. array(
  69. array('packagist' => false),
  70. array('type' => 'composer', 'url' => 'http://packagist.org')
  71. ),
  72. array(
  73. 'example.com' => array('type' => 'composer', 'url' => 'http://example.com'),
  74. ),
  75. );
  76. $data['local config can override by name to bring a repo above system config'] = array(
  77. array(
  78. 'packagist' => array('type' => 'composer', 'url' => 'http://packagistnew.org'),
  79. 'example.com' => array('type' => 'composer', 'url' => 'http://example.com')
  80. ),
  81. array(
  82. 'packagist' => array('type' => 'composer', 'url' => 'http://packagistnew.org')
  83. ),
  84. array(
  85. 'example.com' => array('type' => 'composer', 'url' => 'http://example.com'),
  86. ),
  87. );
  88. return $data;
  89. }
  90. public function testMergeGithubOauth()
  91. {
  92. $config = new Config(false);
  93. $config->merge(array('config' => array('github-oauth' => array('foo' => 'bar'))));
  94. $config->merge(array('config' => array('github-oauth' => array('bar' => 'baz'))));
  95. $this->assertEquals(array('foo' => 'bar', 'bar' => 'baz'), $config->get('github-oauth'));
  96. }
  97. public function testVarReplacement()
  98. {
  99. $config = new Config(false);
  100. $config->merge(array('config' => array('a' => 'b', 'c' => '{$a}')));
  101. $config->merge(array('config' => array('bin-dir' => '$HOME', 'cache-dir' => '~/foo/')));
  102. $home = rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '\\/');
  103. $this->assertEquals('b', $config->get('c'));
  104. $this->assertEquals($home.'/', $config->get('bin-dir'));
  105. $this->assertEquals($home.'/foo', $config->get('cache-dir'));
  106. }
  107. public function testRealpathReplacement()
  108. {
  109. $config = new Config(false, '/foo/bar');
  110. $config->merge(array('config' => array(
  111. 'bin-dir' => '$HOME/foo',
  112. 'cache-dir' => '/baz/',
  113. 'vendor-dir' => 'vendor'
  114. )));
  115. $home = rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '\\/');
  116. $this->assertEquals('/foo/bar/vendor', $config->get('vendor-dir'));
  117. $this->assertEquals($home.'/foo', $config->get('bin-dir'));
  118. $this->assertEquals('/baz', $config->get('cache-dir'));
  119. }
  120. public function testFetchingRelativePaths()
  121. {
  122. $config = new Config(false, '/foo/bar');
  123. $config->merge(array('config' => array(
  124. 'bin-dir' => '{$vendor-dir}/foo',
  125. 'vendor-dir' => 'vendor'
  126. )));
  127. $this->assertEquals('/foo/bar/vendor', $config->get('vendor-dir'));
  128. $this->assertEquals('/foo/bar/vendor/foo', $config->get('bin-dir'));
  129. $this->assertEquals('vendor', $config->get('vendor-dir', Config::RELATIVE_PATHS));
  130. $this->assertEquals('vendor/foo', $config->get('bin-dir', Config::RELATIVE_PATHS));
  131. }
  132. public function testOverrideGithubProtocols()
  133. {
  134. $config = new Config(false);
  135. $config->merge(array('config' => array('github-protocols' => array('https', 'git'))));
  136. $config->merge(array('config' => array('github-protocols' => array('https'))));
  137. $this->assertEquals(array('https'), $config->get('github-protocols'));
  138. }
  139. }