BitbucketTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\Util\Bitbucket;
  13. /**
  14. * @author Paul Wenke <wenke.paul@gmail.com>
  15. */
  16. class BitbucketTest extends \PHPUnit_Framework_TestCase
  17. {
  18. private $username = 'username';
  19. private $password = 'password';
  20. private $authcode = 'authcode';
  21. private $message = 'mymessage';
  22. private $origin = 'bitbucket.org';
  23. private $token = 'bitbuckettoken';
  24. public function testUsernamePasswordAuthenticationFlow()
  25. {
  26. $io = $this->getIOMock();
  27. $io
  28. ->expects($this->at(0))
  29. ->method('writeError')
  30. ->with($this->message)
  31. ;
  32. $io->expects($this->exactly(2))
  33. ->method('askAndHideAnswer')
  34. ->withConsecutive(
  35. array('Consumer Key (hidden): '),
  36. array('Consumer Secret (hidden): ')
  37. )
  38. ->willReturnOnConsecutiveCalls($this->username, $this->password);
  39. $rfs = $this->getRemoteFilesystemMock();
  40. $rfs
  41. ->expects($this->once())
  42. ->method('getContents')
  43. ->with(
  44. $this->equalTo($this->origin),
  45. $this->equalTo(sprintf('https://%s/site/oauth2/access_token', $this->origin)),
  46. $this->isFalse(),
  47. $this->anything()
  48. )
  49. ->willReturn(sprintf('{}', $this->token))
  50. ;
  51. $config = $this->getConfigMock();
  52. $config
  53. ->expects($this->exactly(2))
  54. ->method('getAuthConfigSource')
  55. ->willReturn($this->getAuthJsonMock())
  56. ;
  57. $config
  58. ->expects($this->once())
  59. ->method('getConfigSource')
  60. ->willReturn($this->getConfJsonMock())
  61. ;
  62. $bitbucket = new Bitbucket($io, $config, null, $rfs);
  63. $this->assertTrue($bitbucket->authorizeOAuthInteractively($this->origin, $this->message));
  64. }
  65. private function getIOMock()
  66. {
  67. $io = $this
  68. ->getMockBuilder('Composer\IO\ConsoleIO')
  69. ->disableOriginalConstructor()
  70. ->getMock()
  71. ;
  72. return $io;
  73. }
  74. private function getConfigMock()
  75. {
  76. $config = $this->getMock('Composer\Config');
  77. return $config;
  78. }
  79. private function getRemoteFilesystemMock()
  80. {
  81. $rfs = $this
  82. ->getMockBuilder('Composer\Util\RemoteFilesystem')
  83. ->disableOriginalConstructor()
  84. ->getMock()
  85. ;
  86. return $rfs;
  87. }
  88. private function getAuthJsonMock()
  89. {
  90. $authjson = $this
  91. ->getMockBuilder('Composer\Config\JsonConfigSource')
  92. ->disableOriginalConstructor()
  93. ->getMock()
  94. ;
  95. $authjson
  96. ->expects($this->atLeastOnce())
  97. ->method('getName')
  98. ->willReturn('auth.json')
  99. ;
  100. return $authjson;
  101. }
  102. private function getConfJsonMock()
  103. {
  104. $confjson = $this
  105. ->getMockBuilder('Composer\Config\JsonConfigSource')
  106. ->disableOriginalConstructor()
  107. ->getMock()
  108. ;
  109. $confjson
  110. ->expects($this->atLeastOnce())
  111. ->method('removeConfigSetting')
  112. ->with('bitbucket-oauth.'.$this->origin)
  113. ;
  114. return $confjson;
  115. }
  116. }