GitHubTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\Downloader\TransportException;
  13. use Composer\Util\GitHub;
  14. use RecursiveArrayIterator;
  15. use RecursiveIteratorIterator;
  16. /**
  17. * @author Rob Bast <rob.bast@gmail.com>
  18. */
  19. class GitHubTest extends \PHPUnit_Framework_TestCase
  20. {
  21. private $username = 'username';
  22. private $password = 'password';
  23. private $authcode = 'authcode';
  24. private $message = 'mymessage';
  25. private $origin = 'github.com';
  26. private $token = 'githubtoken';
  27. public function testUsernamePasswordAuthenticationFlow()
  28. {
  29. $io = $this->getIOMock();
  30. $io
  31. ->expects($this->at(0))
  32. ->method('writeError')
  33. ->with($this->message)
  34. ;
  35. $io
  36. ->expects($this->once())
  37. ->method('askAndHideAnswer')
  38. ->with('Token (hidden): ')
  39. ->willReturn($this->password)
  40. ;
  41. $rfs = $this->getRemoteFilesystemMock();
  42. $rfs
  43. ->expects($this->once())
  44. ->method('getContents')
  45. ->with(
  46. $this->equalTo($this->origin),
  47. $this->equalTo(sprintf('https://api.%s/rate_limit', $this->origin)),
  48. $this->isFalse(),
  49. $this->anything()
  50. )
  51. ->willReturn(sprintf('{}', $this->token))
  52. ;
  53. $config = $this->getConfigMock();
  54. $config
  55. ->expects($this->exactly(2))
  56. ->method('getAuthConfigSource')
  57. ->willReturn($this->getAuthJsonMock())
  58. ;
  59. $config
  60. ->expects($this->once())
  61. ->method('getConfigSource')
  62. ->willReturn($this->getConfJsonMock())
  63. ;
  64. $github = new GitHub($io, $config, null, $rfs);
  65. $this->assertTrue($github->authorizeOAuthInteractively($this->origin, $this->message));
  66. }
  67. public function testUsernamePasswordFailure()
  68. {
  69. $io = $this->getIOMock();
  70. $io
  71. ->expects($this->exactly(1))
  72. ->method('askAndHideAnswer')
  73. ->with('Token (hidden): ')
  74. ->willReturn($this->password)
  75. ;
  76. $rfs = $this->getRemoteFilesystemMock();
  77. $rfs
  78. ->expects($this->exactly(1))
  79. ->method('getContents')
  80. ->will($this->throwException(new TransportException('', 401)))
  81. ;
  82. $config = $this->getConfigMock();
  83. $config
  84. ->expects($this->exactly(1))
  85. ->method('getAuthConfigSource')
  86. ->willReturn($this->getAuthJsonMock())
  87. ;
  88. $github = new GitHub($io, $config, null, $rfs);
  89. $this->assertFalse($github->authorizeOAuthInteractively($this->origin));
  90. }
  91. private function getIOMock()
  92. {
  93. $io = $this
  94. ->getMockBuilder('Composer\IO\ConsoleIO')
  95. ->disableOriginalConstructor()
  96. ->getMock()
  97. ;
  98. return $io;
  99. }
  100. private function getConfigMock()
  101. {
  102. $config = $this->getMock('Composer\Config');
  103. return $config;
  104. }
  105. private function getRemoteFilesystemMock()
  106. {
  107. $rfs = $this
  108. ->getMockBuilder('Composer\Util\RemoteFilesystem')
  109. ->disableOriginalConstructor()
  110. ->getMock()
  111. ;
  112. return $rfs;
  113. }
  114. private function getAuthJsonMock()
  115. {
  116. $authjson = $this
  117. ->getMockBuilder('Composer\Config\JsonConfigSource')
  118. ->disableOriginalConstructor()
  119. ->getMock()
  120. ;
  121. $authjson
  122. ->expects($this->atLeastOnce())
  123. ->method('getName')
  124. ->willReturn('auth.json')
  125. ;
  126. return $authjson;
  127. }
  128. private function getConfJsonMock()
  129. {
  130. $confjson = $this
  131. ->getMockBuilder('Composer\Config\JsonConfigSource')
  132. ->disableOriginalConstructor()
  133. ->getMock()
  134. ;
  135. $confjson
  136. ->expects($this->atLeastOnce())
  137. ->method('removeConfigSetting')
  138. ->with('github-oauth.'.$this->origin)
  139. ;
  140. return $confjson;
  141. }
  142. public static function recursiveFind($array, $needle)
  143. {
  144. $iterator = new RecursiveArrayIterator($array);
  145. $recursive = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
  146. foreach ($recursive as $key => $value) {
  147. if ($key === $needle) {
  148. return $value;
  149. }
  150. }
  151. }
  152. }