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