GitHubTest.php 4.6 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 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/', $this->origin)),
  48. $this->isFalse(),
  49. $this->anything()
  50. )
  51. ->willReturn('{}')
  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. return $this->getMock('Composer\Config');
  103. }
  104. private function getRemoteFilesystemMock()
  105. {
  106. $rfs = $this
  107. ->getMockBuilder('Composer\Util\RemoteFilesystem')
  108. ->disableOriginalConstructor()
  109. ->getMock()
  110. ;
  111. return $rfs;
  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. }