GitHubTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 Composer\Util\Http\Response;
  15. use PHPUnit\Framework\TestCase;
  16. use RecursiveArrayIterator;
  17. use RecursiveIteratorIterator;
  18. /**
  19. * @author Rob Bast <rob.bast@gmail.com>
  20. */
  21. class GitHubTest extends TestCase
  22. {
  23. private $username = 'username';
  24. private $password = 'password';
  25. private $authcode = 'authcode';
  26. private $message = 'mymessage';
  27. private $origin = 'github.com';
  28. private $token = 'githubtoken';
  29. public function testUsernamePasswordAuthenticationFlow()
  30. {
  31. $io = $this->getIOMock();
  32. $io
  33. ->expects($this->at(0))
  34. ->method('writeError')
  35. ->with($this->message)
  36. ;
  37. $io
  38. ->expects($this->once())
  39. ->method('askAndHideAnswer')
  40. ->with('Token (hidden): ')
  41. ->willReturn($this->password)
  42. ;
  43. $httpDownloader = $this->getHttpDownloaderMock();
  44. $httpDownloader
  45. ->expects($this->once())
  46. ->method('get')
  47. ->with(
  48. $this->equalTo($url = sprintf('https://api.%s/', $this->origin)),
  49. $this->anything()
  50. )
  51. ->willReturn(new Response(array('url' => $url), 200, array(), '{}'));
  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, $httpDownloader);
  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. $httpDownloader = $this->getHttpDownloaderMock();
  77. $httpDownloader
  78. ->expects($this->exactly(1))
  79. ->method('get')
  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, $httpDownloader);
  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. return $this->getMockBuilder('Composer\Config')->getMock();
  103. }
  104. private function getHttpDownloaderMock()
  105. {
  106. $httpDownloader = $this
  107. ->getMockBuilder('Composer\Util\HttpDownloader')
  108. ->disableOriginalConstructor()
  109. ->getMock()
  110. ;
  111. return $httpDownloader;
  112. }
  113. private function getAuthJsonMock()
  114. {
  115. $authjson = $this
  116. ->getMockBuilder('Composer\Config\JsonConfigSource')
  117. ->disableOriginalConstructor()
  118. ->getMock()
  119. ;
  120. $authjson
  121. ->expects($this->atLeastOnce())
  122. ->method('getName')
  123. ->willReturn('auth.json')
  124. ;
  125. return $authjson;
  126. }
  127. private function getConfJsonMock()
  128. {
  129. $confjson = $this
  130. ->getMockBuilder('Composer\Config\JsonConfigSource')
  131. ->disableOriginalConstructor()
  132. ->getMock()
  133. ;
  134. $confjson
  135. ->expects($this->atLeastOnce())
  136. ->method('removeConfigSetting')
  137. ->with('github-oauth.'.$this->origin)
  138. ;
  139. return $confjson;
  140. }
  141. public static function recursiveFind($array, $needle)
  142. {
  143. $iterator = new RecursiveArrayIterator($array);
  144. $recursive = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
  145. foreach ($recursive as $key => $value) {
  146. if ($key === $needle) {
  147. return $value;
  148. }
  149. }
  150. }
  151. }