SvnTest.php 4.1 KB

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