ConfigTest.php 5.5 KB

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