SvnTest.php 3.9 KB

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