SvnTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\Util;
  12. use Composer\Config;
  13. use Composer\IO\NullIO;
  14. use Composer\Util\Svn;
  15. class SvnTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * Test the credential string.
  19. *
  20. * @param string $url The SVN url.
  21. * @param string $expect The expectation for the test.
  22. *
  23. * @dataProvider urlProvider
  24. */
  25. public function testCredentials($url, $expect)
  26. {
  27. $svn = new Svn($url, new NullIO, new Config());
  28. $reflMethod = new \ReflectionMethod('Composer\\Util\\Svn', 'getCredentialString');
  29. $reflMethod->setAccessible(true);
  30. $this->assertEquals($expect, $reflMethod->invoke($svn));
  31. }
  32. /**
  33. * Provide some examples for {@self::testCredentials()}.
  34. *
  35. * @return array
  36. */
  37. public function urlProvider()
  38. {
  39. return array(
  40. array('http://till:test@svn.example.org/', $this->getCmd(" --username 'till' --password 'test' ")),
  41. array('http://svn.apache.org/', ''),
  42. array('svn://johndoe@example.org', $this->getCmd(" --username 'johndoe' --password '' ")),
  43. );
  44. }
  45. public function testInteractiveString()
  46. {
  47. $url = 'http://svn.example.org';
  48. $svn = new Svn($url, new NullIO(), new Config());
  49. $reflMethod = new \ReflectionMethod('Composer\\Util\\Svn', 'getCommand');
  50. $reflMethod->setAccessible(true);
  51. $this->assertEquals(
  52. $this->getCmd("svn ls --non-interactive 'http://svn.example.org'"),
  53. $reflMethod->invokeArgs($svn, array('svn ls', $url))
  54. );
  55. }
  56. public function testCredentialsFromConfig()
  57. {
  58. $url = 'http://svn.apache.org';
  59. $config = new Config();
  60. $config->merge(array(
  61. 'config' => array(
  62. 'http-basic' => array(
  63. 'svn.apache.org' => array('username' => 'foo', 'password' => 'bar'),
  64. ),
  65. ),
  66. ));
  67. $svn = new Svn($url, new NullIO, $config);
  68. $reflMethod = new \ReflectionMethod('Composer\\Util\\Svn', 'getCredentialString');
  69. $reflMethod->setAccessible(true);
  70. $this->assertEquals($this->getCmd(" --username 'foo' --password 'bar' "), $reflMethod->invoke($svn));
  71. }
  72. public function testCredentialsFromConfigWithCacheCredentialsTrue()
  73. {
  74. $url = 'http://svn.apache.org';
  75. $config = new Config();
  76. $config->merge(
  77. array(
  78. 'config' => array(
  79. 'http-basic' => array(
  80. 'svn.apache.org' => array('username' => 'foo', 'password' => 'bar'),
  81. ),
  82. ),
  83. )
  84. );
  85. $svn = new Svn($url, new NullIO, $config);
  86. $svn->setCacheCredentials(true);
  87. $reflMethod = new \ReflectionMethod('Composer\\Util\\Svn', 'getCredentialString');
  88. $reflMethod->setAccessible(true);
  89. $this->assertEquals($this->getCmd(" --username 'foo' --password 'bar' "), $reflMethod->invoke($svn));
  90. }
  91. public function testCredentialsFromConfigWithCacheCredentialsFalse()
  92. {
  93. $url = 'http://svn.apache.org';
  94. $config = new Config();
  95. $config->merge(
  96. array(
  97. 'config' => array(
  98. 'http-basic' => array(
  99. 'svn.apache.org' => array('username' => 'foo', 'password' => 'bar'),
  100. ),
  101. ),
  102. )
  103. );
  104. $svn = new Svn($url, new NullIO, $config);
  105. $svn->setCacheCredentials(false);
  106. $reflMethod = new \ReflectionMethod('Composer\\Util\\Svn', 'getCredentialString');
  107. $reflMethod->setAccessible(true);
  108. $this->assertEquals($this->getCmd(" --no-auth-cache --username 'foo' --password 'bar' "), $reflMethod->invoke($svn));
  109. }
  110. private function getCmd($cmd)
  111. {
  112. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  113. return strtr($cmd, "'", '"');
  114. }
  115. return $cmd;
  116. }
  117. }