ConfigTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. $data['incorrect local config does not cause ErrorException'] = array(
  89. array(
  90. 'packagist' => array('type' => 'composer', 'url' => 'https?://packagist.org', 'allow_ssl_downgrade' => true),
  91. 'type' => 'vcs',
  92. 'url' => 'http://example.com',
  93. ),
  94. array(
  95. 'type' => 'vcs',
  96. 'url' => 'http://example.com',
  97. ),
  98. );
  99. return $data;
  100. }
  101. public function testMergeGithubOauth()
  102. {
  103. $config = new Config(false);
  104. $config->merge(array('config' => array('github-oauth' => array('foo' => 'bar'))));
  105. $config->merge(array('config' => array('github-oauth' => array('bar' => 'baz'))));
  106. $this->assertEquals(array('foo' => 'bar', 'bar' => 'baz'), $config->get('github-oauth'));
  107. }
  108. public function testVarReplacement()
  109. {
  110. $config = new Config(false);
  111. $config->merge(array('config' => array('a' => 'b', 'c' => '{$a}')));
  112. $config->merge(array('config' => array('bin-dir' => '$HOME', 'cache-dir' => '~/foo/')));
  113. $home = rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '\\/');
  114. $this->assertEquals('b', $config->get('c'));
  115. $this->assertEquals($home.'/', $config->get('bin-dir'));
  116. $this->assertEquals($home.'/foo', $config->get('cache-dir'));
  117. }
  118. public function testRealpathReplacement()
  119. {
  120. $config = new Config(false, '/foo/bar');
  121. $config->merge(array('config' => array(
  122. 'bin-dir' => '$HOME/foo',
  123. 'cache-dir' => '/baz/',
  124. 'vendor-dir' => 'vendor',
  125. )));
  126. $home = rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '\\/');
  127. $this->assertEquals('/foo/bar/vendor', $config->get('vendor-dir'));
  128. $this->assertEquals($home.'/foo', $config->get('bin-dir'));
  129. $this->assertEquals('/baz', $config->get('cache-dir'));
  130. }
  131. public function testFetchingRelativePaths()
  132. {
  133. $config = new Config(false, '/foo/bar');
  134. $config->merge(array('config' => array(
  135. 'bin-dir' => '{$vendor-dir}/foo',
  136. 'vendor-dir' => 'vendor',
  137. )));
  138. $this->assertEquals('/foo/bar/vendor', $config->get('vendor-dir'));
  139. $this->assertEquals('/foo/bar/vendor/foo', $config->get('bin-dir'));
  140. $this->assertEquals('vendor', $config->get('vendor-dir', Config::RELATIVE_PATHS));
  141. $this->assertEquals('vendor/foo', $config->get('bin-dir', Config::RELATIVE_PATHS));
  142. }
  143. public function testOverrideGithubProtocols()
  144. {
  145. $config = new Config(false);
  146. $config->merge(array('config' => array('github-protocols' => array('https', 'git'))));
  147. $config->merge(array('config' => array('github-protocols' => array('https'))));
  148. $this->assertEquals(array('https'), $config->get('github-protocols'));
  149. }
  150. }