SvnTest.php 4.1 KB

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